I
I
Ivan Balan2015-01-08 16:07:40
ASP.NET
Ivan Balan, 2015-01-08 16:07:40

Why are Dependency Containers needed?

I've read and looked around a lot, but it still doesn't work. Who can humanly explain why this is necessary and in what cases it is necessary to use this approach?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-01-08
Protko @Fesor

What to hands services not to initialize. Write your component/service, pass the implementation of the interface to the constructor (that is, your class depends only on the interface), and the container, depending on the settings, will slip the implementation you need.
At the same time, your class is not tied to a specific implementation, the Dependency Inversion principle is observed, it is easy to cover with tests, and at any time in the container config you can replace the implementation without affecting any other code. Well, less boilerplate.
You can also restrict access to different layers of the application so that there would be no temptation from one layer (for example, presentation) to pull something from the request processing layer ... well, or I don’t know ...
There are also options with collectors. That is, we collect all services that implement some kind of interface (for example, drivers) and register them in another service.

A
Alexander S, 2015-02-23
@FirstX

To separate the flies from the cutlets.
Imagine that you conditionally have 3 departments. And they are all very closely related. Well, they can't work without each other, that's all. As a result, I wanted to change something in the second department, be so kind as to change in the other two, because they directly depend on it.
The first thing that comes to mind is the creation of abstract interfaces through which all interaction occurs. Ok, created. But abstract interfaces can't do anything, that's why they're abstract. So somewhere you need to explicitly create an implementation for this abstraction. The question becomes where to do it. To create within one class - instances of another, this is also a strong connection between them. For this, they came up with a box where all correspondences between interfaces and implementations will be stored. I wanted to get the implementation of such and such an interface, I asked the responsible manager, he himself looked into his box and pulled out what you need. And they called it all Dependency Container. That is, it is a specific tool for creating weak links between modules.
Well, the weak coupling of modules and abstractions between them give all sorts of useful features, such as quick switching between implementations (for example, a real / test environment), makes it easier to refactor individual modules, and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question