Four Verification programming methods under ASP. net mvc [continued], asp. netmvc
In ASP. net mvc, which describes ASP. net mvc supports four programming methods for server verification ("manual verification", "tagging ValidationAttribute features", and "enabling data types to implement IValidatableObject or IDataErrorInfo. how does the net mvc framework provide support for these four different programming methods? Next, let's talk about the story behind it.
I. ModelValidator and ModelValidatorProvider
Although the Model binding method varies with the verified data types, ASP. net mvc always uses an object named ModelValidator to verify the bound data objects. All ModelValidator types inherit from the abstract class ModelValidator defined as follows. Its GetClientValidationRules method returns a set of ModelClientValidationRule elements, while ModelClientValidationRule encapsulates client verification rules. We will introduce it in the client Verification Section.
Public abstract class ModelValidator {// other members public virtual IEnumerable <ModelClientValidationRule> constraint (); public abstract IEnumerable <ModelValidationResult> Validate (object container); public virtual bool IsRequired {get ;}}
The verification of target data is completed by calling the Validate method. The input parameter container of this method indicates the verified object. It is precisely because the verified object is always a complex type, which is also called a "container" object with several data members. Therefore, the corresponding parameter is named container. The Validate method indicates that the return value of the verification result is not a simple boolean value, but an element type is a set of ModelValidationResult objects with the following definitions.
public class ModelValidationResult { public string MemberName { get; set; } public string Message { get; set; } }
ModelValidationResult has two string-type attributes: MemberName and Message. The former represents the name of the verified data member, and the latter represents an error Message. Generally, if the ModelValidationResult object comes from the verification of the container object, its MemberName attribute is a null string. For the verification of an attribute of the container object, the attribute name will be used as the MemberName attribute of the returned ModelValidationResult object.
The ModelValidationResult set is returned only when verification fails. If the verified data object meets all verification rules, the Validate method returns Null or an empty ModelValidationResult set. It is worth mentioning that we sometimes use the static read-only field Success of ValidationResult to indicate the result of successful verification. In fact, the value of this field is Null.
Public class ValidationResult {// other members public static readonly ValidationResult Success ;}
ModelValidator has a Boolean read-only attribute IsRequired indicating whether the ModelValidator verifies the target data (that is, the verified data member must have a specific value ), by default, False is returned for this attribute. You can define an attribute as a "required" data member by applying the RequiredAttribute feature.
We know ASP. net mvc mostly uses the Provider mode to provide corresponding components. For example, ModelMetadata describing Model metadata is provided through the corresponding ModelMetadataProvider, and ModelBinder bound to Model can be provided through the corresponding ModelBinderProvider, the ModelValidator used for Model verification is no exception. Its Provider is ModelValidatorProvider, and its type inherits from the abstract class ModelValidator Provider defined below.
public abstract class ModelValidatorProvider { public abstract IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context); }
As shown in the code snippet above, the GetValidators method has two parameters: one is the ModelMetadata object used to describe the verified type or attribute Model metadata, and the other is the current ControllerContext. This method returns a set of elements of the ModelValidator type.
ASP. net mvc registers ModelValidatorProvider through static ModelValidatorProviders. As shown in the following code snippet, ModelValidatorProviders has a static read-only property Providers. The corresponding type is ModelValidatorProviderCollection, which indicates the global ModelValidatorProvider set based on the entire Web application scope.
public static class ModelValidatorProviders { public static ModelValidatorProviderCollection Providers { get; } } public class ModelValidatorProviderCollection : Collection<ModelValidatorProvider> { public ModelValidatorProviderCollection(); public ModelValidatorProviderCollection(IList<ModelValidatorProvider> list); public IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context); }
It is worth mentioning that the ModelMetadata type used to describe the Model metadata has a method such as the next GetValidators. The ModelValidator list returned by the ModelValidatorProviders method is created using the ModelValidatorProvider registered to the ModelValidatorProviders static attribute Providers.
Public class ModelMetadata {// other members public virtual IEnumerable <ModelValidator> GetValidators (ControllerContext context );}
The UML shown in the right figure lists the three core types that constitute the Model verification system. The specific Model verification work is always completed through a specific ModelValidator. As the ModelValidatorProvider of the ModelValidator provider, the ModelValidatorProvider is registered on the static type ModelValidatorProviders.
2. DataAnnotationsModelValidator
In ASP. net mvc describes three different "Automated verification" programming methods, ASP. net mvc uses different ModelValidator internally to verify the bound parameters. A specific ModelValidator is usually provided by the corresponding ModelValidatorProvider. The following describes the native ModelValidator provided by ASP. net mvc and the corresponding ModelValidatorProvider in detail.
For the three verification programming methods mentioned above, the first one (using the ValidationAttribute feature of the application on the data type or its data members to define the corresponding verification rules) is the most common. The declarative verification solution based on the ValidationAttribute feature is finally completed through DataAnnotationsModelValidator. A DataAnnotationsModelValidator object is actually an encapsulation of the ValidationAttribute feature, which can be seen from the following definitions.
public class DataAnnotationsModelValidator : ModelValidator { public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute); public override IEnumerable<ModelClientValidationRule> GetClientValidationRules(); public override IEnumerable<ModelValidationResult> Validate(object container); protected internal ValidationAttribute Attribute { get; } protected internal string ErrorMessage { get; } public override bool IsRequired { get; } }
DataAnnotationsModelValidator is provided by DataAnnotationsModelValidatorProvider. For more information about ValidationAttribute, DataAnnotationsModelValidator, and DataAnnotationsModelValidatorProvider, see the previous three articles.
Model Verification Based on Annotation Feature of ASP. net mvc: ValidationAttribute
Model Verification of ASP. net mvc Based on Annotation Feature: DataAnnotationsModelValidator
Model Verification of ASP. net mvc Based on Annotation Feature: DataAnnotationsModelValidatorProvider
Iii. ValidatableObjectAdapter
If the verified data type implements the IValidatable interface, ASP. net mvc automatically calls the implemented Validate Method for verification. The created ModelValidator is a ValidatableObjectAdapter object. The ValidatableObjectAdapter is defined as follows. The implementation logic of the Validate method is simple: it directly calls the Validate method of the verified object and converts the returned ValidationResult object to the ModelValidationResult type.
public class ValidatableObjectAdapter : ModelValidator { public ValidatableObjectAdapter(ModelMetadata metadata, ControllerContext context); public override IEnumerable<ModelValidationResult> Validate(object container); }
Although ValidatableObjectAdapter inherits from ModelValidator, ASP. net mvc does not seem to regard it as a true ModelValidator, but as an "Adapter )". ASP. net mvc does not define a separate ModelValidatorProvider for ValidatableObjectAdapter. Its provider is actually the DataAnnotationsModelValidatorProvider mentioned above.
4. DataErrorInfoModelValidator
If we implement the IDataErrorInfo interface for the data type, we can use the implemented Error attribute and index to provide verification Error information for ourselves and its data members. For such data types, ASP. net mvc will eventually create a DataErrorInfoModelValidator object for verification. DataErrorInfoClassModelValidator and DataErrorInfoPropertyModelValidator are two specific DataErrorInfoModelValidator objects.
DataErrorInfoClassModelValidator and DataErrorInfoPropertyModelValidator are two internal types. The former implements verification for the container object itself, so it only needs to extract the Error message from the implemented Error attribute and convert it to the returned ModelValidationResult object. The latter specifically verifies an attribute of the container object, in the implemented Validate method, it uses the attribute name to extract the corresponding error message from the implemented index and convert it into the returned ModelValidationResult object.
internal sealed class DataErrorInfoClassModelValidator : ModelValidator { public DataErrorInfoClassModelValidator(ModelMetadata metadata, ControllerContext controllerContext); public override IEnumerable<ModelValidationResult> Validate(object container); } internal sealed class DataErrorInfoPropertyModelValidator : ModelValidator { public DataErrorInfoPropertyModelValidator(ModelMetadata metadata, ControllerContext controllerContext); public override IEnumerable<ModelValidationResult> Validate(object container); }
ASP. net mvc finally uses DataErrorInfoModelValidatorProvider with the following definitions to provide the two types of DataErrorInfoModelValidator. For its implemented GetValidators method, if the type of the verified object implements the IDataErrorInfo interface, it will create a DataErrorInfoClassModelValidator object and add it to the returned ModelValidator list. If a property value of the container type is verified and the container type implements the IDataErrorInfo interface, it creates a DataErrorInfoPropertyModelValidator object and adds it to the returned ModelValidator list.
public class DataErrorInfoModelValidatorProvider : ModelValidatorProvider { public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context); }
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message and share it with us!