asp.net MVC model verification based on callout feature: Dataannotationsmodelvalidatorprovider

Source: Internet
Author: User
Tags constructor

Dataannotationsmodelvalidator is ultimately created by its corresponding modelvalidatorprovider, the Dataannotationsmodelvalidatorprovider. We know from the previous introduction that it is a subclass of Associatedvalidatorprovider, which has been created from all attributes of the specified model metadata in the Getvalidators method for obtaining modelvalidator. Dataannotationsmodelvalidator only need to filter out the validation attributes that inherit from Valiationattribute and create the Dataannotationsmodelvalidator of the object.

Dataannotationsmodelvalidator

We now discuss specific modelvalidator delivery mechanisms in conjunction with Dataannotationsmodelvalidator definitions. As the following code fragment shows, Dataannotationsmodelvalidatorprovider has two static fields Attributefactories and Defaultattributefactory, The latter is a dataannotationsmodelvalidationfactory delegate, which is a dictionary with the value of this delegate as the key of the type object.

1:public class Dataannotationsmodelvalidatorprovider:associatedvalidatorprovider
2: {
3://other Members
4:internal static readonly Dictionary<type, dataannotationsmodelvalidationfactory> attributefactories;
5:internal static dataannotationsmodelvalidationfactory defaultattributefactory;
6:
7:internal static dataannotationsvalidatableobjectadapterfactory defaultvalidatablefactory;
8:internal static readonly Dictionary<type, dataannotationsvalidatableobjectadapterfactory> Validatablefactories;
9:
10:protected override ienumerable<modelvalidator> getvalidators (modelmetadata metadata, Httpactioncontext acti Oncontext, ienumerable<attribute> attributes);
11:}
12:
13:public delegate Modelvalidator dataannotationsmodelvalidationfactory (modelmetadata metadata, ControllerContext Context, Validationattribute attribute);
14:
15:public delegate Modelvalidator dataannotationsvalidatableobjectadapterfactory (Modelmetadata metadata, ControllerContext context);

The creation of Modelvalidator based on Validationattribute

Commissioned Dataannotationsmodelvalidationfactory according to Modelmetadata, ControllerContext and Validationattribute return a Modelvalidator object, and the key of the Dictionary object represented by the field attributefactories is the type of the specific validation attribute. That is, it maintains a matching relationship between a validationattribute characteristic type and a corresponding Modelvalidator factory. In the overridden Getvalidators method, for each of the specified Validationattribute, It first obtains a corresponding dataannotationsmodelvalidationfactory delegate from the Attributefactories based on its type and, if the delegate exists, uses it to create the corresponding Modelvalidator object Otherwise, the Dataannotationsmodelvalidationfactory delegate represented by the field defaultattributefactory is used to create the modelvalidator.

The creation of Modelvalidator based on Ivalidatableobject

In addition to Attributefactories and Defaultattributefactory, Dataannotationsmodelvalidatorprovider also has two static properties, Defaultvalidatablefactory and Validatablefactories, They are used to create modelvalidator for verifiable objects that implement the Ivalidatableobject interface. The type of dataannotationsmodelvalidator is another delegate of type Dataannotationsvalidatableobjectadapterfactory, The delegate creates the corresponding modelvalidator based on Modelmetadata and ControllerContext. Validatablefactories is a dictionary that takes this delegate as value and the type object as the key.

When Dataannotationsmodelvalidatorprovider completes the creation of the Modelvalidator based on the validation feature, If the model type is parsed according to model metadata, the Ivalidatableobject interface is implemented. Then get a corresponding dataannotationsvalidatableobjectadapterfactory delegate from the dictionary validatablefactories based on this type, if the matching delegate object exists, It is used for modelvalidator creation, otherwise the corresponding Modelvalidator object is created using the default factory represented by the field defaultvalidatablefactory.

Default Modelvalidator creation mechanism

When the Dataannotationsmodelvalidatorprovider type is loaded, the above four fields are initialized at the time of the static constructor call. As you can see from the code snippet below, for the general Validationattribute, The corresponding Modelvalidator is a Dataannotationsmodelvalidator object (defaultattributefactory field), and for Rangeattribute, The four validation features of Regularexpressionattribute, RequiredAttribute, and Stringlengthattribute, their corresponding matching modelvalidator will be created. For verifiable objects, a Validatableobjectadapter object is also included in the Modelvalidator list provided by default.

1:public class Dataannotationsmodelvalidatorprovider:associatedvalidatorprovider
2: {
3://other Members
4:static Dataannotationsmodelvalidatorprovider ()
5: {
6://1, Defaultattributefactory
7:defaultattributefactory = (metadata, context, attribute) => new Dataannotationsmodelvalidator (metadata, cont ext, attribute);
8:
9://2, Attributefactories
10:dictionary<type, dataannotationsmodelvalidationfactory> Dictionary = new Dictionary<type, DataAnnotat Ionsmodelvalidationfactory> ();
11:dictionary. Add (typeof (Rangeattribute), (metadata, context, attributes) => new Rangeattributeadapter (metadata, context, Rangeattribute));
12:dictionary. Add (typeof (Regularexpressionattribute), (metadata, context, attribute) => new Regularexpressionattributeadapter ( metadata, context, (Regularexpressionattribute));
13:dictionary. Add (typeof (RequiredAttribute), (metadata, context, attributes) => new Requiredattributeadapter (metadata, context, RequiredAttribute));
14:dictionary. Add (typeof (Stringlengthattribute), (metadata, context, attribute) => new Stringlengthattributeadapter (metadata, Context, (Stringlengthattribute) attribute));
15:attributefactories = dictionary;
16:
17://3, Defaultvalidatablefactory
18:defaultvalidatablefactory = (metadata, context) => new Validatableobjectadapter (metadata, context);
19:
20://4, Validatablefactories
21:validatablefactories = new Dictionary<type, dataannotationsvalidatableobjectadapterfactory> ();
22:}
23:}

Customizing the way Modelvalidator is created

Dataannotationsmodelvalidatorprovider four static fields based on delegates embody the modelvalidator delivery mechanism that they use. Since they are all internal fields, we cannot manipulate them directly, but a series of static methods, shown below, are defined in Dataannotationsmodelvalidatorprovider. Allows us to define the default Modelvalidator according to the specific needs.

1:public class Dataannotationsmodelvalidatorprovider:associatedvalidatorprovider
2: {
3://other Members
4:public static void Registeradapter (type attributetype, type AdapterType);
5:public static void Registeradapterfactory (Type attributetype, Dataannotationsmodelvalidationfactory Factory);
6:public static void Registerdefaultadapter (Type adaptertype);
7:public static void Registerdefaultadapterfactory (Dataannotationsmodelvalidationfactory factory);
8:
9:public static void Registerdefaultvalidatableobjectadapter (Type adaptertype);
10:public static void Registerdefaultvalidatableobjectadapterfactory ( Dataannotationsvalidatableobjectadapterfactory factory);
11:public static void Registervalidatableobjectadapter (type modeltype, type AdapterType);
12:public static void Registervalidatableobjectadapterfactory (Type modeltype, Dataannotationsvalidatableobjectadapterfactory factory);
13:}

For the above 8 static methods, except Registerdefaultadapter and Registervalidatableobjectadapter, the rest are well understood. Registerdefaultadapter is used to register a default modelvalidator type for validation attributes that must have a list of parameter types for Modelmetadata, The constructor of ControllerContext and attribute. If a matching Dataannotationsmodelvalidationfactory delegate object is found based on the type of the validation attribute, the corresponding parameter is passed in to the constructor and eventually a Modelvalidator object of our registration is created.

Registervalidatableobjectadapter is similar to registerdefaultadapter for registering a default modelvalidator for a verifiable object type. The type must have a constructor with a list of argument types Modelmetadata and Controllercontex. If a matching Dataannotationsvalidatableobjectadapterfactory delegate object is found based on the type of the validation attribute, the corresponding parameter is passed in to the constructor and eventually a Modelvalidator object of our registration is created.

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.