I need a dependency injection container here

I have the following dependencies that need to be configured at the beginning of my program:

//Set up dependencies $fileSettings = array(12, 'extra_lines', 'close_on_save'); $exporter = new Exporter('exporterpath/filename.txt', $fileSettings); $outputStream = new OutputStream(); $businessModel = new BusinessModel('param1', 'param2'); //Run application $application = new Application($exporter, $outputStream, $businessModel); $application->start(); 

So, you can see that I have several dependencies for my main application, and this is the code needed to bootstrap so that everything is necessary.

Does this mean that the container is for dependency injection?

If not, is the factory method applicable?

+4
source share
1 answer

This is a good article for dependency injection (even PHP!).

http://fabien.potencier.org/article/12/do-you-need-a-dependency-injection-container

From the article:

In most cases, you do not need a dependency injection container for the benefits of dependency injection.

But when you need to manage many different objects with a lot of dependencies, a container for dependency injection can be really useful (for example, think about the structure).

I believe that the correct answer is related to how complex your script is. At some point, something, somewhere, should know how to put it all together. If it gets tedious and ugly, then there may be time for a container.

In fact, you never had to have a container. This is just a good idea because of the flexibility it provides.

+2
source

All Articles