Welcome Guest, Not a member yet? Register   Sign In
Don't Repeat Yourself in CI doesn't exist
#1

[eluser]gh0st[/eluser]
CodeIgniter is advertised as one of the many frameworks out there that works best with the "so-called" DRY principle.

I call it "so-called" because it doesn't exist, well not in my experience of using CI.

I've noticed not only am I repeating myself, but I'm repeating myself just as much as I was BEFORE I worked with MVC.

For example, no CRUD library is installed for CI so you end up writing controllers to add/edit/delete for every table, so you are essentially repeating yourself for every table you want to append a CRUD capacity to.

Any time you want to check whether the user is logged in, has the right priveleges, etc you enter in a if statement (probably at the constructor level at every constructor you want it in), but every time you copy/paste this code in you are repeating yourself.

In controllers you load the libraries custom to each controller to save on load time, etc -- but this again is repetition.

In some controllers they'll be pagination, but not likely in all controllers -- but any time you put it in you're repeating yourself.

---

Another issue I'm frustrated with is this concept that because I'm using CI I no longer have to worry about changing fields everywhere to make an application work.

But its still the same, I don't see anything NEW or different.

Lets say you want to list all cars, so you have a model to pull out all cars and feed this into an array, which is looped and presented in a view.

But lets say you change, or add a field in the database -- you have to add this field in every controller, every method, every view where you want/require it.

For example;
Code:
foreach ($query->result as $row):
print $row->car_name;
// any new fields have to be added to this loop, and everywhere else too where the change is require.
endforeach;

Where is the DRY in this?

Where is this fancy concept of not changing fields everywhere? It doesn't exist!

---

Finally, I always get this feeling that there is meant to be a correct way of doing MVC, DRY, etc, etc -- but I never see any code or examples of how to do it, nor does CI come automatically bundled with libraries which are meant to make it easier.

For example, if HMVC is so good, why isn't it bundled automatically? If ORM libraries/plugins are meant to be so good, why aren't they bundled automatically?

If the concept is to make development easier -- why do I have to keep going round in circles to get CI the way that its been advertised to work?
#2

[eluser]Crimp[/eluser]
Good rant. A little repetitive, but still good.
#3

[eluser]davidbehler[/eluser]
Ever tried to create a new controller class by extending the CI class? There you can add auth check to the constructor and don't have to do it in every contoller.

Auto-loading of libraries, models, configs, etc. is a pretty nice feature aswell! That saves quite some repetition.

Imho one can reduce repetition to a mininum by outsourcing often used functions and usage of libraries, auto-loading and so on but you can't get totally rid of it.
#4

[eluser]johnwbaxter[/eluser]
@crimp - I see what you did there...

@gh0st - Let me answer a couple of your questions. With regards to the auth check having to be in every controller, that is not quite true. You could do a couple of things here, first you could create a hook that ran before the controller runs that does your auth check - http://ellislab.com/codeigniter/user-gui...hooks.html

That only needs to be written once. Secondly you could Extend the Core controller which you would put your auth code in and then all your other controllers would extend your customised parent controller - http://ellislab.com/codeigniter/user-gui...asses.html

Your other questions are architectural really. You could write your own code and way of including libs relying on some logic or other. At the end of the day you can do what you want to do. Codeigniter does not stand in your way.

Finally with regards to HMVC and ORM being bundled, i agree with you to a certain extent. Why can't they be included as optional additions to CI, you only load them if you need them like other libs and stuff. I simply think that at the moment EE doesn't use ORM so CI does not have ORM. However, there are some excellent user contributed HMVC offerings and also some great ORM offerings, i know they're not supported by CI and that is an issue but they're there so why not use them?!

Top rant though.
#5

[eluser]Colin Williams[/eluser]
Everything you seem to see as hindrances, I (and others, I'm sure) see as flexibility.

If you just don't get MVC yet, don't give up. It's a pretty basic design pattern. I can't figure out why so many people fuss over it.

When you really understand CI from a high-level, I'm sure your frustrations will disappear, and you'll see how to use CI in a DRY way.

Also, have you looked at any user contributed code? Like, look at the blogging software people have released, and don't miss Derek's Bamboo Invoice. These give you an opportunity to pop the hood and take a look at how CI can be used effectively.
#6

[eluser]xwero[/eluser]
I just checked and CI doesn't gets advertised as being a DRY framework on the site, simple and easy is the vibe from the frontpage i'm getting.

DRY is something you have to do yourself. As mentioned before, the most basic example of DRY programming is the autoload config file. If you notice you use a helper,library,model,plugin in all your controllers just add it to autoload.php and it will be loaded without code in the controllers.

DRY code is done by programming when a certain functionality needs to be available and you can do that by autoload or hooks or extending CI classes. Or by convenience, you can find and example of it here

I think it's a good thing most code is optional or third party. CI doesn't have to be another framework with a massive amount of libraries, helpers and plugins. Everything is done already, it's only the way you think it should work that makes you pick a certain piece of code or write it yourself.

I get the feeling you blame CI for you not wanting to find the best solution for your applications.
#7

[eluser]Colin Williams[/eluser]
It's particularly revealing that you say you had a hard time keeping DRY before you used CI. Maybe it's just PHP's fault!
#8

[eluser]GSV Sleeper Service[/eluser]
I think Symfony may be more your kind of thing
#9

[eluser]Colin Williams[/eluser]
Quote:For example, no CRUD library is installed for CI so you end up writing controllers to add/edit/delete for every table, so you are essentially repeating yourself for every table you want to append a CRUD capacity to.

Have you ever considered that each data object in your application might have unique needs with regard to CRUD-like operations? Get you some IgnitedRecord or DataMapper if you want even more abstraction.

Quote:Lets say you want to list all cars, so you have a model to pull out all cars and feed this into an array, which is looped and presented in a view.

But lets say you change, or add a field in the database—you have to add this field in every controller, every method, every view where you want/require it.

Same thing. Just consider that this introduction to the automobile object model requires specific handling, by all components. The very fact that you have separated your app into MVC components makes it easy to handle this addition. If you wanted something more automated, you might have wanted to use something like Drupal, whose CCK module provides a GUI for object modeling.

Quote:Any time you want to check whether the user is logged in, has the right priveleges, etc you enter in a if statement (probably at the constructor level at every constructor you want it in), but every time you copy/paste this code in you are repeating yourself.

Fine-grained control is a good thing! Like, say I show an upload form for privileged users and nothing for unprivileged users. And putting access checks in a controller constructor gives you access control for the whole object. How much broader do you want it.

Also, every time I create a new controller, I have to copy stuff like "class Example extends Controller" in every controller file and I'm repeating myself.

I just think your concept of DRY is way off.
#10

[eluser]xwero[/eluser]
Colin i think you made your point Smile




Theme © iAndrew 2016 - Forum software by © MyBB