A little understanding of asp.net core DI container

 

Two major elements: ServiceCollection and ServiceProvider


From the UML class diagram above, we can see that ServiceCollection is actually a collection, which stores the correspondence between interfaces and implementations. The Item of this collection is the ServiceDescriptor class. The ServiceDescriptor class has two properties of type Type. One represents the interface type, and the other represents the type of the implementation class, forming a corresponding relationship.


ServicePorvider can be understood as a provider of specific services. It is created by the ServiceCollection's extension method BuildServiceProvider. Whatever service we need, just get it from the provider. From the above figure, you can see that ServicePorvider has a method called: GetService. This method will find the corresponding implementation class from the ServiceDescriptor collection (ServiceCollection) according to the interface we provide. When the ServiceType is consistent with the incoming interface type, the ImplementationType implementation type is obtained from the matching ServiceDescriptor, and finally the type is instantiated and returned to us.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324975202&siteId=291194637