AutofacYes. NetPlatform dependency Injection(Di, dependency injection)Container with closeness and fitC #Language features. As application systems become increasingly large and complexAutofacContainers can "flat" the complex class dependencies to manage the relationships between components, providing good adaptability and convenience.
In this blog, we will applyAutofacTo build a traditional model with dependency injection.ASP. NETPages and services/Link between the middle layer to establish a link between "Presentation" and "control.
Then, how to inject dependency(Dependency injection)ImplantationASP. NETMedium?
ASP. NETThe entire page lifecycle process isASP. NETWorker Process Control, which basically breaks down the process we want to use "Constructor injection" in this process.(Constructor injection)"./The middle layer requires us to complete the implantation process before entering the page lifecycle. Based on this consideration, we can useIhttpmoduleBased on "Property injection"(Property injection). The implementation details are as follows:
1,ReferenceDLL
DirectionASP. NETApplicationProgramAdd the following reference:
Autofac. dll
Autofac. Integration. Web. dll
2,ConstructorApplicationStarted togetherContainerproviderAttribute
Before entering the page lifecycle, we chooseGlobalClass. EnableGlobalClass implementationAutofac. Integration. WebIn the namespaceIcontainerprovideraccessorInterface. This interface exposesContainerproviderIs the service consumer.(In this exampleASP. NETPage)Provide the root container. This attribute should be built when the application is started, that isApplication_startEvent completed. Specific ConstructionCodeAs follows:
Code
// Build application containers and register Dependencies
VaR Builder = New Containerbuilder ();
Builder. Register < Empmng > (). < Iempmng > (). Httprequestscoped ();
// ... Register other Dependencies...
// After registration, you can set the containerprovider attribute
_ Containerprovider = New Containerprovider (builder. Build ());
3,SetWeb. config
Based onIhttpmoduleInWeb. config.
Code
< Configuration >
< System. Web >
< Httpmodules >
<! -- IIS6 settings -->
< Add
Name = "Containerdisposal"
Type = "Autofac. Integration. Web. containerdisposalmodule,
Autofac. Integration. Web" />
< Add
Name = "Propertyinjection"
Type = "Autofac. Integration. Web. Forms. propertyinjectionmodule,
Autofac. Integration. Web" />
</ Httpmodules >
</ System. Web >
< System. Webserver >
<! -- Iis7 settings -->
< Modules >
< Add
Name = "Containerdisposal"
Type = "Autofac. Integration. Web. containerdisposalmodule,
Autofac. Integration. Web"
Precondition = "Managedhandler" />
< Add
Name = "Propertyinjection"
Type = "Autofac. Integration. Web. Forms. propertyinjectionmodule,
Autofac. Integration. Web"
Precondition = "Managedhandler" />
</ Modules >
</ System. Webserver >
</ Configuration >
Note:
Containerdisposalmodule, Page request ended,AutofacRelease the previously created component/Service.
PropertyinjectionmoduleBefore the page lifecycle, inject dependencies to the page.
4,Page property acquisition Service/Middle Layer
In. AspxA public attribute is placed on the page background to receive services./Middle layer.
// Used to receive service/intermediate layer attributes
Public Iempmng somedependency { Get ; Set ;}
Protected Void Page_load ( Object Sender, eventargs E)
{
Lblemp. Text = This . Somedependency. selectoneemp ();
}
The method described above is just rough.ASP. NETPages and services/The connectivity of the middle layer has many details, such as the service/Can the organization and connectivity of the intermediate layer be introduced?SingletonMode. Further research is required. InIOCThe field has just arrived, and I hope you will be well versed in your shortcomings and mistakes!