ASP. net mvc Model metadata (2) Preface
In the previous article, I gave you an impression on the Model metadata and did not explain the Model metadata too much. In this article, I will not explain the Model metadata itself, instead, we can clearly understand the system framework when the Model metadata is generated. For example, we set the length of Model metadata generation to two, this article describes the overall generation process. The next article describes the detailed generation process and provides a rough introduction to it.
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 [1]
As mentioned in the previous article, since it is called Model metadata (Model refers to the view Model), it must be related to the Model, in our MVC project, when will the Model be operated? In general, when a view is requested through the Controller's behavior, and the Controller's behavior parameter is Model, some processing is done in the behavior method and then passed to the view. Then, based on the last one in the previous article,
Figure 1
The place where the Model metadata is generated has been locked to the behavior method. Imagine that it cannot be generated in the behavior method, because it is where our custom logic is. Where is that?
I think you have read the previous introduction to the length of filters in ASP. net mvc filter (3) when explaining the execution process of the behavior filter, we mentioned the model binder in the middle and talked about the custom model binder used by the system framework, the two parameters required to use this custom model binder are very important. One is the object that represents the context of the current controller.ControllerContextThe other is the key to generating Model metadata and the key parameter to call the custom Model binder.ModelBindingContextType. Check
Figure 2
InModelBindingContextType has an important attribute, that is, the Model metadata type.ModelMetadataSo that we can know the Model metadata of the Model corresponding to the Controller behavior before our controller behavior is executed.ModelMetadataType has been generated. (For details about this part, see the filter section)
How is it generated? It is generated by the provider provided by default in the system framework. What types are there?
Figure 3
Let's take a look at the top-level base class.ModelMetadataProviderDefinition:
Code 1-1
Public abstract class ModelMetadataProvider {// abstract: // when rewriting in a derived class, initialize a new instance of the object derived from the System. Web. Mvc. ModelMetadataProvider class. Protected metadata (); public abstract IEnumerable <ModelMetadata> GetMetadataForProperties (object container, Type containerType); public abstract ModelMetadata GetMetadataForProperty (Func <object> modelAccessor, Type containerType, string propertyName ); public abstract ModelMetadata GetMetadataForType (Func <object> modelAccessor, Type modelType );}
The definition is very clear. Here we only need to look at the GetMetadataForType () method first. The other two will not care about it in the next article, because what about the GetMetadataForType () method first? Because it is generatedModelMetadataType entry. The first parameter is temporarily ignored. The second parameter is very important.ParameterDescriptorThe ParameterType attribute of the Type indicates the Type of the Model (that is, the Type of the Controller method parameter). Now let's look at 4
Figure 4
In Figure 4, the blue line is the main process, and the red line is the process executed after the blue line is processed.
As mentioned above, the entry method is an abstract method. How is that implemented? as shown in figure 4, it is implementedModelMetadataProviderTypeAssociatedMetadataProviderType, as shown in figure 4, the first step is to obtainAttributeListType,AttributeListType indicatesAssociatedMetadataProviderThe feature set of the modelType parameter of the GetMetadataForType () method.AssociatedMetadataProviderType is an important type. Let's take a look at its definition:
public abstract class AssociatedMetadataProvider : ModelMetadataProvider { protected AssociatedMetadataProvider(); protected abstract ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName); protected virtual IEnumerable<Attribute> FilterAttributes(Type containerType, PropertyDescriptor propertyDescriptor, IEnumerable<Attribute> attributes); public override IEnumerable<ModelMetadata> GetMetadataForProperties(object container, Type containerType); protected virtual ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, PropertyDescriptor propertyDescriptor); public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName); public override ModelMetadata GetMetadataForType(Func<object> modelAccessor, Type modelType); protected virtual ICustomTypeDescriptor GetTypeDescriptor(Type type); }
There are a lot of methods that you don't need to worry about for the moment. Most methods are used to recursively generate Model metadata (the specific process will be explained in the next article ). Switch back to the topic,AttributeListThe origin of the type is throughModelMetadataProviderThe GetTypeDescriptor () method of is used to generate a Model based on the Model type (this can be understood for the moment. After reading the next section, we will know that this may also be the attribute type in the Model ).ICustomTypeDescriptorType (it can be imagined that this is an abstract definition of an object type metadata description object. It is a bit difficult to read, but it does mean so ). The system has a default custom implementation to implement this interface type. We can use this default implementation to obtainAttributeListType.
WithAttributeListType, we can callAssociatedMetadataProviderThe CreateMetadata () method is used to create the Model metadata object. However, the CreateMetadata () definition is abstract, and the actual implementation is inherited.AssociatedMetadataProviderTypeDataAnnotationsModelMetadataProviderType.ModelMetadataMetadata object (the real process is complicated, otherwise it will not be used to explain the generation process separately). After obtaining the Model metadata object, it will not end, but it will continue to be called.AssociatedMetadataProviderThe ApplyMetadataAwareAttributes () method. In this method, the system framework calls an object of the IMetadataAware interface type customized to modify the Model metadata object, in the end, the Model metadata object is actually returned.
It is possible to see that some friends here are still not very clear about the Model metadata, and they are anxious and anxious. If all of the knowledge I have shared has been written to Alibaba Cloud, why should I. Don't worry about the amount of data you have read. You should have some knowledge about this article in the Model metadata series, and I will reveal its secrets in the subsequent chapters. Thank you for your support.
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.