它是有效的,以取代DataAnnotationsModelMetadataProvider和操纵返回ModelMetadata [英] Is it valid to replace DataAnnotationsModelMetadataProvider and manipulate the returned ModelMetadata

查看:148
本文介绍了它是有效的,以取代DataAnnotationsModelMetadataProvider和操纵返回ModelMetadata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在建设使用MVC3中型Intranet应用程序。一个要求是改变和动态翻译字段名称和标签的可能性。目前,我们正在使用的扩展版本的ViewPage ViewMasterPage ViewUserControl 通过spring.net注入来实现这一要求的翻译帮手丰富。

We are currently building a mid-sized intranet application using MVC3. One requirement is the possibility to change and translate field names and labels on the fly. We are currently using extended versions of ViewPage, ViewMasterPage and ViewUserControl enriched by an translation helper injected by spring.net to achieve this requirement.

该解决方案解决了依赖于 ModelMetadata 除了MVC功能的所有要求(例如, LabelFor )。我首先想到的是,应该有一个扩展点,以取代目前的实现。幸运的是,在某种程度上MVC团队建设 ModelMetadataProviders ,我可以注入一个自定义的翻译知道 DataAnnotationsModelMetadataProvider

This solution solves all requirements except MVC features that rely on ModelMetadata (e.g. LabelFor). My first thought was that there should be an extension point to replace the current implementation. Luckily the MVC team build ModelMetadataProviders in a way that I can inject a custom translation aware DataAnnotationsModelMetadataProvider.

protected override ModelMetadata CreateMetadata( IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName )
{
  // Call the base method and obtain a metadata object.
  var metadata = base.CreateMetadata( attributes, containerType, modelAccessor, modelType, propertyName );

  if( containerType != null )
  {
    // Obtain informations to query the translator.
    var objectName = containerType.FullName;
    var displayName = metadata.GetDisplayName();

    // Query the translator.
    var translatedDisplayName = Translator.Translate( objectName, displayName );

    // Update the metadata.
    metadata.DisplayName = translatedDisplayName;
  }

  return metadata;
}

我使用了 containerType GetDisplayName()为重点,以获得从翻译贡献者翻译(使用高速缓存,不会降低性能),然后调用基础,由之前更新显示名称属性更改返回 ModelMetadata 其返回。

I'm using the containerType and GetDisplayName() as key to obtain a translation from a translation contributor (that uses caching, does not degrade performance), then calling base and change the returned ModelMetadata by updating the DisplayName property before returning it.

是什么让我不自信这个解决方案是,我不能predict我有什么副作用期待?或者,如果有更好的,妥善的解决方案?

What makes me unconfident about this solution is that I can't predict what side effects I have to expect? Or if there is a better, proper solution?

推荐答案

是的,这是有效的使用。

Yes, this is the valid usage.

请确保您从 DataAnnotationsModelMetadataProvider 导出为你做的再利用它提供的所有功能。

Make sure you derive from DataAnnotationsModelMetadataProvider as you do to reuse all the features it provides.

您也可以处理更多的在这里自定义属性 - 你可以用自己的自定义属性装饰你的模型,然后更新 ModelMetadata 基于它们

You can also handle more custom attributes here - you can decorate your model with your own custom attributes and then update ModelMetadata based on them.

这篇关于它是有效的,以取代DataAnnotationsModelMetadataProvider和操纵返回ModelMetadata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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