Three methods of dependency Injection Using Unity in ASP. NET MVC

Source: Internet
Author: User

In ASP. in NET MVC4, in order to solve the coupling between the Controller and the Model, we usually need to introduce IoC in the Controller activation system to process the Controller of user requests, make the Controller dependent on the ModelRepository abstraction rather than its implementation. We can use IoC in three stages to implement the decoupling operation mentioned above. First, we need to briefly introduce the Controller activation process by default: the user sends the request to the Black ASP.. NET, the routing system parses the request, matches the request according to the registered routing rules, and parses information such as the name of the Controller and Action. The parsed information is handed over to an MvcRouteHandler object for processing. MvcHttpHandler contains a ControllerFactory member. If the constructor does not provide an object that implements the IControllerFactory interface, the default constructor calls ControllerBuilder. current. getControllerFactory () gets an object like this. The system calls the aforementioned object Sino-German GetHttpHandler and obtains an MvcHandler object that implements the IHttpHandler interface to finally process the request. Call the BeginProcessRequest method in MvcHandler to continue processing the request. In the method, obtain the information of Controller and Action from the Information parsed in 1, and then use two types of IControllerFactory objects to activate the Controller object, and finally execute the corresponding Action. The first method is known from the above two methods. We can create our own IControllerFactory object to implement dependency injection. However, we can directly inherit defacontrocontrollerfactory and override the GetControllerInstance method, in this way, you do not need to re-implement other functions. The following is a simple example of UnityControllerFactory space controllerfactory inherited by Unity: namespace UnitySample {public class UnityControllerFactory: DefaultControllerFactory {private IUnityContainer container; public UnityControllerFactory (IUnityContainer container) {this. container = container;} protected override IController GetControllerInstance (RequestContext requestContext, Type controllerType) {return null = = ControllerType? Null: (IController) this. container. resolve (controllerType); // return base. getControllerInstance (requestContext, controllerType) ;}}we can use ControllerBuilder in App_Start to set the system to use this ControllerFactory IUnityContainer container = new UnityContainer (); container. registerType <IXXXRepository, XXXRepository> (); UnityControllerFactory = new UnityControllerFactory (container); ControllerBuilder. current. setCo NtrollerFactory (factory); in the second method, in the ultcontrollerfactory inherited above, a member of ControllerActivator is used to activate the Controller. If the created object does not provide an IControllerActivator object, A DefaultControllerActivator object that implements IControllerActivator by default is provided. This interface contains the Create method used to Create the Controller object, and there is an IControllerActivator Type constructor in DefaultControllerFactory to define it. Therefore, we can use a custom object to implement the IControllerActivator for dependency injection. Namespace UnitySample {public class UnityControllerActivator: IControllerActivator {private IUnityContainer container; public UnityControllerActivator (IUnityContainer container) {this. container = container;} public IController Create (RequestContext requestContext, Type controllerType) {return controllerType = null? Null: (IController) container. registerType (controllerType) ;}} modify the code in App_Start in method 1 and use the ControllerActivator: IUnityContainer container = new UnityContainer (); container. registerType <struct, XXXRepository> (); // UnityControllerFactory = new UnityControllerFactory (container); IControllerActivator controllerActivator = new container (container); DefaultControllerFactory defa UltFactory = new defacontrocontrollerfactory (controllerActivator); ControllerBuilder. current. setControllerFactory (defaultFactory); the third method, like in the DefaultControllerFactory class, also has a constructor containing a parameter (type: IDependencyResolver) and a constructor without parameters, by default, defacontrocontrolerfactory uses a non-argument constructor to instantiate a DefaultControllerActivator object. In this case, a default IDependencyResolver object is provided. Therefore, we can use a custom IDependencyResolver class to implement dependency injection. Methods GetService and GetServices exist in the IDependencyResolver interface to parse specific types namespace UnitySample {public class handler: IDependencyResolver {private IUnityContainer container; public handler (IUnityContainer container) {this. container = container;} public object GetService (Type serviceType) {return container. resolve (serviceType);} public IEnumerable <object> GetServices (Type serviceType) {return container. resolveAll (serviceType) ;}} modify the method in App_Start and use this custom DependencyResolver: IUnityContainer container = new UnityContainer (); container. registerType <IXXXRepository, XXXRepository> (); UnityDependencyResolver resolver = new UnityDependencyResolver (container); DependencyResolver. setResolver (resolver );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.