How to use DI and DI containers
Posted
by
Pinetree
on Programmers
See other posts from Programmers
or by Pinetree
Published on 2012-09-02T19:24:40Z
Indexed on
2012/09/02
21:48 UTC
Read the original article
Hit count: 328
I am building a small PHP mvc framework (yes, yet another one), mostly for learning purposes, and I am trying to do it the right way, so I'd like to use a DI container, but I am not asking which one to use but rather how to use one.
Without going into too much detail, the mvc is divided into modules which have controllers which render views for actions. This is how a request is processed:
- a Main object instantiates a Request object, and a Router, and injects the Request into the Router to figure out which module was called.
- then it instantiates the Module object and sends the Request to that
- the Module creates a ModuleRouter and sends the Request to figure out the controller and action
- it then creates the Controller and the ViewRenderer, and injects the ViewRenderer into the Controller (so that the controller can send data to the view)
- the ViewRenderer needs to know which module, controller and action were called to figure out the path to the view scripts, so the Module has to figure out this and inject it to the ViewRenderer
- the Module then calls the action method on the controller and calls the render method on the ViewRenderer
For now, I do not have any DI container set up, but what I do have are a bunch of initX() methods that create the required component if it is not already there. For instance, the Module has the initViewRenderer() method. These init methods get called right before that component is needed, not before, and if the component was already set it will not initialize it. This allows for the components to be switched, but it does not require manually setting them if they are not there.
Now, I'd like to do this by implementing a DI container, but still keep the manual configuration to a bare minimum, so if the directory structure and naming convention is followed, everything should work, without even touching the config.
If I use the DI container, do I then inject it into everything (the container would inject itself when creating a component), so that other components can use it? When do I register components with the DI? Can a component register other components with the DI during run-time? Do I create a 'common' config and use that? How do I then figure out on the fly which components I need and how they need to be set up?
If Main uses Router which uses Request, Main then needs to use the container to get Module (or does the module need to be found and set beforehand? How?) Module uses Router but needs to figure out the settings for the ViewRenderer and the Controller on the fly, not in advance, so my DI container can't be setting those on the Module before the module figures out the controller and action...
What if the controller needs some other service? Do I inject the container into every controller? If I start doing that, I might just inject it into everything...
Basically I am looking for the best practices when dealing with stuff like this. I know what DI is and what DI containers do, but I am looking for guidance to using them in real life, and not some isolated examples on the net.
Sorry for the lengthy post and many thanks in advance.
© Programmers or respective owner