In the MVC model, we can define a lot of metadata related to the view, which plays a very important role in developing the view, especially in data verification. These metadata are generally not defined on the business entity (or persistent entity), so in many cases, we need to develop two types of entities: view model and business model. In this way, we need to use the view model in the communication between action and view. Then, when processing the business logic, we need to map the view model to the business model, this will make our development framework cumbersome. Generally, the view model and business model are similar objects in many cases, but the view model has a lot of metadata related to the view. In this case, as long as we can use the view model as the metadata description object (metadatatype) of the business model, we do not directly participate in the communication between the action and the view, let these jobs be undertaken by the business model, which can effectively avoid a lot of repetitive work.
InSystem. componentmodel. dataannotationsThe metadatatypeattribute label is provided internally, so that we can specify the view metadata type for the business model. Especially when we use frameworks such as linq2sql and EF to generate an object framework, we can provide its corresponding view metadata type in the form of a partial class:
[Metadatatype (typeof (product_metadata)] public partial class product {} public class product_metadata {}
In most cases, this is okay. However, this alone cannot solve all problems. Generally, the business model and metadatatype are not in the same assembly, so you cannot extend the business model in the form of a partial class. Therefore, we need a mechanism to delay the registration of the Business ing between the business model and metadatatype. Through MVC source code analysis, we can extend the dataannotationsmodelmetadataprovider's
Gettypedescriptor method to solve this problem.
First, we define a business ing container between business model and metadatatype:
Public static class typedescriptorhelper {static hashtable = new hashtable (); static synchronized locker = new synchronized (); static typedescriptorhelper () {} public static void registermetadatatype (type, type metadatatype) {locker. enterwritelock (); hashtable [type] = metadatatype; locker. exitwritelock ();} public static icustomtypedescriptor get (type) {locker . Enterreadlock (); var metadatatype = hashtable [type] as type; icustomtypedescriptor descriptor = NULL; If (metadatatype! = NULL) {descriptor = new associatedmetadatatypetypedescriptionprovider (type, metadatatype). gettypedescriptor (type);} locker. exitreadlock (); Return descriptor ;}}
This container is very simple. We just define a hashtable to save their ing relationships. Then, we inherit a new class from dataannotationsmodelmetadataprovider and override the gettypedescriptor method:
Public class customdataannotationsmodelmetadataprovider: dataannotationsmodelmetadataprovider {protected override system. componentmodel. icustomtypedescriptor gettypedescriptor (type) {var descriptor = typedescriptorhelper. get (type); If (descriptor = NULL) {descriptor = base. gettypedescriptor (type);} return descriptor ;}}
The next thing to do is to register customdataannotationsmodelmetadataprovider to the system and use it to replace the original dataannotationsmodelmetadataprovider. Add the following in global. asax:Code:
Modelmetadataproviders. Current = new customdataannotationsmodelmetadataprovider ();
Finally, you need to add the desired ing in the ing container:
Typedescriptorhelper. registermetadatatype (typeof (product), typeof (product_metadata ));
With this ing, we can completely free up the business model dependency on metadatatype.
Note: A very low-level error occurred while debugging this extension. It took me several hours. In other words, the submitted data contains a string named site (in route. values), And I define a complex type named site in the Action parameter.ProgramDuring running, an exception always occurs: The type cannot be converted. I suspect that I have extended the metadatatype, so I tried my best to debug it over there and I never found the cause. At last, all the code is restored, and the problem persists, so that I can go in the right direction and let me die.
Supplement: Extended dataannotationsmodelmetadataprovider can only obtain UI-related metadata in dataannotations. To obtain the Validation-related metadata, you also need to extend dataannotationsmodelvalidatorprovider. If the extension method is the same, the gettypedescriptor method is overwritten.