Benefits of resolving dependencies and IoC in asp.net mvc

Possible duplicate:
Why do I need an IoC container and not a simple DI code?

I read several articles on this topic and I did not find any brilliant advantages. For example, this code:

//some action in a controller //simplest solution: var repository = new EmployeeRepository(); var model = repository.ListAllEmployees(); 

Well, you can say: in this solution, the controller / action is very dependent on the EmployeeRepository. Dependency Resolution:

 var repository = DependencyResolver.Current.GetService<IEmployeeRepository>(); var model = repository.ListAllEmployees(); 

I did not find it better than the following, without resolving the / ioc dependency:

 var model = Managers.EmployeeManager.ListAllEmployees(); 

Hope someone can give me some ideas / links / posts on this topic.

+6
source share
2 answers

Your DependencyResolver example is not much better than your source code, so yes .. there is not much benefit from this.

Fortunately, this is not how you should do it. Instead, you use a constructor or property nesting.

I agree with @adriaanp, lighter unit testing is only a side benefit of dependency injection. The biggest benefit is that it encourages a dependency-free architecture. Your classes are autonomous and independent of a specific implementation (except for the interface and requirements).

Another benefit is that the IoC container can control the life of your dependencies. Suppose you want an object to exist for the lifetime of a request on a web page. Also suppose you want to access this object in many different classes. You can create an object in the page load event, and then pass it to each object that you create it. However, this may mean that you need to pass it to objects that do not actually use them, simply because they create objects that need it (or create objects that create objects that need it, or ... etc.).

When using dependency injection and an IoC container, the container controls the lifetime of the object, and it deals with injecting it into the objects that need it. You do not need to create this in your projects. You just get it for free.

And finally, for the testing problem. When you check something, if there is a hard-coded new one there, you cannot easily replace it with a mocked object to pass it to test data. Suppose you want to verify that the object is calculating the value correctly. Providing test data with known values ​​simplifies testing.

+10
source

The main advantage of control inversion is decoupling. By unleashing, you improve maintainability. Your controller / action depends on the interface, and the implementation is injected into the controller.

Resolving dependencies through an IoC container simplifies the dependency injection logic for your controller by configuring the container. If it really becomes clear, if one class depends on the service, this service depends on other services, and these services may also depend on more services, the container will handle this resolution of dependencies.

Many people can tell you to implement inversion of control in order to be able to test your code, this is not the same as inverting control sentences, but makes it more reliable, but this is not the main reason.

+5
source

Source: https://habr.com/ru/post/901411/


All Articles