Four ways to verify programming under ASP-NET MVC [sequel]

Source: Internet
Author: User

In the article "Four Ways to verify programming under ASP.", we describe the programming of the four server-side validations supported by ASP. ("Manual Validation", "Labeling Validationattribute properties", " Let the data type implement Ivalidatableobject or IDataErrorInfo "), how do you provide support for these four different programming methods within the ASP. Next we'll talk about the story behind it.

first, modelvalidator and Modelvalidatorprovider

Although the model binding differs in the way it is verified by the data type, ASP. NET MVC always uses an object named Modelvalidator to validate the bound data object. All modelvalidator types inherit from the abstract class Modelvalidator, which has the following definition. Its Getclientvalidationrules method returns a collection of element types Modelclientvalidationrule, and Modelclientvalidationrule is the encapsulation of the client validation rules. We'll cover it in detail in the Client validation section.

   1:public Abstract class Modelvalidator
   2: {
   3:     //other Members
   4: Public     virtual ienumerable<modelclientvalidationrule> getclientvalidationrules ();
   5: Public     abstract ienumerable<modelvalidationresult> Validate (Object container);
   
   7: Public     virtual bool isrequired {get;}
   8:}

Validation of the target data is done by calling the Validate method, where the input parameters of the method container represent the object being validated. It is because the object that is validated is always a complex type, which is called a "container" object with several data members, so the corresponding parameter is named container. The Validate method indicates that the return value of the validation result is not a simple Boolean value, but an element type is a collection of Modelvalidationresult objects with the following definition.

   1:public class Modelvalidationresult
   2: {  
   3: Public     string MemberName {get; set;}
   4: Public     string Message {get; set;}
   5:}

Modelvalidationresult has two string-type properties, MemberName, and message, which represent the name of the validated data member, which represents an error message. In general, if the Modelvalidationresult object originates from validation against the container object itself, its MemberName property is an empty string. For validation of a property on a container object, the property name is used as the MemberName property of the returned Modelvalidationresult object.

The Modelvalidationresult collection is returned only if the validation fails. If the validated data object conforms to all validation rules, the Validate method returns null or an empty Modelvalidationresult collection directly. It is worth mentioning that we sometimes use Validationresult's static read-only field success to indicate the result of a successful validation, in fact the value of this field is null.

   1:public class Validationresult
   2: {
   3:     //other Members
   4: Public     static readonly Validationresult Success;
   5:}

Modelvalidator has a read-only property of a Boolean type isrequired indicates whether the Modelvalidator has "mandatory" validation of the target data (that is, the validated data member must have a specific value), which returns false by default. We can define a property as a "required" data member by applying the RequiredAttribute attribute.

We know that ASP. NET MVC mostly uses the provider model to provide the corresponding components, such as modelmetadata that describe the model metadata are provided by corresponding Modelmetadataprovider. The modelbinder that implements the model binding can be provided by the corresponding Modelbinderprovider, and the modelvalidator used to implement the model validation is no exception. Its corresponding provider is Modelvalidatorprovider, and the corresponding type inherits from the abstract class Modelvalidator Provider, which has the following definition.

   1:public Abstract class Modelvalidatorprovider
   2: {
   3: Public     abstract ienumerable<modelvalidator> getvalidators (modelmetadata metadata, ControllerContext context);
   4:}

As shown in the code snippet above, the Getvalidators method has two parameters, one is the Modelmetadata object that describes the validated type or the property's model metadata, and the other is the current controllercontext. The method returns a collection of element type Modelvalidator.

ASP. NET MVC registers the Modelvalidatorprovider used with the static type Modelvalidatorproviders. As shown in the following code fragment, Modelvalidatorproviders has a static read-only property providers, corresponding to the type modelvalidatorprovidercollection, It represents a global Modelvalidatorprovider collection based on the entire Web application scope.

   1:public Static Class Modelvalidatorproviders
   2: {   
   3: Public     static modelvalidatorprovidercollection Providers {get;}
   4:}
   
   6:public class Modelvalidatorprovidercollection:collection<modelvalidatorprovider>
   7: {   
   8: Public     modelvalidatorprovidercollection ();
   9: Public     modelvalidatorprovidercollection (ilist<modelvalidatorprovider> list);
  Ten: Public     ienumerable<modelvalidator> getvalidators (modelmetadata metadata, controllercontext context);   
  11:}

It is worth mentioning that the Modelmetadata type used to describe the model metadata has the following Getvalidators method, The list of Modelvalidator it returns is created using Modelvalidatorprovider registered to the Modelvalidatorproviders static property providers.

   1:public class Modelmetadata
   2: {
   3:     //other Members
   4: Public     virtual ienumerable<modelvalidator> getvalidators (controllercontext context);
   5:}
As shown in the diagram on the right, the UML lists the three core types that make up the model validation system. The specific model verification work is always done by a specific modelvalidator, Modelvalidatorprovider registered as the Modelvalidator provider is above the static type Modelvalidatorproviders.Second, Dataannotationsmodelvalidator

We have covered three different types of "automated validation" programming in four verification programs under ASP. ASP. NET MVC internally uses different modelvalidator to validate the bound parameters. A specific modelvalidator usually has a corresponding modelvalidatorprovider to provide, and the next content will be for the ASP. The native modelvalidator and corresponding modelvalidatorprovider provided by MVC are described in detail.

The first of the three verification programming methods mentioned above is most commonly used to define the corresponding validation rules by applying the Validationattribute attribute on the data type or its data members. This declarative validation solution based on the Validationattribute feature is ultimately done through Dataannotationsmodelvalidator. A Dataannotationsmodelvalidator object is actually a encapsulation of a validationattribute attribute, which can be seen from the definition shown below.

   1:public class Dataannotationsmodelvalidator:modelvalidator
   2: {   
   3: Public     dataannotationsmodelvalidator (modelmetadata metadata, ControllerContext context, Validationattribute attribute);
   4: Public     override Ienumerable<modelclientvalidationrule> Getclientvalidationrules ();
   
   6: Public     override ienumerable<modelvalidationresult> Validate (Object container);
   
   8:     protected internal Validationattribute     Attribute {get;}
   9:     protected internal string                  errormessage {get;}
  Ten: Public     override bool                       isrequired {get;}
  11:}

Dataannotationsmodelvalidator's provider is Dataannotationsmodelvalidatorprovider, about Validationattribute, The details of Dataannotationsmodelvalidator and Dataannotationsmodelvalidatorprovider can be found in three previous articles.

ASP. NET MVC model validation based on labeling features: Validationattribute
ASP. NET MVC model validation based on labeling features: Dataannotationsmodelvalidator
ASP. NET MVC model validation based on labeling features: Dataannotationsmodelvalidatorprovider

Third, Validatableobjectadapter

If the validated data type implements the Ivalidatable interface, ASP. NET MVC will automatically invoke the implemented validate method to validate it. The modelvalidator created at this point is a Validatableobjectadapter object. Validatableobjectadapter is defined as follows, the implementation logic of its Validate method is simple: It calls directly the Validate method of the object being validated, and converts the returned Validationresult object to the Modelvalidationresult type.

   1:public class Validatableobjectadapter:modelvalidator
   2: {
   3: Public     validatableobjectadapter (modelmetadata metadata, controllercontext context);
   4: Public     override ienumerable<modelvalidationresult> Validate (Object container);
   5:}

Although Validatableobjectadapter inherits from Modelvalidator, ASP. NET MVC does not seem to see it as a real modelvalidator, but rather as an "adapter (Adapter)". ASP. NET MVC also does not define a separate modelvalidatorprovider for Validatableobjectadapter, Its provider is actually the Dataannotationsmodelvalidatorprovider mentioned above.

Iv. Dataerrorinfomodelvalidator

If we let the data type implement the IDataErrorInfo interface, we can use the implemented Error property and index to provide validation error information for itself and the owning data member. For such data types, ASP. NET MVC will eventually create a Dataerrorinfomodelvalidator object to validate it. Dataerrorinfoclassmodelvalidator and Dataerrorinfopropertymodelvalidator are two specific dataerrorinfomodelvalidator.

Dataerrorinfoclassmodelvalidator and Dataerrorinfopropertymodelvalidator are two internal types. The former enforces validation on the container object itself, so it only needs to extract the error message from the implemented Error property and convert it to the returned Modelvalidationresult object. The latter specifically validates a property of a container object that, in the implemented validate method, extracts the corresponding error message from the implemented index using the property name and converts it to the returned Modelvalidationresult object.

   1:internal Sealed class Dataerrorinfoclassmodelvalidator:modelvalidator
   2: {
   3: Public     dataerrorinfoclassmodelvalidator (modelmetadata metadata, ControllerContext controllercontext);
   4: Public     override ienumerable<modelvalidationresult> Validate (Object container);
   5:}
   
   7:internal Sealed class Dataerrorinfopropertymodelvalidator:modelvalidator
   8: {
   9: Public     dataerrorinfopropertymodelvalidator (modelmetadata metadata, ControllerContext controllercontext);
  : Public     override ienumerable<modelvalidationresult> Validate (Object container);
  11:}

ASP. NET MVC eventually uses the Dataerrorinfomodelvalidatorprovider defined below to provide both types of dataerrorinfomodelvalidator. For the Getvalidators method of its implementation, if the type of the object being validated implements the IDataErrorInfo interface, It creates a Dataerrorinfoclassmodelvalidator object and adds it to the returned Modelvalidator list. If a property value for the container type is validated and the container type implements the IDataErrorInfo interface, it creates a Dataerrorinfopropertymodelvalidator object and adds it to the returned Modelvalidator list.

   1:public class Dataerrorinfomodelvalidatorprovider:modelvalidatorprovider
   2: {
   3: Public     override ienumerable<modelvalidator> getvalidators (Modelmetadata metadata, ControllerContext context);
   4:}

Four ways to verify programming under ASP-NET MVC [sequel]

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.