ASP. net mvc Model metadata (III)

Source: Internet
Author: User

 

ASP. net mvc Model metadata (III) Preface

In the previous article, we roughly explained the generation process of Model metadata, and did not describe the Model metadata itself and the detailed generation process. This article will explain the detailed generation process, I will also explain the structure of the Model metadata. After reading this article, you will have a clear impression on the structure of the Model metadata.

 

Model Metadata
  • What is Model?Metadata?
  • Generate ModelMetadata process [1]
  • Generate ModelMetadata process [2]
  • ModelMetaDataDefinition and Explanation
  • ModelMetadata application (common feature application)-1
  • ModelMetadata application (custom view template)-2
  • ModelMetadata application (IMetadataAwareInterface Usage)-3

 

Process of generating Model metadata [2]

Do you still remember the last graph in Chapter 1 of the Model metadata series?

Figure 1

 

No error. The MVC framework generates a Model metadata ModelMetadata (actually the DataAnnotationsModelMetadata type is inherited from the ModelMetadata type, in the next section, we will introduce the ModelMetadata type for a more intuitive and convenient explanation ). Let's take a look at the definition of the ModelMetadata type:

Code 1-1

Public class ModelMetadata {public ModelMetadata (ModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName); // summary: // obtain a set of model metadata objects that describe the attributes of the model. //// Return result: // a set of model metadata objects used to describe model attributes. Public virtual IEnumerable <ModelMetadata> Properties {get;} // Summary: // gets the model type. //// Return result: // model type. Public Type ModelType {get;} protected ModelMetadataProvider Provider {get; set ;}...... }

Only one constructor and three attributes are left. The first parameter type in the constructor will be familiar to you, that is, the Model metadata generation program mentioned in the previous article is used to generate the Model metadata (ModelMetadata type). In this way, the reference of the ModelMetadataProvider type is set to the internal Model metadata, that is, the Provider attribute, Which is purposeful. Later we will talk about it. There is also a Properties attribute in its definition. The type is a set of ModelMetadata types, this is the key of the ModelMetadata type. The Properties attribute represents the set of attribute metadata in the description type of the current ModelMetadata.

Figure 2

Use the Customer type described in the previous article to describe it. The MVC framework corresponding to the Customer type will also generate the corresponding ModelMetadata type structure. Here, the author mentions it, for the complex type that the Address attribute type is Address type, the MVC framework continues to generate the data as if it were a Customer type.

So how is this structure generated? Needless to say, it relies on the Provider attribute, that is, the reference of the ModelMetadataProvider type, to generate the structure, as shown in 3:

Figure 3

The GetMetadataForProperties () method of the AssociatedMetadataProvider type is called based on the Model attribute and ModelType attribute of the current Model metadata ModelMetadata type (corresponding to the object of the Customer type, here, the Model attribute of the ModelMetadata type represents the value of the object corresponding to the current Model metadata. It is also used to determine whether it is a complex type, as described above.

In the GetMetadataForProperties () method, all attributes of the current object of the Customer type are obtained based on the GetProperties () method of the custom type description type and encapsulated into a set of attribute description types.

Then, the GetMetadataForProperty () method of the AssociatedMetadataProvider type is called Based on the obtained property description type set and the individual property description type in the traversal, the first parameter modelAccessor is Null by default, and the second parameter containerType indicates the current Customer type, the third parameter is that the attribute description type contains all the information of the attribute type. Some may ask what these attributes do, because the second parameter containerType will be mentioned later.

In the GetMetadataForProperty () method of the AssociatedMetadataProvider type, all the description information (that is, those feature classes) of the current attribute is obtained based on the parameter of the PropertyDescriptor type ), for example, if the current PropertyDescriptor type is CustomerID of the structured Customer type, the AttrbuteList type in Figure 3 contains all the feature classes attached to this property. The subsequent generation process is the same as described in the previous article. The CreateMetadata () of the AssociatedMetadataProvider type is still called, but the method in the AssociatedMetadataProvider type is abstract, it is actually done by the CreateMetadata () method in its implementation class DataAnnotationsModelMetadataProvider.

Here you may find that the operations in the yellow box in Figure 3 belong to the operations in traversal, that is, only one ModelMetadata instance is generated each time and then merged and returned together.

In addition, in Figure 3, each called function in the yellow box has a Type containerType parameter, which is the Customer Type mentioned above, in addition, the ContainerType attribute is assigned to the generated ModelMetadata type instance, indicating that the newly generated ModelMetadata type instance is called A. the information described in A is all the information of the CustomerID attribute in the Customer type, the ContainerType attribute in A indicates the type of the merid attribute.

Here, we also want to say that when the system generates a default view model of the Customer type, for example, the MVC Framework will generate only one ModelMetadata instance if it is M, M itself does not itself detect whether it is a complex type, so M will not call the provider to generate it, instead, M is used externally to call the function in M to detect whether M is a complex type and then generate it, if this M is used in the MVC framework, it may call methods that detect itself to check whether it is of a complex type. The obvious Customer type is of a complex type, at this time, M will generate the attribute in the description type in sequence as described in this article, but it is only limited to this layer. Some may ask that the Address attribute is also a complex type in the Customer type, yes, but M will only generate the ModelMetadata instance of the Address attribute itself, instead of generating the internal Address attribute.

Now let's look at Figure 2 again. Is it a bit clear.

(Which of the following experts knows where the ModelMetadata instance itself is detected in the MVC framework? Tell me what you know. I believe there is something for sure)

This article ends. Next, I will introduce the CreateMetadata () method in the DataAnnotationsModelMetadataProvider type in detail. I will go through this method and explain in detail the ModelMetadata object type.

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.