Answer the question
In order to leave comments, you need to log in
Dependency Injection Container, tried it in practice - did not understand the meaning?
Hello everyone, I started to study the dependency injection pattern, everything seems to be clear and logical.
As far as I understand, special containers are used for convenient dependency management.
For a very long time, I could not understand how this container is used in practice, what is the scope of the container, and at what point are services added to the container?
Having asked these questions on the forums, I was advised to take any DI container and try to work with it, then it will become clear.
I decided to take a very simple DI container, from the developers of symfony - Pimple .
Wrote the SystemBlock class (system block) with 3-4 dependencies:
<?php
use Less\SystemBlock;
$systemBlock = new SystemBlock\SystemBlock(
new SystemBlock\GigabytePower,
new SystemBlock\GeForceVideo,
new SystemBlock\IntelProcessor
);
$os = new SystemBlock\LinuxOS;
$os->setUser(new SystemBlock\OSUser("Вася", "Vasya", true));
$systemBlock->setOperatingSystem($os);
$systemBlock->start();
<?php
use Less\SystemBlock;
$container = new \Pimple\Container;
$container["power"] = function ($c) {
return new SystemBlock\GigabytePower;
};
$container["video"] = function ($c) {
return new SystemBlock\GeForceVideo;
};
$container["processor"] = function ($c) {
return new SystemBlock\IntelProcessor;
};
$container["system_block"] = function ($c) {
return new SystemBlock\SystemBlock(
$c["power"],
$c["video"],
$c["processor"]
);
};
$systemBlock = $container["system_block"];
$os = new SystemBlock\LinuxOS;
$os->setUser(new SystemBlock\OSUser("Вася", "Vasya", true));
$systemBlock->setOperatingSystem($os);
$systemBlock->start();
Answer the question
In order to leave comments, you need to log in
Container
The main task of a container is to reuse services more than once, for example, a certain provider in more than 1 place or a certain Sender in 20 places throughout the application. and make it easier to access them.
Moreover, it is the container that helps to simplify some other parts of the application, for example, the router, when we register routes and their handlers (controllers), we build the application in such a way that all the arguments of these handlers and the arguments of these arguments will be pulled from the container, that is, they will greatly reduce our problems with dependency management and make the router code itself (and other code) much simpler and more reliable.
Interaction of relatively simple objects / entities within the same layer is best done in your way - it is logical and understandable, and in general the container will only interfere, even more - out of place in this task. It was noted to you in the answer on that forum.
Modern containers actually provide sooo many more features: autowiring, when you don’t need to write a config, just write a class in the constructor, and if the container finds such a class, it will configure itself (and also take into account its constructor parameters), that is, the developer it remains to work only with unique and non-standard situations.
Or gives simplified factories/builders/decorators.
Container or not, with DI testing becomes possible by definition and will be relatively easy anyway.
The main thing is not to combine these two concepts. The pattern itself is just a special case of dependency inversion, the class received the dependency through the constructor / setter, and that’s it, it doesn’t know the details, therefore it’s easier to test - just replaced it with a dummy.
And the container is a tool for working with a complex application that injects just the same, that is, it performs the work of injecting. Central to it is the word container.
Regarding your entry points: multiple entry points = multiple initializations.
As if they are inherently different applications are already obtained by definition.
But you can simplify, and bring the initialization of the container into an abstraction - into a certain App / Kernel class and do it there, and at the entry point, initialize not the container over and over again, but the specific application.
In general, I did not read the question.
When you need to test code: you need to replace objects in order not to test dependencies (during unit tests). That is, you will have a hard connection.
Plus, if you have experience, read 150 pages of perfect code, everything will fall into place.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question