Home c# IOC containers, are they necessary?

IOC containers, are they necessary?

Author

Date

Category

When using the principle of “inversion of dependencies”, it is recommended to use the introduction through the constructor, but ultimately, with an increase in the complexity of the application, the number of dependences that must be implemented are increasing. A huge class of God appears, which manages all these dependencies (this approach is also called Poor Man’s Di “Dependency Injection In .Net”).

Such code, at least it turns out to be tested, except for the class that controls dependencies.

When using the IOC container, the same approach is actually obtained, so why use then more additional settings for containers and extra frameworks?

When using the IOC container, we can ask you to request a copy you need (pre-configured, of course), (HTTPS : //Habr.com/post/131993/ ):

// where you need to create an instance of Scheduleviewer We instead of new, do this:
ScheduleViewer ScheduleViewer = Ninjectkernel.get & LT; Scheduleviewer & GT; ();

And whether the connection of the code increases, when are we everywhere to insert such queries for the created instance? After all, then the code becomes practically not tested (due to the modular testing)


Answer 1, Authority 100%

You are absolutely right to use ninjectkernel.get & lt; Scheduleviewer & gt; () Instead of New Scheduleviewer () – Not the best idea. Here only di does not boil down to the constant ninjectkernel.get , it is an anti-pattern.

Instead of constant access to the IOC container, you need to request all your dependencies as the parameters of the designer:

class foo
{
  Private Readonly Scheduleviewer Viewer;
  Public Foo (Scheduleviewer Viewer)
  {
    this.viewer = viewer;
  }
}

If you need to create new objects – you should request their factory:

class foo
{
  Private Readonly Func & Lt; Scheduleviewer & GT; Viewerfactory;
  Public Foo (FUNC & LT; ScheduleViewer & GT; ViewerFactory)
  {
    this.viewerfactory = viewerfactory;
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions