如何延长CachedDataAnnotationsModelMetadataProvider? [英] How can I extend CachedDataAnnotationsModelMetadataProvider?

查看:176
本文介绍了如何延长CachedDataAnnotationsModelMetadataProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在寻找使用 CachedDataAnnotationsModelMetadataProvider ,因为它的提高性能,我们在我们的MVC4应用程序中使用了大量元数据。

We are looking to use CachedDataAnnotationsModelMetadataProvider as it improves performance and we use a lot of Meta Data in our MVC4 application.

目前,我们正在创建一个自定义ModelMetadataProvider从DataAnnotationsModelMetadataProvider继承和覆盖 CreateMetadata 属性来如做一些自动生成的显示名称从名称等除去标识然而,我们也希望缓存它所以我们希望我们的基础上CachedDataAnnotationsModelMetadataProvider定制ModelMetadataProvider。

We are currently creating a custom ModelMetadataProvider inheriting from DataAnnotationsModelMetadataProvider and overriding CreateMetadata attribute to do some automatic display name creation e.g. remove Id from names etc. However we also want to cache it so we wanted to base our custom ModelMetadataProvider on CachedDataAnnotationsModelMetadataProvider.

如果我们试图重写 CreateMetadata 我们不能因为它是密封的。它是密封的任何原因 - 我想我可以得到源,只是重新实现只是觉得奇怪的是,我不能延长

If we try to override CreateMetadata we can't as it is sealed. Any reason it is sealed - I guess I can get the source and just reimplement just found it odd that I couldn't extended?

有没有人做过类似的东西吗?

Has anyone done anything similar?

推荐答案

我猜想,它是密封的原因是因为实际的 CreateMetadata 实现包含缓存逻辑,你不应该被修改

I would guess that the reason it is sealed is because the actual CreateMetadata implementation contains caching logic that you should not be modifying.

要延长 CachedDataAnnotationsModelMetadataProvider ,我发现下列似乎运作良好:

To extend CachedDataAnnotationsModelMetadataProvider, I find that the following seems to work well:

 using System.Web.Mvc;

 public class MyCustomMetadataProvider : CachedDataAnnotationsModelMetadataProvider
 {
      protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
      {
           var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);

           //modify the base result with your custom logic, typically adding items from
           //prototype.AdditionalValues, e.g.
           result.AdditionalValues.Add("MyCustomValuesKey", prototype.AdditionalValues["MyCustomValuesKey"]);

           return result;
      }

      protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(IEnumerable<Attribute> attributes, Type containerType, Type modelType, string propertyName)
      {
           CachedDataAnnotationsModelMetadata prototype = base.CreateMetadataPrototype(attributes, containerType, modelType, propertyName);

           //Add custom prototype data, e.g.
           prototype.AdditionalValues.Add("MyCustomValuesKey", "MyCustomValuesData");

           return prototype;
      }
 }

这篇关于如何延长CachedDataAnnotationsModelMetadataProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆