ASP. NET model meta-data learning Three

Source: Internet
Author: User
Tags list of attributes

5.Model metadata Availability mechanism

The Modelmetadata object that represents the model metadata is ultimately provided by a Modelmetadataprovider object, and the supply mechanism of the model metadata is modelmetadataprovider-centric. Let's start by looking at the Modelmetadata constructor:

 Public Modelmetadata (Modelmetadataprovider provider, Type Containertype, func<object> Modelaccessor,type Modeltype,string PropertyName);

created from this attribute object.

         Public class Dataannotationsmodelmetadata:modelmetadata        {            public dataannotationsmodelmetadata (Dataannotationsmodelmetadataprovider provider,type containertype,func<Object> Modelaccessor,type modeltype,string  PropertyName, Displaycolumnattribute displaycolumnattribute);             protected Override string Getsimpledisplaytext ();        }

Dataannotationsmodelmetadata object, if the attribute object's DisplayColumn property returns a property name that is defined in the current data class, the value of the corresponding property will be the return value of the Getsimpledisplaytext.

6.CachedDataAnnotationsModelMetadata

the model metadata provided by the ASP. NET MVC is actually an object of the Cacheddataannotationsmodelmetadata class. This class is not the subclass of the Dataannotationsmodelmetadata class that was learned above,cachedmodelmetadata<tprototypecache> is its base class. The cachedmodelmetadata<tprototypecache> base class uses the "prototype" design pattern, where one of its constructors designates another as a prototype cachedmodelmetadata< The Tprototypecache> object, and some of its own properties are derived from the same name property as the prototype object. This base class overrides all attributes that describe the model metadata, and defines the corresponding protected virtual methods to derive their values and returns the property values corresponding to the base class, Cacheddataannotationsmodelmetadata overrides all Computexxx methods ( The corresponding protected virtual method, which is used to calculate the properties of the model metadata). As the type that caches the model metadata information, Cacheddataannotationsmetadataattributes contains all the data annotation attributes used to define the model metadata. And Cacheddataannotationsmodelmetadata is the encapsulation of Cacheddataannotationsmetadataattributes, the computexxx () mentioned above The return value of the method is derived from the previously encapsulated object.

7.ModelMetadataProvider

This is an abstract class, as shown below, and the Getmetadataforproperties method is used to get the model metadata collection that describes all the properties of the specified container type; The Getmetadataforpropety method is used to obtain a description of a

         Public classModelmetadataprovider {protectedModelmetadataprovider ();  Public AbstractIenumerable<modelmetadata> Getmetadataforproperties (Objectcontainer, Type containertype);  Public AbstractModelmetadata Getmetadataforproperty (func<Object> Modelaccessor, Type containertype,stringPropertyName);  Public AbstractModelmetadata Getmetadatafortype (func<Object>modelaccessor, Type modeltype); }

The model metadata for the Body property. The Getmetadatafortype method directly returns the model metadata for the container object and type. Next look at the 3 available classes:

(1) Associatedmetadataprovider: Provide Dataannotationsmodelmetadata Dataannotationsmodelmetadataprovider and Cacheddataannotationsmodelmetadata Cacheddataannotati Onsmodelmetadata are all Associatedmetadataprovider subclasses. This class parses the model metadata based on the attributes applied to the data type or its data members, and creates the Modelmetadata object as the model metadata based on the list of all attributes associated with it.

(2) Dataannotationsmodelmetadataprovider: This class in the Createmetadata method of implementation, It first extracts the Displaycolumnattribute attribute from the provided list of attributes and invokes the constructor to create a Dataannotationsmodelmetadata object, and then initializes it with the provided data annotation attribute.

(3) Cachedassociatedmetadataprovider<tmodelmetadata>: The Createmetadata method overridden in this class generates a key based on the data type and property name (for attribute-based model metadata) and obtains the pre-created Modelmetadata object from the cache represented by the ObjectCache object. If the cached Modelmetadata object exists, the abstract method Createmetadataformprototype is called and a new Modelmetadata object is created, and if not, a Modelmetadata object is created as a prototype. The object is cached after it is returned.

Summary: Modelmetadataproviders, Dataannotationsmodelmetadataprovider, Cacheddataannotationsmodelmetadataprovider get Modelmetadataprovider;

Cacheddataannotationsmodelmetadataprovider Dataannotationsmodelmetadataprovider can get modelmetadata.

ASP. NET model meta-data learning Three

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.