nHibernate验证程序自定义IMessageInterpolator [英] nHibernate Validator custom IMessageInterpolator

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

问题描述

任何人都设法使自定义IMessageInterpolator起作用以 启用错误消息的定制.我已经尝试遵循 此网页上的说明,但无济于事. http://codelog.climens.net/2009/03 /04/nhibernate-validator-custom-messages/

Has anyone managed to get a custom IMessageInterpolator working to enable customisation of error messages. I have tried following the instructions on this web page but to no avail. http://codelog.climens.net/2009/03/04/nhibernate-validator-custom-messages/

查看代码,DefaultMessageInterpolator看起来很漂亮 融入框架中,所以我有什么想念的吗?

Looking into the code the DefaultMessageInterpolator seems pretty baked into the framework so is there anything I am missing.

我已经包含了单元测试,以演示如何实现它.

I have included my unit tests to show how I am implementing it.

namespace TestValidator
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestValidator()
        {
            var customer = new Customer();

            ClassValidator classValidator = new ClassValidator(customer.GetType());
            InvalidValue[] validationMessages = classValidator.GetInvalidValues(customer);

            Assert.IsTrue(validationMessages.Length == 1);
            Assert.IsTrue(validationMessages[0].Message == "may not be null or empty");
        }

        [TestMethod]
        public void TestCustomInterpolator()
        {
            ValidatorEngine ve = new ValidatorEngine();
            NHVConfiguration nhvc = new NHVConfiguration();
            nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(CustomMessageInterpolator).AssemblyQualifiedName;
            ve.Configure(nhvc);

            var customer = new Customer();

            ClassValidator classValidator = new ClassValidator(customer.GetType());
            InvalidValue[] validationMessages = classValidator.GetInvalidValues(customer);

            Assert.IsTrue(validationMessages.Length == 1);
            // THIS TEST FAILS!!
            Assert.IsTrue(validationMessages[0].Message == "CustomMessageInterpolator");
        }
    }

    public class Customer
    {
        public virtual int CustomerId { get; set; }

        [NotNullNotEmpty]
        public string CustomerName { get; set; }
    }

    public class CustomMessageInterpolator : IMessageInterpolator
    {
        public CustomMessageInterpolator()
        {

        }

        public string Interpolate(string message, IValidator validator, IMessageInterpolator defaultInterpolator)
        {
            return "CustomMessageInterpolator";
        }
    }
}

推荐答案

我终于完成了这项工作.在我的存储库中调用验证器之前,我明确创建了Interpolator并将其作为参数传递.

I have got this to work finally. Before calling the validator in my repository I explicitly create and pass the Interpolator as a parameter.

ClassValidator classValidator = new ClassValidator(obj.GetType(), null, new CustomMessageInterpolator(), 
CultureInfo.CurrentCulture, ValidatorMode.UseAttribute);
InvalidValue[] validationMessages = classValidator.GetInvalidValues(obj);

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

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