Three ways to use unity for Dependency injection in ASP.

Source: Internet
Author: User

in ASP. NET MVC4, in order to solve the coupling between controller and model, we usually need to introduce IOC in the controller activation system to handle the controller of the user request. Let the controller rely on the abstraction of modelrepository rather than its implementation.

We can use IOC to implement the decoupling operation described above in three stages, first of all we need to briefly describe the controller's activation process by default:

1, the user sends the request black ASP, the routing system parses the request, matches the request according to the registered routing rule, resolves the controller and the action name and so on information.

2, the parsed information to a Mvcroutehandler object for processing, mvchttphandler in the presence of a controllerfactory member, If the constructor does not provide an object that implements the Icontrollerfactory interface, the default constructor obtains one such object by calling ControllerBuilder.Current.GetControllerFactory ().

3, the system calls the above object in Germany Gethttphandler obtained a Mvchandler object that implements the IHttpHandler interface final processing request.

4. Call the BeginProcessRequest method in Mvchandler to continue processing the request, and obtain the Controller and action information from the information resolved in 1 in the method, The Controller object is then activated using 2 types of Icontrollerfactory objects, and the corresponding action is eventually executed.

The first of these methods

From the above 2, we can create our own Icontrollerfactory objects to implement dependency injection, but we can do this by directly inheriting the defaultcontrollerfactory and overriding the GetControllerInstance method. This eliminates the effort to re-implement some of the other features.

Here's a simple example of a unitycontrollerfactory that inherits from Defaultcontrollerfactory created with unity:

1 namespaceUnitysample2 3 {4 5  Public classunitycontrollerfactory:defaultcontrollerfactory6 7 {8 9 PrivateIunitycontainer container;Ten  One  Publicunitycontrollerfactory (Iunitycontainer container) A  - { -  the  This. Container =container; -  - } -  + protected OverrideIController getcontrollerinstance (RequestContext requestcontext, Type controllertype) -  + { A  at return NULL= = Controllertype?NULL: (IController) This. Container. Resolve (controllertype); -  - //return base. GetControllerInstance (RequestContext, controllertype); -  - } -  in } -  to}

We can use this controllerfactory with the Controllerbuilder setup system in App_start

1 New UnityContainer (); 2 3 container. Registertype (); 4 5 New unitycontrollerfactory (container); 6 7 ControllerBuilder.Current.SetControllerFactory (Factory);

The second method of

In the defaultcontrollerfactory inherited above, a member of the Controlleractivator is used to enable the controller to be activated, If a Icontrolleractivator object is not provided in the creation object, a type of Defaultcontrolleractivator object that implements the Icontrolleractivator by default is provided. There is a create method for creating a controller object in this interface, and in Defaultcontrollerfactory there is a construction method of the Icontrolleractivator type to make it. So we can use a custom implementation of an object that implements self-icontrolleractivator as an excuse for dependency injection.

1 namespaceUnitysample2 3 {4 5  Public classUnitycontrolleractivator:icontrolleractivator6 7 {8 9 PrivateIunitycontainer container;Ten  One  Publicunitycontrolleractivator (Iunitycontainer container) A  - { -  the  This. Container =container; -  - } -  +  PublicIController Create (RequestContext requestcontext, Type controllertype) -  + { A  at returnControllertype = =NULL?NULL: (IController) container. Registertype (controllertype); -  - } -  - } -  in}

Modify the code in App_start in Method 1, using this controlleractivator:

1Iunitycontainer container =NewUnityContainer ();2 3 container. Registertype ();4 5 //Unitycontrollerfactory factory = new unitycontrollerfactory (container);6 7Icontrolleractivator Controlleractivator =NewUnitycontrolleractivator (container);8 9Defaultcontrollerfactory defaultfactory =Newdefaultcontrollerfactory (controlleractivator);Ten  OneControllerBuilder.Current.SetControllerFactory (defaultfactory);

The third method of

As in the Defaultcontrollerfactory class, there is also a parameter in Defaultcontrolleractivator (type Idependencyresolver) Constructor and a constructor without parameters, Defaultcontrolerfactory instantiates a Defaultcontrolleractivator object using the parameterless constructor by default. In this case, a default Idependencyresolver object is provided. So we can also implement dependency injection using a custom idependencyresolver class. There are methods GetService and getservices in the Idependencyresolver interface to parse the specific type

1 namespaceUnitysample2 3 {4 5  Public classUnitydependencyresolver:idependencyresolver6 7 {8 9 PrivateIunitycontainer container;Ten  One  Publicunitydependencyresolver (Iunitycontainer container) A  - { -  the  This. Container =container; -  - } -  +  Public ObjectGetService (Type servicetype) -  + { A  at returncontainer. Resolve (servicetype); -  - } -  -  Publicienumerable<Object>getservices (Type servicetype) -  in { -  to returncontainer. ResolveAll (servicetype); +  - } the  * } $ Panax Notoginseng}

Modify the method in App_strat, using this custom dependencyresolver:

1 iunitycontainer container=new  unitycontainer (); 2 3 container. Register type<ixxxrepository,xxxrepository>(); 4 5 unitydependencyresolver resolver=new  unitydependencyresolver (container); 6 7 dependencyresolver.setresolver (resolver)

Three ways to use unity for Dependency injection in ASP.

Related Article

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.