NHibernate Validator 未与 Fluent NHibernate 集成
我在让 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.1、NHibernate Validator 1.3 和 Fluent 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此进行了调试,似乎当它验证我的实体时,它再次初始化了我的验证器引擎。我通过将上面的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):