In the previous article, we introduce the modelvalidator ("asp.net mvc modelvalidator as the core model validation system: Modelvalidator"), which is really used to implement model verification. As well as for providing modelvalidator modelvalidatorprovider ("asp.net mvc to modelvalidator as the core model validation system: Modelvalidatorprovider"), Then for ASP.NET MVC model verification system, the final is how to register the Modelvalidatorprovider, and how to use them to create a corresponding modelvalidator to implement model verification? This is the focus of this article.
First, Modelvalidatorproviders
We register the Modelvalidatorprovider through the static type Modelvalidatorproviders. As the following code fragment shows, Modelvalidatorproviders has a static read-only property providers, which is of type modelvalidatorprovidercollection, Represents a registered Modelvalidatorprovider list based on the entire Web application scope.
1:public Static Class Modelvalidatorproviders
2: {
3: Public static modelvalidatorprovidercollection Providers {get;}
4:}
5:
6:public class Modelvalidatorprovidercollection:collection<modelvalidatorprovider>
7: {
8: Public modelvalidatorprovidercollection ();
9: Public modelvalidatorprovidercollection (ilist<modelvalidatorprovider> list);
ienumerable<modelvalidator> getvalidators (modelmetadata metadata, controllercontext context);
11:}
It is worth mentioning that Modelvalidatorprovidercollection defines a getvalidators method for returning a modelvalidator set created from each Modelvalidatorprovider in the collection. In this method, the specified model metadata and controller context are passed into the Getvalidators method of each Modelvalidatorprovider object, Each Modelvalidator object that is obtained will be the element of the final returned Modelvalidator collection.
By default, the registered Modelvalidatorprovider list will contain three objects, by means of the modelvalidatorproviders providers. Corresponds to the three types of Modelvalidatorprovider we described earlier, namely Dataannotationsmodelvalidatorprovider, Clientdatatypemodelvalidatorprovider and Dataerrorinfopropertymodelvalidator.
If we need to add a custom modelvalidatorprovider, we can add the corresponding object directly to the Modelvalidatorproviders providers list. If you need to use a custom modelvalidatorprovider to replace the existing modelvalidatorprovider, for example, we created an extended Dataannotationsmodelvalidatorprovider , you also need to remove the existing modelvalidatorprovider from the list.
The Modelvalidator delivery mechanism implemented in Modelvalidatorprovider is based on the model metadata and the controller context. The Modelmetadata type, which is actually used to describe the model metadata, also defines a getvalidators method for all Modelvalidator objects that are based on the specified controller context. As shown in the following code snippet, this method directly invokes the method of the same name as the Modelvalidatorprovidercollection object that is expressed through the Modelvalidatorproviders providers property.
1:public Abstract class Modelvalidator
2: {
3: //other Members
4: Public virtual ienumerable<modelvalidator> getvalidators (ControllerContext context)
5: {
6: Return ModelValidatorProviders.Providers.GetValidators (this, context);
7: }
8:}
Ii. Modelvalidator, Modelvalidatorprovider and Modelvalidatorproviders
Above we introduce the Modelvalidator for model verification, which is used to provide Modelvalidator Modelvalidatorprovider, As well as the modelvalidatorproviders for registering Modelvalidatorprovider, the entire modelvalidator delivery mechanism is centered on these three types of components, and the UML shown in the following illustration shows the relationship between them.