NHibernate 验证器未与 Fluent NHibernate 集成 [英] NHibernate Validator not integrating with Fluent NHibernate

查看:15
本文介绍了NHibernate 验证器未与 Fluent NHibernate 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让 NHV 与 Fluent NHibernate 一起工作时遇到了一些麻烦.我有一个单元测试,它有一个实体应该没有通过验证,最终会抛出一个 ADO 异常.我已按以下方式配置 NHV:

I'm having some trouble getting NHV to work with Fluent NHibernate. A unit test that I have that has an entity that SHOULD be failing validation ends up throwing an ADO exception. I have NHV configured the following way:

    private static void Init()
    {
            _SessionFactory = Fluently.Configure()
              .Database(MsSqlConfiguration.MsSql2005.ConnectionString(connectionString)
                  .ShowSql())
              .Mappings(m =>
                  m.FluentMappings.AddFromAssemblyOf<SessionFactory>()
                  .ExportTo(pathToExportMappingsTo))
              .ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
              .ExposeConfiguration(ConfigureNhibernateValidator)
              .BuildSessionFactory();
    }

    private static void ConfigureNhibernateValidator(Configuration config)
    {
        var nhvConfiguration = new NHibernate.Validator.Cfg.Loquacious.FluentConfiguration();
        nhvConfiguration
           .SetDefaultValidatorMode(ValidatorMode.OverrideAttributeWithExternal)
           .Register(Assembly.Load("Business.Objects")
           .ValidationDefinitions())
           .IntegrateWithNHibernate
               .RegisteringListeners();

        ValidatorEngine validatorEngine = new ValidatorEngine();
        validatorEngine.Configure(nhvConfiguration);

        ValidatorInitializer.Initialize(config, validatorEngine);
    }

我已经多次查看此配置并在互联网上搜索以尝试找出此配置的问题所在.我还查看了 NHV 源中提供的示例,但我无法弄清楚为什么我的单元测试不会抛出 InvalidStateException.我有一个单元测试来验证应该失败的同一个实体,它直接通过验证引擎验证它,这有效.

I've looked over this configuration several times now and scoured the internet to try and find out what's wrong with this. I've also looked at examples provided in the NHV source but I haven't been able to figure out why my unit test does not throw an InvalidStateException. I have a unit test to validate the same entity that should be failing that validates it directly via the validation engine and this works.

有人觉得上面的配置有什么问题吗?

Does anyone see anything wrong with the above configuration?

我正在使用 NHibernate 3.1NHibernate Validator 1.3Fluent NHibernate 1.2.0.712

推荐答案

我调试了这个,似乎当它去验证我的实体时,它再次初始化了我的验证器引擎.我通过将上面的 ConfigureNhibernateValidator(Configuration config) 方法更改为以下方法来更正此问题(此处的关键是设置 SharedEngineProvider):

I debugged this and it seemed that when it went to validate my entity it was initialize my validator engine again. I corrected this by changing the ConfigureNhibernateValidator(Configuration config) method above to the following (the key here was to set the SharedEngineProvider):

    private static void ConfigureNhibernateValidator(Configuration config)
    {
        var provider = new NHibernateSharedEngineProvider();
        NHibernate.Validator.Cfg.Environment.SharedEngineProvider = provider;

        var nhvConfiguration = new NHibernate.Validator.Cfg.Loquacious.FluentConfiguration();
        nhvConfiguration
           .SetDefaultValidatorMode(ValidatorMode.OverrideAttributeWithExternal)
           .Register(Assembly.Load("Business.Objects")
           .ValidationDefinitions())
           .IntegrateWithNHibernate
               .AvoidingDDLConstraints()
               .RegisteringListeners();

        ValidatorEngine validatorEngine = NHibernate.Validator.Cfg.Environment.SharedEngineProvider.GetEngine();
        validatorEngine.Configure(nhvConfiguration);

        ValidatorInitializer.Initialize(config, validatorEngine);
    }

这篇关于NHibernate 验证器未与 Fluent NHibernate 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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