带有自定义 ResourceProvider 的 DataAnnotation [英] DataAnnotation with custom ResourceProvider

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

问题描述

我创建了一个自定义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 具有 ErrorMessageResourceTypeErrorMessageResourceName 属性,但 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?

推荐答案

我意识到这是一个老问题,但想补充一点.我发现自己处于同样的情况,似乎没有关于这个主题的任何文档/博客.尽管如此,我还是想出了一种使用自定义资源提供程序的方法,但需要注意的是.需要注意的是,我在一个 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; } 

新属性如下所示:

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);
    }
}

注意事项

  • 您需要使用派生属性而不是标准属性来装饰您的代码
  • 我正在使用 ErrorMessageResourceType 来传递正在验证的类的类型.我的意思是,如果我在客户类中并验证 FirstName 属性,我将通过 typeof(customer).我这样做是因为在我的数据库后端中,我使用完整的类名(命名空间 + 类名)作为键(与在 asp.net 中使用页面 URL 的方式相同).
    • 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

      (半被盗的)帮助代码看起来像这样......

      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;
          }
      }
      

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

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