The Unity framework is a classic implementation of the IOC pattern, which configures the section via the Config file, container the interface to the implementation, and the sections in config that correspond to the full name, so that the application does not need to be like the Ninject way. Depending on the interface items and implementation items, The decoupling configuration allows the application to implement the effect of the static update service, that is, the functionality of the application update service is not Exited.
The following is the implementation of unity, the core interface Iunitycontainer is the Unity mode implementation of the foundation, in the controller life cycle Iunitycontainer to transfer the role of the section configuration, Creates an instance object of the dependent interface service when the controller is instantiated, and when the Countroller execution finishes, the instance object of the interface service is referred to iunitycontainer for maintenance
First you need to install unity, referencing the following two DLLs
next, Implement Unity's Management class, create a new MVC5 Web site project, and implement the following
public classUnitycontrollerfactory:defaultcontrollerfactory {Static ObjectSynchelper =New Object(); Staticdictionary<string, iunitycontainer> containers =Newdictionary<string, iunitycontainer>(); publicIunitycontainer UnityContainer {Get;Private Set; } publicUnitycontrollerfactory (stringContainerName ="") { if(containers. ContainsKey (containername)) { this. UnityContainer =containers[containername]; return; } Lock(synchelper) {if(containers. ContainsKey (containername)) { this. UnityContainer =containers[containername]; return; } Iunitycontainer Container=NewUnityContainer (); //Configure UnityContainerUnityconfigurationsection configsection = configurationmanager.getsection (unityconfigurationsection.sectionname) asunityconfigurationsection; if(NULL= = Configsection &&!string. IsNullOrEmpty (containername)) {Throw NewConfigurationerrorsexception ("the <unity> configuration section does not Exist."); } if(NULL!=Configsection) { if(string. IsNullOrEmpty (containername)) {configsection.configure (container); } Else{configsection.configure (container, containername); }} Containers. ADD (containername, container); this. UnityContainer =containers[containername]; } } protected Overrideicontroller getcontrollerinstance (requestcontext requestcontext, Type Controllertype) {if(NULL==Controllertype) { return NULL; } return(icontroller) this. Unitycontainer.resolve (controllertype); } public Override voidReleasecontroller (icontroller Controller) { this. Unitycontainer.teardown (controller); } }
Suppose there is a service interface in a standalone C # class library named MyInterface
public Interface iemployeerepository { IEnumerable<Employee> getemployees (string""); }
And the implementation of the service interface is named MyService in a separate C # class library
public classEmployeerepository:iemployeerepository {Private StaticIlist<employee>employees; Staticemployeerepository () {employees=NewList<employee>(); Employees. ADD (NewEmployee (guid.newguid (). ToString (),"Zhang San","male",NewDateTime (1981,8, -),"Sales Department")); Employees. ADD (NewEmployee (guid.newguid (). ToString (),"John Doe","female",NewDateTime (1982,7,Ten),"Personnel")); Employees. ADD (NewEmployee (guid.newguid (). ToString (),"Harry","male",NewDateTime (1981,9, +),"Personnel")); } publicIenumerable<employee> GetEmployees (stringID ="") { returnEmployees. Where (e = e.id = = Id | |string. IsNullOrEmpty (id) | | id = ="*"); } }
In the new MVC5 Web site project above, refer to the C # class library of myinterface, and add the following sentence to the Application_Start in the global file of the Web Site. Inject the unitycontrollerfactory we created into the Controllerbuilder initialization
ControllerBuilder.Current.SetControllerFactory (new unitycontrollerfactory ());
Add the Section Interface service map information to the Web. config of the MVC5 website to configure the interface and its service implementation class information to unity
<Unity> <Containers> <Container> <Registertype= "mvc.iemployeerepository, mvc.mvcapp"Mapto= "mvc.employeerepository, mvc.mvcapp"/> </Container> </Containers> </Unity>
The benefits of unity and Ninject are obvious, interface and implementation decoupling, without the need to directly rely on implementation items, to achieve the effect of static update, when the application is running, only the implementation of the DLL (here is MyService.dll) download replace, You can implement the service update, To automate static updates, of course, you need to do some work, such as MyService should need version detection and download function, interested can do
Unity IoC Base on MVC