如何在NHibernate.Validator中获取插值消息

时间:2010-04-19 02:55:55

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

我正在尝试将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中获取插值字符串?

2 个答案:

答案 0 :(得分:2)

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

我认为问题中的代码是在正确的轨道上,但有几个问题。内插器未使用ResourceManagerCulture详细信息进行完全初始化,并且似乎不允许每个验证属性只能有一个DefaultMessageInterpolator。此外,您不需要验证对象的实例来获取插值消息。

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

可以使用Initialize上具有以下签名的重载DefaultMessageInterpolator方法来完成此操作:

    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来处理这个问题,或者只是自己动手。

我希望这有助于某人。

答案 1 :(得分:1)

感谢您的帮助 - 如果可以的话,我会支持。我只是想补充一点,除了Stank说明的DefaultMessageInterpolator上的第一个Initialize调用之外,我还必须进行第二次不同的Initialize调用以完全初始化它(我只使用第一次调用获得了一些Null Reference Exceptions)。我的代码如下:

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