ASP. NET MVC Model binding (II)

Source: Internet
Author: User

ASP. NET MVC Model binding (II.) preface

The simple demonstration of the model binding is a bit of an understanding of how the model bindings are used, and have you ever wondered when the model binder was executed? Or what is the process of execution? These doubts will be lifted in this article, and some of the description types and contextual parameters involved will be covered in the following pages.

Model binding
    • Imodelbinder, custom model binder Simple implementation
    • Location of the model binder in the MVC framework
    • The default model binder generation process in MVC
    • Simple application of Imodelbinderprovider
    • Ivalueprovider the location and process generated in the MVC framework
    • Ivalueprovider's application Scenario
    • The realization of Ivalueprovider Namevaluecollectionvalueprovider

location of the model binder in the MVC framework

No nonsense directly into the topic, the model binder as the name implies is for the model of the binding to provide the help of such a function module, for the moment so understand. Model here refers to the ViewModel, generally in the controller method parameters used in the model, so that the model binder can be used, but have you ever wondered why this is used? Things in the world are cause and effect, of course, the use of such a way can not escape, which is related to the location of the model binder generated in the MVC framework, we look at 1.

Figure 1

See Figure 1, perhaps some friends may feel very abrupt, here is suggested that friends first to see the blogger before the ASP. NET MVC Filter series of articles, see the friends will certainly feel very familiar with, but also to consider not to see the previous length of the friend.

The Invokeaction () method of the Controlleractioninvoker type is the necessary way to execute the controller method in the MVC framework, as illustrated in Figure 1, where the yellow indicator line is the main process (though not a flowchart). First, MVC generates a controllerdescriptor type, and then regenerates the actiondescriptor type according to the controllerdescriptor type. Objects of type FilterInfo are then generated based on the actiondescriptor type, for controllerdescriptor types and The Actiondescriptor type represents the controller description type (the object contains various information of the controller) and the Controller method description type (with the controller description type one meaning), the meaning of these two types and the generation process will be explained in the following space, For the time being, just know what they represent.

The explanation for the filterinfo type is that it contains all the filter information that is currently being invoked to control its methods (bloggers or friends are advised to look at an article in the ASP. NET MVC Filter series), It can be seen from its structure that it contains the various types of filter type Collection Object properties.

The point is that, as you can see from Figure 1, the MVC framework performs the authorization authentication Iauthorizationfilter filter before executing the iauthorizationfilter filter and executing the controller behavior filter. Before Iactionfilter , MVC acquires objects of type Parameterdescriptor according to the actiondescriptor type " In this case, the friend who has seen the previous model meta-data must know that the parameterdescriptor type and model metadata are used to describe the model,parameterdescriptor The type weighs in on the model itself, and the model metadata focuses more on the external decoration of the model, lamenting the power of MVC.

The model binder (Imodelbinder) is then generated based on the ParameterType attribute in the parameterdescriptor type, representing the type of model (type). The details of the generation are explained in the next section and cannot be robbed of business downstairs.

Presumably you now know why you should put ViewModel in the controller method as a parameter to use it.

The default model binder generation process in MVC

This section explains the specific process that the model binder generates, is not too complex, and will concatenate all of these knowledge points in the subsequent length of the controller method.

Now let's take a look at the details of the model binder generation, Figure 2

Figure 2

(The bold part is represented as the property name, and the cyan in front of the boldface denotes the attribute type, whereas in boldface the representation property belongs to the type)

As illustrated in Figure 2, MVC first calls the controlleractioninvoker type Getmodelbinder () method to generate the imodelbinder type, inside the method, MVC first determines whether the Binder property in the Bindinginfo attribute in the parameter parameterdescriptor is empty, and if it is empty (this section details the parameterdescriptor type in the next article, And to explain several types of descriptions in a reversed way, it is called the binders attribute in the controlleractioninvoker type.

Now let's take a look at the definition of the binders property, such as code 1-1.

Code 1-1

protected Internalmodelbinderdictionary Binders {Get            {                if( This. _binders = =NULL)                {                     This. _binders =modelbinders.binders; }                return  This. _binders; }            Set            {                 This. _binders =value; }        }

As you can see clearly in code 1-1, For the use of the Binders property is actually using the Modelbinders.binders property in the current system context, here we take a pause, we look at the Modelbinders.binders property in the system to the default provided by the binder, code 1-2.

Code 1-2

Private Staticmodelbinderdictionary createdefaultbinderdictionary () {modelbinderdictionary Dictionary2=Newmodelbinderdictionary (); Dictionary2. ADD (typeof(HttpPostedFileBase),NewHttppostedfilebasemodelbinder ()); Dictionary2. ADD (typeof(byte[]),NewBytearraymodelbinder ()); Dictionary2. ADD (typeof(Binary),NewLinqbinarymodelbinder ()); returnDictionary2; }

There is no other meaning, is to show you the system by default several model binder types, the same is the use of the modelbinderdictionary type of the Add () method, the only difference is that this is when the system starts to add the build good, Our custom model binder is later added manually.

Tangent back to the topic, from Figure 2 we can see that the modelbinderdictionary type calls the Getbinder () method when it is actually called the modelbinderprovidercollection type of the Getbinder () method, In fact, there is a modelbinderprovidercollection type of field inside the Modelbinderdictionary type, and then look at Figure 2, The Getbinder () method in the Imodelbinderprovider type is actually called when the Getbinder () method of the Modelbinderprovidercollection type is actually called.

However, in the Anti-compilation tool also has anti-compilation, like the modelbinderprovidercollection type, I do not see the details of its instantiation, but it does not matter in the next article will prove its internal details.

This article explains how these types can be used to generate the model binder and model bindings in the following pages.


Jinyuan

Source: http://blog.csdn.net/jinyuan0829

This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.