• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dependency injection

I just read a mice article about dependency injection by the lead developer of symphony.

If it's translated to CI a common example is when developers want to load models in other models to use one or more methods. So instead of doing
function model_method()
    $CI =& get_instance();
    $result = $this->other_model->method();
    // use $result now
function controller_method()
   $result = $this->model_two->method($this->model_one->method());
This adds more code to the controller but the coupling between the models is a lot looser.

Some of the core libraries could use a looser coupling too.

Great article. It's certainly given me something to think about. Thanks for sharing. However, if a model requires another model, then it's dependant on that model, so shouldn't that model load it? It's not quite the same as injecting another object to be used from the outside. In the articles examples, the object being injected doesn't need to be made aware of the object being injected into it. With models loading models, I think it's different. The model depends on the model it's loading.

The model method isn't depending on the other models method but on the data outputted by that model.

Indeed, which is why (to me anyway) it makes sense that the first model includes the second one, as it's a dependency. Sounds analogous to expecting your dog to order a takeaway for you, when it would have been a lot easier to have placed the order yourself. (Hope this make sense).

What if you load one model in the other and at one point during the apps/sites lifetime you or another developer changes the controller because a method from the internal model to be called. Because the internal model isn't visible you are likely to try to reload that model again. The loader method will prevent this but it's one call that isn't needed.
What if later on data from somewhere else in another method has to be injected with the data from the method you use the other models controller in.

Next to making your code more maintainable, by flexibility, Dependency injection makes things more visible in the long run too. It doesn't matter if it is an object with methods or just data.

I see what your saying, but it seems a bit fruitless to me having a model (or any other object for that matter) loaded in the scope of the current function that you don't necessarily need to use. Personally, I think the issue should be addressed in the model loader. We should have the option to pass a model back by reference, or load into the CodeIgniter super object. If the model doesn't need to be available in the global scope, then we should be able to assign it to a class property instead.

Then, if we were to specify that we want the model passed back by reference, it should not be added to the instantiated models list, and should not be put into the CodeIgniter global scope. The loader should allow for multiple instantiations of the same object.

However, with that said, the model file itself should only need to be loaded once. Once it's loaded, you'd instantiate the class with the "new" keyword, so if anything, I feel that the model loader should allow us to specify whether or not we want the model instantiated in the global scope.

At the current time, the model loader assumes we want a model instantiated and globally accessible within the scope of the CI Super Object, but this may not always be the desired behaviour.

Nice article!

It is a nice addition to method overloading. Altough I like the latter better. It does wonders with CI's _remap() and libraries.

But one request;-) Do not post interesting articles on a friday, it will keep me busy all weekend trying to find uses for it!


TheFuzzy0ne i think your idea about models creates complexity that isn't needed. If a model method requires data from another model to do its job both of the models are needed. Maybe you only need other methods from the first model to get the content for the page but that doesn't make the other model a secondary citizen.

It is true the coupling in the controller will create more code than coupling in the model but i think it's a small price to pay for better maintainable code.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.