ASP. NET MVC Model binding (IV)

Source: Internet
Author: User

ASP. NET MVC Model binding (IV) Preface

The previous space gives a rough explanation of the model binder Imodelbinder and the implementation type, model binder provider, and can imagine the model binder as a large container, why do you say so? Leave a question here.

First, the controller's method parameters may be many types, may be multiple of the same type, in response to this situation the MVC framework uses the binding implementation is ivalueprovider to do, and for the different types of parameters and so on some cases, There are also significant differences in the implementation types of Ivalueprovider, and these specific implementations will be explained in the following pages.

All say onlooker sees most, we do not go into the MVC framework, stand outside to see. This article will be outside the perspective of the Ivalueprovider to do a description.

Model binding
    • Imodelbinder , custom model Simple implementation of the binder
    • Model Binder in MVC position in the frame
    • MVC the default model in Binder Generation Process
    • Imodelbinderprovider the simple application
    • Ivalueprovider in MVC the positions and procedures generated in the framework
    • Ivalueprovider the application Scenario
    • Ivalueprovider the realization of the Namevaluecollectionvalueprovider

Ivalueprovider the location and process generated in the MVC framework

The location of the build

Can you recall the description of where the model binder was generated in the ASP. NET MVC Model binding (ii), here's a description of the generated location,

Figure 1

As shown in Figure 1, in the Blue Line execution process, the Ivalueprovider type is generated after the model binder is generated, saying that the build is a bit inappropriate to get it. Why do you say that in the build section below

The resulting process

Let's look at the implementation code for the Blue Line process in 11.

Code 1-1

protected Virtual ObjectGetparametervalue (ControllerContext controllercontext, Parameterdescriptor parameterdescriptor) {T Ype ParameterType=Parameterdescriptor.parametertype; Imodelbinder Modelbinder= This.            Getmodelbinder (Parameterdescriptor); Ivalueprovider Valueprovider=ControllerContext.Controller.ValueProvider; stringstr = ParameterDescriptor.BindingInfo.Prefix??Parameterdescriptor.parametername; predicate<string> propertyfilter =Getpropertyfilter (Parameterdescriptor); Modelbindingcontext context2=NewModelbindingcontext {fallbacktoemptyprefix= ParameterDescriptor.BindingInfo.Prefix = =NULL, Modelmetadata= ModelMetadataProviders.Current.GetMetadataForType (NULL, ParameterType), ModelName=str, modelstate=controllerContext.Controller.ViewData.ModelState, PropertyFilter=PropertyFilter, Valueprovider=Valueprovider}; Modelbindingcontext BindingContext=context2; return(Modelbinder.bindmodel (ControllerContext, BindingContext)??parameterdescriptor.defaultvalue); }

For the method shown in code 1-1, the return type and the function of this method do not need to be taken care of, what we want to know now is how Ivalueprovider is coming!!!

From code 1-1, we can clearly see that after the model binder is generated, the MVC framework obtains a reference to the controller requested by the current request from the ControllerContext controller context parameter object. The Ivalueprovider type is then obtained according to the current controller object reference.

The MVC framework then instantiates the Modelbindingcontext type and assigns the Ivalueprovider type you just acquired to the Valueprovider attribute in it.

For the Modelbindingcontext type, the model binding context object, look at its definition code 1-2.

Code 1-2

 Public classModelbindingcontext { PublicModelbindingcontext ();  PublicModelbindingcontext (Modelbindingcontext bindingcontext);  Public BOOLFallbacktoemptyprefix {Get;Set; }  Public ObjectModel {Get;Set; }  PublicModelmetadata Modelmetadata {Get;Set; }  Public stringmodelname {Get;Set; }  PublicModelstatedictionary Modelstate {Get;Set; }  PublicType Modeltype {Get;Set; }  Publicpredicate<string> PropertyFilter {Get;Set; }  Publicidictionary<string, modelmetadata> PropertyMetadata {Get; } //        //Summary://Gets or sets the value provider. //        //return Result://The value provider.          PublicIvalueprovider Valueprovider {Get;Set; } }

All we need to do here is to get a basic understanding of the Modelbindingcontext type, and go back to the topic, which is directly obtained from the reference to the current controller object, so let's take a look at the Valueprovider property in the controller. Let's take a look at the controller type, code 1-3.

Code 1-3

 Public Abstract class controller:controllerbase, Iactionfilter, Iauthorizationfilter, IDisposable, Iexceptionfilter, iresultfilter{   ...}

I had a joke with everyone to relieve the atmosphere. There is no attribute in the controller type that we are looking for, and some friends think about it in the base class type, indeed in the Controllerbase type (code 1-4).

Code 1-4

 Public Abstract class controllerbase:icontroller{...     Public Get Set ; }}

Do we have to assign a value to the Controller object when we are using Ivalueprovider?

Of course not, let's take a look at the implementation of the Valueprovider property in code 1-4, example code 1-5.

Code 1-5

 PublicIvalueprovider Valueprovider {Get            {                if( This. _valueprovider = =NULL)                {                     This. _valueprovider = ValueProviderFactories.Factories.GetValueProvider ( This.                ControllerContext); }                return  This. _valueprovider; }            Set            {                 This. _valueprovider =value; }        }

As you see here, it should be understood that the origin of the Ivalueprovider type is derived from the factories attribute of the system's valueproviderfactories type, based on the current controller context.

Here we look at the definitions of several related types that generate the Ivalueprovider type, example code 1-6.

Code 1-6

 Public Static classvalueproviderfactories {//Summary://gets the collection of value provider factories for the application. //        //return Result://A collection of value-Provider factory objects.          Public StaticValueproviderfactorycollection Factories {Get; } }
Public classValueproviderfactorycollection:collection<valueproviderfactory> { Publicvalueproviderfactorycollection (); PublicValueproviderfactorycollection (ilist<valueproviderfactory>list); //Summary://Returns a value provider factory for the specified controller context. // //Parameters://ControllerContext://an object that encapsulates information about the current HTTP request. // //return Result://The value provider factory object that is used to specify the controller context. Publicivalueprovider Getvalueprovider (ControllerContext controllercontext); protected Override voidInsertItem (intindex, valueproviderfactory item); protected Override voidSetItem (intindex, valueproviderfactory item); } Public Abstract classValueproviderfactory {protectedvalueproviderfactory (); //Summary://returns a value provider object for the specified controller context. // //Parameters://ControllerContext://an object that encapsulates information about the current HTTP request. // //return Result://A value-provider object. Public Abstractivalueprovider Getvalueprovider (ControllerContext controllercontext); }

Valueproviderfactories type of this pattern has seen too much before, it is not said, it has a valueproviderfactorycollection type of static properties, The valueproviderfactorycollection type is also the collection type of the Valueproviderfactory type, So when the Ivalueprovider type is eventually generated, the valueproviderfactorycollection type is traversed first, Get an instance of each valueproviderfactory type and generate the Ivalueprovider type, which is also the first match rather than the best match.

Incidentally, a controller context object can be used to classify the generation logic in the Valueproviderfactory type and to generate different ivalueprovider types for different controllers. The use of the Ivalueprovider type is explained in the following pages.

Jinyuan

Source: http://www.cnblogs.com/jin-yuan/

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

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.