NHibernate Validator Custom IMessageInterpolator

Has anyone been able to get a custom IMessageInterpolator that works to enable customization of error messages. I tried to follow the instructions on this web page, but to no avail. http://codelog.climens.net/2009/03/04/nhibernate-validator-custom-messages/

A look into the DefaultMessageInterpolator code seems pretty baked into the framework, so I have nothing to lose.

I included my unit tests to show how I implement them.

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";
        }
    }
}
+1
source share
4 answers

, . Interpolator .

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

, , :

Assert.AreEqual(typeof(CustomMessageInterpolator), ve.Interpolator.GetType());

. , , CFG DefaultMessageInterpolator,

ve.Configure(nhvc);

, ClassValidator DefaultMessageInterpolator. Climens App.config, nhvalidator.cfg.xml.

0

Nhibernate NHibernate, . , , , Singleton . , , - , .

        Environment.SharedEngineProvider = new NHibernateSharedEngineProvider();

        var fluentConfiguration = new FluentConfiguration();

        fluentConfiguration
            .SetMessageInterpolator<CustomMessageInterpolator>()
            .SetDefaultValidatorMode(ValidatorMode.OverrideAttributeWithExternal)
            .IntegrateWithNHibernate
            .ApplyingDDLConstraints()
            .And
            .RegisteringListeners();

        var validatorEngine = Environment.SharedEngineProvider.GetEngine();
        validatorEngine.Configure(fluentConfiguration);
        configuration.Initialize(validatorEngine);
0

, , NHV.

var engine = NHibernate.Validator.Cfg.Environment.SharedEngineProvider.GetEngine();
return engine.GetClassValidator(YourEntityType);
0

Source: https://habr.com/ru/post/1719993/


All Articles