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