inversion of control - How to use Dependency Injection and not Service Locator -


I'm hearing people saying that you should not use the service locator for your dependency injection. So how do you inject dependency without relying on a service locator? I want to try the IOC containers, but do not want to land in any anti-pattern.

Should you just set everything up, so there is a place where there is always the deepest dependency chain in all classes?

I do not have the right to be filled with dependence on all the IoC containers of choice, so what is it?

So where do you use "your container" (for rexolving)? And how to solve all this, how can you get it, as if your code goes away?

Am I missing one thing?

I should remind you what I am doing? Just do not want to fall into an anti-pattern and this requires some tips / head.

Should you just set everything up, so there is a place where always in all classes Is there a dependency chain in deeper classes? (If I / I understand that)

Yes, it is called Root Root of your application, and this is where you can get your IoC Container And configure your root type.

It is not right that all your codes are filled with dependency on the IoC container of choice, right?

Correct, it would be better not to reference your IoC container around your type because it will make them less reusable, and in general the concept of IOC container for some type .

So where do you use "your" container (for rexolving)? And how to solve all this, how can you get it, as if your code goes away? Is using the interface through every layer to the front layer a part of designing everything correctly?

You can use your container to root your structure and anywhere in your code to start the type (from the factory type) through the container (usually for dependency chain support) To do

Many IoC containers can generate these factory types for you, so you only have to pass, e.g. IMyFactory As a dependency, or in the case of some IOC containers, a Func & lt; IMyService & gt; This means that you do not need to make factory type of dependency on your IoC container.

In relation to using the interface, the dependency reversal principle states that you should be dependent on absorption, not on the congestion, so if you want to adopt dependency injection, then you get the focus of your code with this concept have to keep.

Comments