Study of IOC application in ASP. NET MVC4

Source: Internet
Author: User

ASP. NET MVC has also been in contact for a long time, but because I have been mainly responsible for the background, especially the database work for the framework does not have a good understanding, especially Jing Jinnan Master of the ASP. NET MVC4 Framework analysis of a book has been bought for more than 2 years, really seriously read and practice the first chapter just. At the beginning of the old think this book is not good, thick and obscure, not easy to insist on reading, now in retrospect feel ashamed. The standard apprentice does not blame the teacher's mentality, of course, learning does need a process, from shallow into deep will be more and more like. So far, my favorite book series is still the introduction of the classic series, the content of simple exercises, easy to remember and practice. Recently there is a process of learning Java, let me slowly began to learn to read the source code, this just know that the master Jiang is the source level of analysis, and more than a lot of relevant books good place, basically every complex knowledge point he will create a relatively simple small example according to his understanding to promote the reader's understanding, is really good. Gossiping for a while, into today's topic-asp.net MVC and IOC related practices.

First of all, since the IOC is introduced in this chapter of controller, we first talk about controller knowledge. The main process of the ASP. NET MVC framework is to get the name of the controller and the name of the action by parsing the URL information, and to convert the request data contained in the request context into the argument list of the action method invocation through the model's binding mechanism. One of the things to mention here is whether the controller's execution is synchronous or asynchronous, which is actually asynchronous by default, where the Disableasyncsupport property is responsible for control, and the default is false, which I've been puzzled about for a long time. The next mention is the Sessionstatebehavior attribute in the Controllerfactory class, with default, Required, ReadOnly, Disable, so far, my session in the framework Is still not particularly clear, after clarifying and then coming back to revise.

The following diagram represents the basic process of controller activation, after the request arrives, HttpModule intercepts, Mvcroutehandler call Gethttphandler method to find the corresponding HttpHandler processing class, That is Mvchandler. Then call the class's asynchronous processing method, note that this will always use asynchronous processing, the method first through the Controllerbuilder object to get the current controllerfactory, and then through the name of the controller to find and create the controller, let's call the [Begin ]execute executes, can be asynchronous or can be synchronous execution, mentioned before. When the controller finishes executing, Controllerfactory calls release to release it.

In other words, the controller type of cache, in order to improve the efficiency of the resolution controller, the framework caches it, is a form of XML file cache, you can%windir%\ Found in the relevant subdirectory of the micrisoft.net, similar to the arearegistration cache, when required to deserialize the XML file into a List<type> object.

Finally, the application of IOC in the key controller of this paper is introduced. Prior to this, we need to introduce the reasons for applying IOC, in the actual development process, the user's request will be sent directly to the controller, if it is a call to a business function, the controller will call model (also can be external service), When the data needs to be presented, the later model gets the data converted to ViewModel for rendering. However, the controller will be directly dependent on the model (domain model) of the business layer, not the principle of dependency inversion in the design of the complex, so the IOC is introduced to reduce the coupling.

In the IOC application, we select the framework's default Dependencyresolver component and Unity component, and the current unity component has moved from CodePlex to GitHub. There is a UNITYMVC package native support IOC do not need code, direct configuration, but due to version compatibility reasons, with the implementation of the code below, the relevant content is relatively simple, not introduced. The main point here is that there are a lot of relevant integration programs, this is the most used, of course, Mr. Jiang introduced the combination with Ninject is also very good.

Public class unitydependencyresolver : idependencyresolver

{

iunitycontainer _container;

public unitydependencyresolver (iunitycontainer container)

{

_container = container;

}

public Object GetService (Type servicetype)

{

object instance;

Try

{

Instance = _container. Resolve (servicetype);

if (servicetype.isabstract | | servicetype.isinterface)

{

return null;

}

return instance;

}

catch (Exception e)

{

return null;

}

}

public IEnumerable<object> GetServices (Type servicetype)

{

return _container. ResolveAll (servicetype);

}

}

Reference:

[1] Jing Jinnan. The ASP. NET MVC4 framework revealed [M]. Shanghai: Electronic industry Press, 2012. 86-122

Study of IOC application in ASP. NET MVC4

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.