ASP. NET Web API model-actionbinding Preface
The previous sections of the model part of the knowledge points divided into a module to explain, and in the controller implementation process is divided into a number of processes, for the controller execution process (a) mainly explained the filter and in the filter space in the back also talked about, and in the filter also has some implementation process, That is, after the authorization filter is executed, the behavior filter executes before, we have to do is model binding, before also said before the model of the knowledge Point module are explained almost, today this space we will look at the source of these scattered knowledge points, that is, the model binding entry point.
model-actionbinding
httpactionbinding origin of
We know from the previous few that the entry point into the entire model binding in the ASP. NET WEB API framework is in the httpactionbinding type, as described in the previous space of this type, which encapsulates the parameterbinding array , these parameterbinding are the object that each parameter in the Controller method executes the model binding, since we know that there are many object instances of parameterbinding types in the httpactionbinding type, Then we'll see how the Httpactionbinding type is generated.
Example code 1-1
this. Setsingle<iactionvaluebinder> (new Defaultactionvaluebinder ());
First we see example code 1-1 in the service container of the httpconfiguration type, which is the Defaultactionvaluebinder type that is registered as the Iactionvaluebinder type service by default.
Example code 1-2
namespace system.web.http.modelbinding{ public class Defaultactionvaluebinder:iactionvaluebinder { public Defaultactionvaluebinder (); public virtual httpactionbinding getbinding (Httpactiondescriptor actiondescriptor); protected virtual httpparameterbinding getparameterbinding (httpparameterdescriptor parameter); }}
The definition of the Defaultactionvaluebinder type is shown in code 1-2, in which the two methods are important, and the first getbinding () method is used within the framework to make the call. According to the Httpactiondescriptor Controller method description type Object gets to our desired httpactionbinding, and its internal implementation is to call the following getparameterbinding () method, Takes the Httpactiondescriptor object to the Httpparameterdescriptor collection and then iterates through the call to the Getparameterbinding () method, In order to obtain the Httpparameterbinding object instance, and finally generate the Httpactionbinding object instance, from a design point of view, two methods in this Defaultactionvaluebinder type getbinding ( ) and the Getparameterbinding () method Use the template method pattern, which is common in frame design.
httpparameterbinding origin of
Let's say the details of the getparameterbinding () method are implemented because of the way in which the bindings are used. That is, according to the Httpparameterdescriptor type instance how to create httpparameterbinding.
Example code 1-3
protected Virtualhttpparameterbinding getparameterbinding (httpparameterdescriptor parameter) {parameterbindingattribute par Ameterbinderattribute=parameter. Parameterbinderattribute; if(Parameterbinderattribute = =NULL) {parameterbindingrulescollection parameterbindingrules=parameter. Configuration.parameterbindingrules; if(Parameterbindingrules! =NULL) {httpparameterbinding binding=parameterbindingrules.lookupbinding (parameter); if(Binding! =NULL) { returnbinding; }} Type ParameterType=parameter. ParameterType; if(Typehelper.issimpleunderlyingtype (parametertype) | |Typehelper.hasstringconverter (ParameterType)) { returnParameter. Bindwithattribute (NewFromuriattribute ()); } Parameterbinderattribute=NewFrombodyattribute (); } returnparameterbinderattribute.getbinding (parameter); }
Code 1-3 is a concrete implementation, so let's take a look at the process and the types involved.
First, the Parameterbindingattribute identifier is used on the controller method parameter based on the parameter Httpparameterdescriptor type instance. And gets an instance of the Parameterbindingattribute type. Let's take a look at the Parameterbindingattribute type definition for a moment.
Example code 1-4
[AttributeUsage (Attributetargets.parameter | AttributeTargets.Class, inherited = true , AllowMultiple = false public abstract class Parameterbindingattribute:attribute { // Methods protected Parameterbindingattribute (); public abstract httpparameterbinding getbinding (httpparameterdescriptor parameter); }
As you can see from code 1-4, this parameterbindingattribute type is suitable for types and parameters, that is, the way we choose to bind can identify this feature when the model type is defined, It is also possible to identify the parameter as appropriate when defining the Controller method.
But why is there a getbinding () method in this type? Because this type is an abstract type, that is, the template method pattern mentioned above is used, and in the subclass implementation, the httpparameterbinding type of the response is generated according to the circumstances of its own adaptation. Look at the following diagram to represent the related object model.
Figure 1
Each of the types involved in Figure 1 above can be seen in front of the space, the length of the omission of the trouble everyone to move their hands to see it.
httpparameterbinding the selection mechanism
Then the thoughts of code 1-3, after we have obtained the Parameterbindingattribute, do not know whether the parameters in this controller method are identified with Parameterbindingattribute, or whether there is an identity on the parameter type. If there is one, you can see the last code in code 1-3 and call the Getbinding () method directly using the obtained Parameterbindingattribute type, as shown in Figure 1 in the previous section.
However, there is also the case that we define the controller method when the parameters are not clearly identified we want to use some kind of binding mechanism, or in the definition of the model when there is no explicit representation, At this point the framework obtains the corresponding instance of the parameterbinding type from a defined set of rules based on the type of description currently controlling its method parameters. The following example code defines the definition of a rule collection.
Example code 1-5
Internal Staticparameterbindingrulescollection getdefaultparameterbinders () {parameterbindingrulescollection ruless=Newparameterbindingrulescollection (); Ruless. ADD (typeof(cancellationtoken), parameter =Newcancellationtokenparameterbinding (parameter)); Ruless. ADD (typeof(httprequestmessage), parameter =Newhttprequestparameterbinding (parameter)); Ruless. ADD (Delegate(httpparameterdescriptor parameter) {if(!typeof(httpcontent). IsAssignableFrom (parameter. ParameterType)) {return NULL; } returnParameter. Bindaserror (Error.format (Srresources.parameterbindingillegaltype,New Object[] {parameter. Parametertype.name, parameter. ParameterName})); }); returnruless; }
What is represented in code 1-5 is the rule definition, which means that when we use the Httpparameterdescriptor type instance to get parameterbinding from the collection, The parameterbindingrulescollection type takes the ParameterType out of our Httpparameterdescriptor type instance and matches the type of each rule previously defined, The type matches and then the corresponding delegate type is called for parameterbinding generation. What we can see from code 1-5 is that there are only cancellationtoken types and httprequestmessage types in the rules, so if we control their method parameter types as custom complex types, there is no definition here, This time the framework takes out the parametertype in the Httpparameterdescriptor type, and if it is a simple type parameter that can be converted to a string type, a Fromuriattribute type is generated as the identity. The Fromuriattribute type inherits from the Modelbinderattribute type.
If the judgment here is not passed, then it is a complex type, and finally we look at the definition in code 1-3, the final generation of the Frombodyattribute identity type, this time please refer to Figure 1.
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-actionbinding