What does dependency injection infrastructure do?

I understand the basic concept of Injection Dependency . Instead of having a lot of global state, you instead pass what you need for the constructors of your various objects.

But I do not understand how this concept can be applied to the structure? What does the Injection Dependency framework do for you and when should you use it?

+4
source share
5 answers

The framework picks up everything for you based on some kind of configuration (be it this code, an XML file, etc.).

This is plumbing, basically.

Many DI frameworks also include aspect-oriented programming, object lifecycle management, etc., but the basics of DI are to make your application work and work with objects that talk to each other.

Another way: the DI structure is a bit of code that calls the constructors and says that all this will be executed when it is done :)

+5
source

The main thing I use it for is that it allows me to connect new implementations of an object without having to recompile. I can just change the configuration file and my objects are connected correctly.

+1
source

The best way to find out how useful they are is to try a little. I wanted to tell Spring.NET to try, it looks like a good implementation.

0
source

One thing that I really like about AOP is that itโ€™s very easy to move between development and production versions. You can create very detailed logging and then basically flip the xml switch and it disappears.

0
source

One of the main advantages of using DI / IOC is the reduction in communication between different classes.

When you use this approach, you need to set up a hierarchy of objects that express the dependencies that you selected from your code. This can turn into a lot of code. Even if you use part of the DI framework code, this can significantly reduce the amount of configuration, allowing you to customize in a more expressive way:

  • Whenever an ILogger is required, use FileLogger.
  • You can go even further than conventions whenever a controller is requested I [Name]
    replace it with [Name] Controller.
  • You can have singleton controls for you. This way, your code is the same as with any other dependency, but DI guarantees you get the same instance of all the time

A couple of structural configurations that I use:

ForRequestedType<ILogger>().TheDefaultIsConcreteType<NLogLogger>(); //the following injects any property that has a type //that implements IController. (overcomes a regular asp.net limitation with DI) SetAllProperties( p => p.TypeMatches(t => t.IsConcreteAndAssignableTo(typeof(IController))) ); 
0
source

All Articles