ASP. NET Web API model-valueprovider Preface
The previous article explains the model metadata, which is an important part of the model binding, except that there are more knowledge points involved in the model binding, and for the ASP. The API framework in the model binding section has added a 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 is in the model binding plays a crucial part, we do not have to tube what model binding, It is simply to understand the Valueprovider series of object models, because we do not know where Valueprovider is executed before the concatenation of the knowledge points before and after the combination. So let's have a look at the Valueprovider related objects.
Model-valueprovider
Figure 1
Ivalueprovider interface Type --valueprovider Behavior Constraints
First we look at the right part of 11 and start with a Ivalueprovider interface type, so let's look at the definition of the interface:
Example code 1-1
Public Interface Ivalueprovider { bool containsprefix (string prefix); Valueproviderresult GetValue (string key); }
As we see in code 1-1, the Ivalueprovider interface defines two methods, one is the Containsprefix () method, receives a string parameter and returns a bool value type. This method indicates whether the prefix value is present in the current valueprovider based on the specified prefix value, which is followed by an example later, and then the GetValue () method, which is the corresponding value returned from the executed key value. From here we can probably guess that this valueprovider should be similar to the key-value team, and the returned result is encapsulated in the Valueproviderresult type, which is described later. After the Ivalueprovider interface constrains the behavior of the Valueprovider value provider, we should take a look at the infrastructure of the Valueprovider value provider. But before we do, let's start by looking at the definition of the Ienumerablevalueprovider interface type as shown in Figure 1, what is the main responsibility of this interface?
Ienumerablevalueprovider interface Type -valueprovider Behavior Constraints
Example code 1-2
Public Interface Ienumerablevalueprovider:ivalueprovider { IDictionary<stringstring> Getkeysfromprefix (string prefix ); }
It is clear from code 1-2 that the responsibility for seeing the Ienumerablevalueprovider interface type is simple, retrieving the specified prefix value, and finally returning as a key-value team, which is described in detail below.
Namevaluepairsvalueprovider type -valueprovider Basic Structure
Example code 1-3
Public classNamevaluepairsvalueprovider:ienumerablevalueprovider, Ivalueprovider { PublicNamevaluepairsvalueprovider (func<ienumerable<keyvaluepair<string,string>>>valuesfactory, CultureInfo culture); PublicNamevaluepairsvalueprovider (ienumerable<keyvaluepair<string,string>>values, CultureInfo culture); Public Virtual BOOLContainsprefix (stringprefix); Public Virtualidictionary<string,string> Getkeysfromprefix (stringprefix); Public VirtualValueproviderresult GetValue (stringkey); }
In code 1-3, we see the definition of the Namevaluepairsvalueprovider type, first of all its constructor, the difference between the two constructors is that the first one is Func<ienumerable<keyvaluepair <string, the constructor parameter of the string>>> type, the second is the constructor parameter of the ienumerable<keyvaluepair<string, the string>> type, The first parameter type of the second constructor is actually the return type of the first constructor, which can be seen here, in fact, in the internal implementation, it is also the declaration of the first constructor is nothing, when declaring the second constructor, the argument is actually encapsulated as a delegate.
For a keyvaluepair<t,u> type that can be understood as a subkey of a key-value team, 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
Public classValueprovidercasecontroller:apicontroller { Public stringGet () {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") }; Namevaluepairsvalueprovider Namevaluepairsvalueprovider=NewNamevaluepairsvalueprovider (Dictionary,NULL); StringBuilder Strbuilder=NewStringBuilder (); Namevaluepairsprefixanalysis (Strbuilder, Namevaluepairsvalueprovider,"Employeesinfo"); returnstrbuilder.tostring (); } Private voidNamevaluepairsprefixanalysis (StringBuilder StringBuilder, Namevaluepairsvalueprovider namevaluepairs,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(varIteminchDictionarys) {Console.WriteLine ("Key:"+ Item. Key +"Value:"+item. Value); } foreach(keyvaluepair<string,string> KeyValueinchDictionarys) {Namevaluepairsprefixanalysis (StringBuilder, Namevaluepairs, KeyValue. Value); } } Else{StringBuilder. Appendline (prefix+":"+Namevaluepairs. GetValue (prefix). Rawvalue.tostring ()); } }}
Let's look at code 1-4, first I defined a keyvaluepair<string,string>[] type in the Get () method, in order to be able to instantiate the Namevaluepairsvalueprovider type, After this we can see that I have called a namevaluepairsprefixanalysis () method that I have customized, And an instance of the Namevaluepairsvalueprovider type in which the Getkeysfromprefix () method is called, that is, the behavior that code 1-2 constrains. 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 that has "employeesinfo" as the prefix, the idictionary<string returned after the Getkeysfromprefix () method is called, The value of the 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 the GetValue () method called with 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
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
Public classQuerystringvalueprovider:namevaluepairsvalueprovider { PublicQuerystringvalueprovider (Httpactioncontext actioncontext, CultureInfo culture):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 in the constructor when the Querystringvalueprovider type is initialized, encapsulating the request query string as the original data source as the delegate type, and then calling the constructor of the base class.
Routedatavalueprovider type -valueprovider Specific Structure
Example code 1-6
Public classRoutedatavalueprovider:namevaluepairsvalueprovider { PublicRoutedatavalueprovider (Httpactioncontext actioncontext, CultureInfo culture):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, here is not suitable for, will feel incoherent, although after reading this article do not know what role does not know how to use, feel pinched neck of the same uncomfortable, but I will be in front of the contents of the whole series together to do a demonstration, As shown in this is the basic part of the knowledge, is a cushion.
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
ASP. NET Web API Model-valueprovider