I study WPF and MVVM and have a little understanding of the principles of SOLID. As a result, I heard about such concepts as DI and IoC. There are many articles on this subject in the network, but basically all of them are poorly structured, because of this there is no general picture of what to look for. As a result, the question arose: where to start, in what order and what should be studied in order to know a good and flexible architecture?

UPD. There are already ready DI containers, such as Unity or Ninject. I would like to hear what it is, how much they are needed, and is it worth it to pay attention to them at all?

    1 answer 1

    If you read about SOLID, then you should have understood approximately what DIP (Dependency inversion principle) is - the principle of dependency inversion. This is when your code is tied to abstractions and not details.

    DIP has two rules: - the modules of the upper levels should not depend on the modules of the lower levels. Both types of modules should depend on abstractions. - abstractions should not depend on the details. Details must depend on abstractions.

    If you open and read the principles of IoC, you will see the same DIP. This is a theory, and the implementation of the theory is called an IoC container. This is done through:

    • Template factory or Service locator
    • Dependency injection (Eng. Dependency injection)
      • Through the designer (English Constructor injection)
      • Through the class method (English Setter injection)
      • Through the injection interface (eng. Interface injection)

    Dependency injection is quite simple and the description takes about three pages of printed text: https://habrahabr.ru/post/131993/

    All you have to do is follow the two rules of DIP and understand whether your class depends on details or on abstractions (interfaces). If you see in the code that you are addressing something concrete and not abstract, you need to fix it.

    • I try to adhere to these principles. I know there are ready-made DI containers, such as Unity or Ninject. I would like to hear what it is, how much they are needed, and is it worth it to pay attention to them at all? - Lightness
    • @Lightness, they simplify dependency injection and make it possible to store interface-> class bindings in one place. I didn’t look at Unity, but Ninject has a good wiki page that shows how an injection is done without Ninject (Dependency Injection By Hand) and with Ninject (Dependency Injection With Ninject). Can be found on the left in the menu Ninject-> Introduction - Alex Krass