ASP. NET Web API Model-valueprovider

Source: Internet
Author: User

ASP. NET Web API Model-valueprovider

Objective

the previous article explains Model metadata,the model metadata is an important part of the model binding, except that there are more knowledge points involved in the model binding, for ASP . Framework of the ASP. NET Web API framework in the Model binding part of the new parameter binding mechanism, which will be explained in the following space, the previous space is to explain the theoretical knowledge is not related to the demonstration of the example, This people do not have to hurry in the final model part of the basic knowledge after the completion of the above, I will put all the above mentioned in series, and today this space for everyone to bring in the model binding plays a crucial part, we do not need to control what model binding, but simply to understand Valueprovider This series of object models, because before the combination of knowledge points before and after concatenation, we do not know Valueprovider where it was executed. So let's have a look at the Valueprovider related objects.

Model-valueprovider

Figure 1

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/49/BE/wKioL1QZfzPR_T_QAAGM5t3jfmM887.jpg "title=" Valueprovider1.png "width=" 738 "height=" 295 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:738px;height:295px; "alt=" Wkiol1qzfzpr_t_qaagm5t3jfmm887.jpg "/>


Ivalueprovider interface Type --valueprovider Behavior Constraints

First we look at the right part of the 1 and start with a Ivalueprovider interface type, so let 's look at the definition of the interface:

Example code 1-1

Publicinterfaceivalueprovider {boolcontainsprefix (stringprefix);   Valueproviderresultgetvalue (Stringkey); }

we are in the code1-1seen in theIvalueprovidertwo methods are defined in the interface, one isContainsprefix ()method to receiveStringparameter of type and returns theboolvalue type, this method represents the value of the specified prefix to view the currentValueproviderThe prefix value is present in the following example, and then theGetValue ()method is to return the corresponding value according to the key value of the execution, from here we can probably guess thisValueprovidershould be similar to the same type as the key-value team, and the returned results are encapsulated in theValueproviderresulttype, this type will be explained later. In theIvalueproviderinterface Constraints GoodValueproviderafter the behavior of the value provider, we should take a look atValueproviderThe underlying structure of the value provider. But before we do, we're going to have to figure out1As shown in, let's take a look atIenumerablevalueproviderinterface type definition, what is the main responsibility of this interface?


Ienumerablevalueprovider interface Type -valueprovider Behavior Constraints

Example code 1-2

Publicinterfaceienumerablevalueprovider:ivalueprovider {idictionary<string, String>GetKeysFromPrefix (str   Ingprefix); }

from the code 1-2 It is clear that the responsibility for seeing the Ienumerablevalueprovider interface type is simple, retrieving the specified prefix value and finally returning as a key-value team, This will be explained in more detail below.


Namevaluepairsvalueprovider type -valueprovider Basic Structure

Example code 1-3

Publicclassnamevaluepairsvalueprovider:ienumerablevalueprovider, Ivalueprovider {PublicNameValuePairsValueProv        Ider (func<ienumerable<keyvaluepair<string, String>>>valuesfactory, CultureInfoculture); Publicnamevaluepairsvalueprovider (ienumerable<keyvaluepair<string, String>>values,         Cultureinfoculture);        Publicvirtualboolcontainsprefix (Stringprefix);        Publicvirtualidictionary<string, String>getkeysfromprefix (Stringprefix);    Publicvirtualvalueproviderresultgetvalue (Stringkey); }

in code 1-3 We see the definition of the Namevaluepairsvalueprovider type, let's start with its constructor, the difference between the two constructors is that the first one is func< Ienumerable<keyvaluepair<string, a constructor parameter of type string>>>, and the second is ienumerable<keyvaluepair< String, the constructor argument of the string>> type, and the first parameter type of the second constructor is actually the return type of the first constructor, which you can see here, in fact, in the internal implementation, and the declaration that the first constructor is nothing, When declaring the second constructor, the parameter is encapsulated again as a delegate.

for The keyvaluepair<t,u> type can be understood as a subkey of a key-value team, where only one key value in its type corresponds to one value, which is itself.

As for the remaining three methods, we still rely on a simple example to illustrate.

Example code 1-4

    publicclassvalueprovidercasecontroller : apicontroller   {         publicstringget ()          {                         keyvaluepair<string,string>[] dictionary=newkeyvaluepair <string,string>[]            {                 newkeyvaluepair<string, String> ("Employeesinfo.name", "Jinyuan"),                 newKeyValuePair<string,string> ("Employeesinfo.age", ",  ")               newkeyvaluepair<string, String> ("Employeesinfo.Sex "," male "),                 Newkeyvaluepair<string,string> ("EmployeesInfo.AddressInfo.AddressInfo", "Nanjing"),                 newKeyValuePair<string,string> (" EmployeesInfo.AddressInfo.ZipCode "," 210000 ")              };             Namevaluepairsvalueprovidernamevaluepairsvalueprovider=newnamevaluepairsvalueprovider (dictionary,null);             stringbuilderstrbuilder=newstringbuilder ();             namevaluepairsprefixanalysis ( strbuilder, namevaluepairsvalueprovider,  "Employeesinfo");             returnstrbuilder.tostring ();        }          Privatevoidnamevaluepairsprefixanalysis (stringbuilderstringbuilder,  Namevaluepairsvalueprovidernamevaluepairs,stringprefix)         {             IDictionary<string, string> Dictionarys=namevaluepairs. Getkeysfromprefix (prefix);                         if  (Dictionarys. count>0)             {                 console.writeline (prefix+ " Retrieves the data source key value for the prefix ... ");                 foreach  (Varitemindictionarys)                 {                     console.writeline ("Key:" +item. key+ " value:" +item. Value);                }                 foreach  ( Keyvaluepair<string, string>keyvalueindictionarys)                  {                     namevaluepairsprefixanalysis (stringbuilder,  Namevaluepairs, keyvalue. Value);                }             }            else             {                 stringbuilder. Appendline (prefix+ ":" +namevaluepairs. GetValue (prefix). Rawvalue.tostring ());            }         }   }

Let's look at the code 1-4, first I define a keyvaluepair<string,string>[] type in the Get () method , in order to be able to instantiate Namevaluepairsvalueprovider type, after which you can see that I called a My custom namevaluepairsprefixanalysis () method, and in which I use the An instance of the Namevaluepairsvalueprovider type calls the Getkeysfromprefix () method, which is the behavior that is constrained by the code 1-2. Let's take a look at table 1.

Table 1



Key

Value

Employeesinfo.name

Jinyuan

Employeesinfo.age

24

Employeesinfo.sex

Man

EmployeesInfo.AddressInfo.AddressInfo

Nanjing city

EmployeesInfo.AddressInfo.ZipCode

210000


Table 1 represents the initial data source, which is our definition of the keyvaluepair<string,string>[] type of key, value schematic table.

However, when we use An instance of the Namevaluepairsvalueprovider type has been "Employeesinfo" as the prefix called the Getkeysfromprefix () method after the returned the value of the idictionary<string,string> type is shown in table 2.

Table 2


Key

Value

Name

Employeesinfo.name

Age

Employeesinfo.age

Sex

Employeesinfo.sex

AddressInfo

Employeesinfo.addressinfo

the table 2 value here is only the first-level relationship value.

after this we output the current to retrieve the prefix value and the retrieved value, and will be traversed in table 2 value values as the prefix value again to prefix the data source for retrieval, if not, then there is no searchable.

and use The GetValue () method called by the Namevaluepairsvalueprovider type instance is based on the prefix value that has not been retrieved by the last suffix, which is the Key value in the original data source .

Finally, let's look at the results.

Figure 2

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/49/BC/wKiom1QZgH3SDw29AAL_-3eRrtA219.jpg "title=" Valueprovider2.png "width=" 738 "height=" 181 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:738px;height:181px; "alt=" Wkiom1qzgh3sdw29aal_-3errta219.jpg "/>

At the client we get the value, of course here is just a demo example, the value provider provides the value direction reversed.

Then we can see the retrieved records on the server, and we can clearly see that there are two layers of structure in it. Interested friends delve into the specific implementation of the search.

Querystringvalueprovider type -valueprovider Specific Structure

Example code 1-5

   publicclassQueryStringValueProvider : NameValuePairsValueProvider    {        publicquerystringvalueprovider ( Httpactioncontextactioncontext, cultureinfoculture)              : base (func, culture)         {             func<ienumerable<keyvaluepair<string,  string>>>func=null;            if   (Func==null)             {                 func=  ()  => ActionContext.ControllerContext.Request.GetQueryNameValuePairs ();             }        }   } 

from Code 1-5, you can see the execution of the constructor in the Querystringvalueprovider type initialization, encapsulating the request query string as the original data source as the delegate type. The constructor for the base class is then called.


Routedatavalueprovider type -valueprovider Specific Structure

Example code 1-6

   publicclassroutedatavalueprovider : namevaluepairsvalueprovider   {         publicroutedatavalueprovider (HttpActionContextactionContext,  cultureinfoculture)             : base ( Func, culture)         {             Func<IEnumerable<KeyValuePair<string, string>>>func=null;             if  (func==null)              {                 func=  ( =>getroutes) ( ActionContext.ControllerContext.RouteData);             }        }   } 

As with the above 1-5, the values in httproutedata are used as the original data source.

The left part of Figure 1 will be explained in the following space, where it is not suitable, will feel incoherent, although after reading this article do not know what role does not know how to use, feel pinched neck the same uncomfortable, But in the back I will put all the above mentioned in series to do a demonstration, shown this is the basic part of the knowledge, is a cushion.


This article is from the "Jinyuan" blog, please be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1554575

ASP. NET Web API Model-valueprovider

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.