ASP. NET MVC Model metadata (III)

Source: Internet
Author: User

ASP. NET MVC Model metadata (III) Preface

In the previous article, we explained about the model metadata generation process, and did not describe the model metadata itself and the detailed generation process, this article will explain the detailed generation process, and will be the structure of the model metadata itself a little explanation, After reading this post you will have a very clear picture of the structure of the model metadata.

Model Meta Data
    • What is model metadata?
    • The process of generating model metadata "one"
    • The process of generating model metadata "two"
    • Definition and explanation of Modelmetadata
    • Model metadata application (common feature application)-1
    • Model Metadata app (custom view template)-2
    • Model metadata application (Imetadataaware interface use)-3

The process of generating model metadata "two"

Remember the last picture in the first chapter of the model meta-data series?

Figure 1

No mistake, the MVC framework generates a model metadata modelmetadata based on the view model we define (the actual dataannotationsmodelmetadata type is inherited from the Modelmetadata type, In order to be more intuitive and convenient to explain in the following, it is also used modelmetadata type to introduce). Let's take a look at the definition of the Modelmetadata type:

Code 1-1

 Public classModelmetadata { PublicModelmetadata (Modelmetadataprovider provider, Type Containertype, func<Object> Modelaccessor, Type modeltype,stringPropertyName); //        //Summary://gets a collection of model metadata objects that describe the properties of the model. //        //return Result://A collection of model metadata objects used to describe model properties.          Public VirtualIenumerable<modelmetadata> Properties {Get; }//        //Summary://gets the type of the model. //        //return Result://the type of the model.          PublicType Modeltype {Get; } protectedModelmetadataprovider Provider {Get;Set; } ...}

Only a constructor and three attributes are left, and in the next section, the first parameter type in the constructor must be familiar to you, which is the model metadata generator that was used to generate the model metadata (Modelmetadata type), This is to set the reference of the Modelmetadataprovider type to the inside of the model metadata, that is, the provider property, which is purposeful and then comes up with a property attribute in its definition, Type everyone sees is a collection of modelmetadata types, which is where the Modelmetadata type is key, and the Properties property represents the collection of attribute metadata in the currently described type of modelmetadata.

Figure 2

Using the customer type described in the previous article, the structure of the MVC framework that corresponds to the customer type will also generate the corresponding Modelmetadata type structure, incidentally, for the address attribute type is the address type, which is a complex type, The MVC framework continues to build down as if the customer type was generated.

So how is this structure generated? Needless to say, it relies on the provider property, which is a reference to the Modelmetadataprovider type, to generate the structure, as shown in 3:

Figure 3

The Modelmetadata type is first based on the current model metadata (the corresponding object is the customer type) The Model property in the Modeltype property to call the Associatedmetadataprovider type's Getmetadataforproperties () method as a parameter, Here's the model property of the Modelmetadata type, which represents the value of the object corresponding to the current model metadata, and is used to determine if it is a complex type, as the Modeltype property says.

The Getmetadataforproperties () method first obtains all the properties of the current object as a customer type based on the GetProperties () method of the custom type description type, and encapsulates the collection of property description types.

This collection is then traversed based on the obtained property description type collection, and the Getmetadataforproperty () method in the Associatedmetadataprovider type is called based on a single property description type in the traversal. This is to say that the first parameter modelaccessor is null by default, the second parameter containertype is the current customer type, and the third parameter is the property description type all information about the property type in the bread. Some friends will ask what these attributes do, because the second argument Containertype.

In the Getmetadataforproperty () method of the Associatedmetadataprovider type, all the descriptive information (that is, those attribute classes) on the current property is obtained according to the parameters of the PropertyDescriptor type. For example, the current PropertyDescriptor type is CustomerID in the structured customer type, and the Attrbutelist type in Figure 3 contains all the attribute classes that are attached to this property. The subsequent generation process still calls the Associatedmetadataprovider type of createmetadata () just as it was explained in the previous article, except that 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 actions in the yellow box in Figure 3 are the actions in the traversal, that is, each time a Modelmetadata type instance is generated and then finally merged together to return.

What's more, in Figure 3, each function called in the Yellow box has a type Containertype parameter, which is the customer type mentioned above, and is assigned to the Containertype property in the generated Modelmetadata type instance , indicating that the newly generated instance of Modelmetadata type, such as the information described in A,a, is all the information of the CustomerID attribute in the customer type, The Containertype property in a is the type that represents the description of the CustomerID property.

Here is to say, when the system is generated by default, such as the view model is the customer type, then the MVC framework will only generate an instance of the Modelmetadata type if it is called M, because M itself does not detect itself is not a complex type, So M is not going to call the provider down, but instead of using m in the outside to call the function in M to detect whether M is a complex type and then build it, if now the MVC framework is used in this m may call to detect its own method to check whether it is a complex type, The obvious type of customer is a complex type, and at this point m will generate the attributes in the type described in this article in turn, just this layer, and some friends may ask that the address attribute is also a complex type in the customer type, right, But M will only generate an instance of the Modelmetadata type of the address property itself, rather than generating the interior of the Address property.

Now let's see again Figure 2, is not a little clear feeling.

(Does any great God know where to invoke the self-detection of Modelmetadata type instances in the MVC framework?) Know to tell a little brother to body, looking for the bloody also didn't find, I believe it is certain)

At the end of this article, the Createmetadata () method in the Dataannotationsmodelmetadataprovider type is described in detail in the next section, from which a detailed explanation of the Modelmetadata object type is entered.


Jinyuan

Source: http://blog.csdn.net/jinyuan0829

This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.