ASP. NET MVC Model metadata (II)

Source: Internet
Author: User
Tags abstract definition

ASP. NET MVC Model metadata (II)Preface

In the previous article, to leave an impression of the model metadata, there is no excessive explanation of the model metadata, and in this article does not explain the model metadata itself, but for its generation process, so that you can clearly understand when the system framework is generated when the model metadata , for the model metadata generation length is set to two, this is its overall generation process, the next chapter is a detailed production process and will be a rough introduction to it, I hope you can read the harvest

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 "one"

As I said in the previous article, since the model metadata (model refers to the view model) is definitely related to model, when does the model work in our MVC project? In general, when a view is requested through the behavior of the controller, the parameters of the controller behavior are model, then some processing is done in the behavior method and then passed to the view. And then, according to the last one in the previous chapter,

Figure 1

The place where the model metadata is generated has been locked into the behavior method, and it is conceivable that it is not possible to generate it in the behavior method, because that is where our custom logic is. Where is that?

Presumably you have seen the previous introduction to the length of the filter, in the ASP. NET MVC filter (c) in the implementation of the behavior filter, the middle of the reference to the model binder, and to the system framework to use the custom model binder, The two parameters required to use this custom model binder are important, one is the object ControllerContextthat represents the current controller context, and the other is the key to generating the model metadata, as well as the key parameters for invoking the custom models Binder The modelbindingcontext type. Look down.

Figure 2

There is an important attribute in the modelbindingcontext type, that is, the model metadata type modelmetadata, so that we can know before our controller behavior executes, The model metadata Modelmetadata type for the model that corresponds to the controller behavior has been generated. (This section details the filter length)

And how is it generated? What types are generated by the provider that is provided by default in the system framework?

Figure 3

So let's take a look at the definition of the topmost base class Modelmetadataprovider :

Code 1-1

 Public Abstract classModelmetadataprovider {//Summary://When overridden in a derived class, initializes a new instance of the object derived from the System.Web.Mvc.ModelMetadataProvider class.         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); }

Very clear definition, three abstract methods, here we only need to look at the Getmetadatafortype () method, the other two temporarily regardless of the next article will be mentioned, because first look at the Getmetadatafortype () method? Because it is the entry that generates the modelmetadata type, the first parameter is temporarily ignored, the second argument is important, is the ParameterType property of the parameterdescriptor type, Represents the type of model (that is, the type of the controller method parameter), now let's look at 4

Figure 4

The Blue line in Figure 4 is the main flow, and the red line is the process that executes after the blue process.

As mentioned above, the entrance method is for the abstract method, that is how to achieve the specific, from Figure 4 can be seen by the implementation of the Modelmetadataprovider type associatedmetadataprovider Type to handle, you can see from Figure 4 that the first is to get a attributelist type, and theattributelist type represents the Associatedmetadataprovider type Getmetadatafortype () method parameter modeltype the set of attributes on the type, the associatedmetadataprovider type is the more important type, let's first look at its definition:

 Public Abstract classAssociatedmetadataprovider:modelmetadataprovider {protectedAssociatedmetadataprovider (); protected AbstractModelmetadata createmetadata (ienumerable<attribute> attributes, Type containertype, func<Object> Modelaccessor, Type modeltype,stringPropertyName); protected VirtualIenumerable<attribute> filterattributes (Type containertype, PropertyDescriptor PropertyDescriptor, Ienumerable<attribute>attributes);  Public OverrideIenumerable<modelmetadata> Getmetadataforproperties (Objectcontainer, Type containertype); protected VirtualModelmetadata Getmetadataforproperty (func<Object>Modelaccessor, Type containertype, PropertyDescriptor propertydescriptor);  Public OverrideModelmetadata Getmetadataforproperty (func<Object> Modelaccessor, Type containertype,stringPropertyName);  Public OverrideModelmetadata Getmetadatafortype (func<Object>modelaccessor, Type modeltype); protected Virtualicustomtypedescriptor gettypedescriptor (type type); }

There are a few ways to do this, and most methods are used to recursively generate the model metadata (the process is explained in the next article). All right, cut back to the subject. Then the above content, the origin of theattributelist type, is through Modelmetadataprovider 's GetTypeDescriptor () The method is based on the type of model (which is understood here for a moment, and then you will know that there may be an attribute type in the model) to generate a icustomtypedescriptor Type (which can be imagined as an abstract definition of an object-type metadata description object.) It's a bit of a detour, but it does mean that. And the system will have a default custom implementation to implement this interface type, we use this default implementation to obtain the model type of the attributelist type.

With the attributelist type, we can call the Createmetadata () method of the Associatedmetadataprovider type to create the model metadata object. But the definition of this createmetadata () is abstract, and the real implementation is inherited by the associatedmetadataprovider type. The Dataannotationsmodelmetadataprovider type, which we then generate to get the modelmetadata metadata Object (the real process is more cumbersome, Otherwise, it will not be used to explain the process of generation, and the model metadata object is not finished, but it continues to call the Associatedmetadataprovider The Applymetadataawareattributes () method of the type, and in this method, the system framework calls us to customize the object that implements the Imetadataaware interface type to personalize the model metadata object. Finally, the model metadata object is actually returned.

It is possible to see some friends here on the model metadata is still not clear and understanding, friends anxious I also urgent, if I share these knowledge everyone read all unintelligible then why do I do. Do not hurry to see how much is how much after reading this model metadata series of articles should have some understanding, will be in the next chapter slowly uncover its secret. Thank you for your support.

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.