NHibernate Validator 未与 Fluent NHibernate 集成

发布于 2024-10-30 23:58:47 字数 1656 浏览 9 评论 0原文

我在让 NHV 与 Fluent NHibernate 配合使用时遇到一些问题。我的单元测试有一个应该验证失败的实体,最终会抛出 ADO 异常。我按照以下方式配置了 NHV:

    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。我有一个单元测试来验证应该失败的同一实体,该实体直接通过验证引擎对其进行验证,并且这是有效的。

有谁发现上面的配置有什么问题吗?

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

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

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?

I'm using NHibernate 3.1, NHibernate Validator 1.3 and Fluent NHibernate 1.2.0.712

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2024-11-06 23:58:48

我对此进行了调试,似乎当它验证我的实体时,它再次初始化了我的验证器引擎。我通过将上面的ConfigureNhibernateValidator(Configuration config)方法更改为以下内容来纠正这个问题(这里的关键是设置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);
    }

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文