Recently in the study of open source code, just found Asp.net5 source,: Https://github.com/aspnet.
Today is mainly about dependencyinjection this part, for your reference, but also welcome to visit treatise. Gossip does not say much, the following code for a simple analysis
The project structure is as follows:
Contains Dependencyinjection, Dependencyinjection.abstractions, DEPENDENCYINJECTION.AUTOFAC, Dependencyinjection.ninject and dependencyinjection.tests five projects.
- Dependencyinjection.abstractions basic interfaces and classes that are used when exposed to the underlying information.
- Dependencyinjection the Di interface implemented by Microsoft itself
- DEPENDENCYINJECTION.AUTOFAC implementation of Di using AUTOFAC (the component does not know whether it is open source)
- Dependencyinjection.ninject the di that is implemented using Ninject (which does not know whether to open source)
- Dependencyinjection.tests for the above four projects of the test code, this part can not see, but for the use of Di is still a reference value.
dependencyinjection.abstractions
The project contains basic interfaces as well as some underlying classes/enumerations. For interface-oriented programming, the user does not need to know the details of the implementation, only need to understand the corresponding interface. Three other projects (Dependencyinjection, DEPENDENCYINJECTION.AUTOFAC, Dependencyinjection.ninject) is the implementation of the relevant interface of the project, you can choose one of the implementation, that is, the use of Microsoft this di,dependencyinjection.abstractions is a must reference class library, the other three projects ( As indicated above), introduce one.
Dependencyinjection.abstractions the files contained in this project are not many, the following I will be the project file:
The project lacks a critical interface-IServiceProvider, whichis located under System and is defined as follows:
Public Interface IServiceProvider { object GetService (Type servicetype); }
The purpose of this interface is to follow the incoming type and convert it to the corresponding instance, which is the core of Di and the class/interface of the final work.
However, the definition of the interface is flawed, the return value type is object type, the user needs to be forced to type conversion, so the interface is extended (serviceproviderextensions), the extension is defined as follows (the implementation is omitted):
Public Static classserviceproviderextensions{ Public StaticT getservice<t> ([Notnull] ThisIServiceProvider provider); Public Static ObjectGetrequiredservice ([Notnull] ThisIServiceProvider provider, [Notnull] Type servicetype); Public StaticT getrequiredservice<t> ([Notnull] ThisIServiceProvider provider); Public StaticIenumerable<t> getrequiredservices<t> ([Notnull] ThisIServiceProvider provider); Public Staticienumerable<Object> getrequiredservices ([Notnull] ThisIServiceProvider provider, [Notnull] Type servicetype);}
Instances created using Serviceprovidoer actually have a corresponding life cycle, and the framework uses an enumeration Servicelifetime representation, defined as follows:
Public enum Servicelifetime { Singleton,// globally unique Scoped,// Certain range of Transient// instant }
The total is divided into three kinds, three kinds of time is Transient<scoped<singleton. Transient represents only the current instance (a new one is created each time), scoped represents the current scope (which is defined in Servicescope), and Singleton represents the family's only (similar to singleton).
Iservicescopefactory and Iservicescope, these two interfaces are very simple, so the name of the iservicescopefactory is a scope of a factory class, producing a Servicescope object. The Iservicescope object generates a Iserviceprovidoer object, generally by Iservicescope, which is the scope represented by the enumeration scoped. These two interfaces are defined as follows:
Public Interface iservicescopefactory { iservicescope createscope (); } Public Interface iservicescope:idisposable { get;} }
About Iservicescope Perhaps not quite understand, I will tests project under the test code to take out, slightly modified (to replace Var to the actual interface, easy to read)
//Services. Addscoped<ifakescopedservice, fakeservice> (); Public voidnestedscopedservicecanberesolved () {IServiceProvider container=CreateContainer (); Iservicescopefactory outerscopefactory= Container. Getservice<iservicescopefactory>(); using(Iservicescope Outerscope =Outerscopefactory.createscope ()) {iservicescopefactory innerscopefactory= outerscope.serviceprovider.getservice<iservicescopefactory>(); using(Iservicescope Innerscope =Innerscopefactory.createscope ()) {Ifakescopedservice Outerscopedservice= outerscope.serviceprovider.getservice<ifakescopedservice>(); Ifakescopedservice Innerscopedservice= innerscope.serviceprovider.getservice<ifakescopedservice>(); Assert.notequal (Outerscopedservice, Innerscopedservice); } } }
The relationships of the above classes are probably as follows:
[ASP. 5] Dependencyinjection Project Code Analysis