【问题标题】:How to get interpolated message in NHibernate.Validator如何在 NHibernate.Validator 中获取插值消息
【发布时间】:2010-04-19 02:55:54
【问题描述】:

我正在尝试将 NHibernate.Validator 与 ASP.NET MVC 客户端验证集成,我发现的唯一问题是我根本无法将非插值消息转换为人类可读的消息。我认为这将是一项简单的任务,但结果证明这是客户端验证中最难的部分。主要问题是因为它不是服务器端的,我实际上只需要正在使用的验证属性,而我实际上没有实例或其他任何东西。

以下是我已经尝试过的一些摘录:

// Get the the default Message Interpolator from the Engine
IMessageInterpolator interp = _engine.Interpolator;
if (interp == null)
{
  // It is null?? Oh, try to create a new one
  interp = new NHibernate.Validator.Interpolator.DefaultMessageInterpolator();
}

// We need an instance of the object that needs to be validated, se we have to create one
object instance = Activator.CreateInstance(Metadata.ContainerType);

// we enumerate all attributes of the property. For example we have found a PatternAttribute
var a = attr as PatternAttribute;

// it seems that the default message interpolator doesn't work, unless initialized
if (interp is NHibernate.Validator.Interpolator.DefaultMessageInterpolator)
{
  (interp as NHibernate.Validator.Interpolator.DefaultMessageInterpolator).Initialize(a);
}

// but even after it is initialized the following will throw a NullReferenceException, although all of the parameters are specified, and they are not null (except for the properties of the instance, which are all null, but this can't be changed)
var message = interp.Interpolate(new InterpolationInfo(Metadata.ContainerType, instance, PropertyName, a, interp, a.Message));

我知道上面是一个看似简单的问题的相当复杂的代码,但我仍然没有解决方案。有没有办法从 NHValidator 中取出插值字符串?

【问题讨论】:

    标签: asp.net-mvc-2 nullreferenceexception nhibernate-validator


    【解决方案1】:

    好的,所以我知道这是一个老问题,但我在尝试做同样的事情时偶然发现了这个问题,它帮助我开始了 - 所以我想我会提供一个答案。

    我认为问题中的代码在正确的轨道上,但存在一些问题。插值器没有完全用ResourceManagerCulture 细节初始化,而且它似乎不允许每个验证属性只能有一个DefaultMessageInterpolator 的事实。此外,您不需要验证对象的实例即可获得内插消息。

    在问题的代码中,您使用属性值初始化插值器,您还需要使用要使用的ResourceManager 的详细信息来初始化插值器。

    这可以使用DefaultMessageInterpolator 上的重载Initialize 方法来完成,该方法具有以下签名:

        public void Initialize(ResourceManager messageBundle, 
                               ResourceManager defaultMessageBundle, 
                               CultureInfo culture)
    

    第一个参数是用户自定义的ResourceManager,如果你想使用自己的资源文件来显示错误消息,你可以传递一个null,如果你只想使用默认的ResouceManager,第二个参数是默认的ResourceManager - 你可以通过

      new ResourceManager( 
          NHibernate.Validator.Cfg.Environment.BaseNameOfMessageResource,
          Assembly.GetExecutingAssembly());
    

    为此,最后一个参数是要使用的文化,(NHibernate.Validator 附带资源文件,其中包含多种语言的验证消息) - 如果您将 null 传递给它,它将只使用 CultureInfo.CurrentCulture

    最后,每个属性只能有一个DefaultMessageInterpolator,因此您需要为每个验证属性创建一个新的DefaultMessageInterpolator。您可以使用DefaultMessageInterpolatorAggregator 来处理这个问题,或者自己动手。

    我希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      感谢大家的帮助——如果可以的话,我会投赞成票。我只想补充一点,除了 Stank 说明的 DefaultMessageInterpolator 上的第一个 Initialize 调用之外,我还必须进行第二个不同的 Initialize 调用来完全初始化它(我只使用第一个调用就得到了一些 Null 引用异常)。我的代码如下:

      string interpolatedMessage = "";
      DefaultMessageInterpolator interpolator = new DefaultMessageInterpolator();
      
      interpolator.Initialize(null,
          new ResourceManager(
              NHibernate.Validator.Cfg.Environment.BaseNameOfMessageResource,
              Assembly.Load("NHibernate.Validator")),
              CultureInfo.CurrentCulture);
      
      interpolator.Initialize(attribute as Attribute);
      
      if (attribute is IValidator && attribute is IRuleArgs)
      {
          IValidator validator = attribute as IValidator;
          IRuleArgs ruleArgs = attribute as IRuleArgs;
      
          InterpolationInfo interpolationInfo = new InterpolationInfo(
              validatableType, 
              null, 
              propertyName, 
              validator,
              interpolator, 
              ruleArgs.Message);
      
          interpolatedMessage = interpolator.Interpolate(interpolationInfo);
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-12
        • 1970-01-01
        • 2019-10-24
        • 2011-03-27
        • 1970-01-01
        相关资源
        最近更新 更多