Here is a sample code from everyone's favorite Nerddinner tutor

public class DinnersController : Controller { IDinnerRepository dinnerRepository; // // Dependency Injection enabled constructors public DinnersController() : this(new DinnerRepository()) { } public DinnersController(IDinnerRepository repository) { dinnerRepository = repository; } ... 

What difference does it make if I create a repository object in controller methods?

  public ActionResult Edit(int id) { DinnerReposotiry dinnerRepository = new DinnerRepository(); Dinner dinner = dinnerRepository.GetDinner(id); if (!dinner.IsHostedBy(User.Identity.Name)) return View("InvalidOwner"); return View(dinner); } 

    2 answers 2

    Using DependencyInjection provides three benefits.

    1. The ability to replace some service that is not technologically neutral.
    2. Ability to automatically test different modules of the program independently. In your example, Dependency Injection allows you to write an automated test that checks that the DinnersController correctly calls the methods of the DinnerRepository.
    3. Ability to reuse DinnersController code with other IDinnerRepository implementations.

      The difference is that in the consumer class you will work with the object at the level of abstractions and be able to substitute any implementation of the abstract interface. I will give an example: You have an interface that provides a certain set of operations. Today, the customer wants the GetSomeInfo () method of implementing this interface to refer to the local database, as well as to the web service in the future. At the initial stage, you write the implementation of MyBDInterfaceImplementation, which has GetSomeInfo () to the database. The consumer class simply knows about the interface that it owns the GetSomeInfo method and calls it. Then you implement another class that implements this interface and the GetSomeInfo () method in this case accesses the web service. As a result, in the consumer class you do not need to change anything, only in the place where the constructor of this class is called to transfer an object of another type to it. Thus, in practice, the user of your application chooses to use a local database — you transfer the first implementation of the interface to the consumer class constructor if the user selects a web service — in a similar way, substitute the implementation with access via the web service. The next plus is testability. First, it is easy to write using TDD, User Stories, and so on. Secondly, it is easy to test using mocks, dummy implementations, and so on.

      Fowler's article on IoC will also be useful. Rob Martin's books on agil-development and clean code.

      I hope the answer to the question helped to understand.

      • Fine! Found a related article in Dependency Inversion - Andrey Ashgaliyev
      • Inversion! = Injection. <br> <br> There are two patterns: Dependency Injection and Inversion of Control. - Modus
      • @tokyoshitzko What article did you find? Give me a link. - Mikhail Danshin