DataAnnotation定制ResourceProvider [英] DataAnnotation with custom ResourceProvider

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

问题描述

我创建了一个自定义 ResourceProvider 可以从数据库中获取本地化信息。我现在想用 DataAnnotation 来验证添加到模型中。

I have created a custom ResourceProvider to pull localization information from a database. I now want to use DataAnnotation to add validation to the model.

DataAnnotation ErrorMessageResourceType ErrorMessageResourceName 属性,但 ErrorMessageResourceType 只接受的System.Type (即编译的资源文件)

DataAnnotation has ErrorMessageResourceType and ErrorMessageResourceName properties but ErrorMessageResourceType only accepts System.Type (i.e. a compiled resource file)

有没有什么办法让DataAnnotation使用自定义ResourceProvider?

Is there any way to get DataAnnotation to use the custom ResourceProvider?

推荐答案

我意识到这是一个老问题,但想补充一点。我发现自己在同样的情况,并似乎没有要上这个话题的任何文档/ blogumentation。尽管如此,我想出了一个方法来使用自定义的资源提供者,有一点需要注意。需要说明的是,我在MVC应用程序,所以我还是有 HttpContext.GetLocalResourceObject()可用。这是asp.net使用本地化项目的方法。缺乏资源的对象不会从编写我们自己的解决方案,即使其数据库表直接查询阻止你。不过,我认为这是值得指出的。

I realize this is an old question, but wanted to add a bit. I found myself in the same situation and there doesn't appear to be any documentation/blogumentation on this topic. Nevertheless, I figured out a way to use a custom resource provider, with one caveat. The caveat is that I'm in an MVC application so I still have HttpContext.GetLocalResourceObject() available. This is the method that asp.net uses to localize items. The absence of the resource object doesn't stop you from writing our own solution, even if its a direct query of the DB tables. Nevertheless, I thought it was worth pointing out.

虽然我没有与下面的解决方案非常高兴,它似乎工作。对于每个验证属性我想用我的上述属性继承和重载的IsValid()。装修看起来是这样的:

While I'm not terribly happy with the following solution, it seems to work. For each validation attribute I want to use I inherit from said attribute and overload the IsValid(). The decoration looks like this:

[RequiredLocalized(ErrorMessageResourceType= typeof(ClassBeginValidated), ErrorMessageResourceName="Errors.GenderRequired")]
public string FirstName { get; set; } 

新的属性看起来是这样的:

The new attribute looks like this:

public sealed class RequiredLocalized : RequiredAttribute {

    public override bool IsValid(object value) {

        if ( ! (ErrorMessageResourceType == null || String.IsNullOrWhiteSpace(ErrorMessageResourceName) )   ) {
            this.ErrorMessage = MVC_HtmlHelpers.Localize(this.ErrorMessageResourceType, this.ErrorMessageResourceName);
            this.ErrorMessageResourceType = null;
            this.ErrorMessageResourceName = null;
        }
        return base.IsValid(value);
    }
}

说明


  • 您需要与派生属性,而不是标准的一
  • 来装饰你的code
  • 我使用ErrorMessageResourceType传递类的类型被验证。我的意思是,如果我在一个客户类和验证的财产我会通过 typeof运算(客户)。我因为在我的数据库后端我使用类的全名(命名空间+类名)作为重点(以同样的方式在页面URL在asp.net使用)这样做。

    • MVC_HtmlHelpers.Localize 的仅仅是一个简单的包装为我定制的资源提供者

    • You need to decorate your code with the derived attribute, not the standard one
    • I'm using ErrorMessageResourceType to pass the type of the class being validated. By that I mean if I'm in a customer class and validating the FirstName property I would pass typeof(customer). I'm doing this because in my database backend I'm using the full class name (namespace + classname) as a key (the same way a page URL is used in asp.net).
      • MVC_HtmlHelpers.Localize is just a simple wrapper for my custom resource provider

      的(半被盗)辅助code看起来是这样的....

      The (semi-stolen) helper code looks like this ....

      public static string Localize (System.Type theType, string resourceKey) {
          return Localize (theType, resourceKey, null);
      }
      public static string Localize (System.Type theType, string resourceKey, params object[] args) {
          string resource = (HttpContext.GetLocalResourceObject(theType.FullName, resourceKey) ?? string.Empty).ToString();
          return mergeTokens(resource, args);
      }
      
      private static string mergeTokens(string resource, object[] args)        {
          if (resource != null && args != null && args.Length > 0) {
              return string.Format(resource, args);
          }  else {
              return resource;
          }
      }
      

      这篇关于DataAnnotation定制ResourceProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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