I am struggling to get Fluent NHibernate and the NHibernate Validator working together and there seems to be a lack of documentation on the internet about the best way to do this. I have found a few websites which detail how to configure the validator and NHibernate but not Fluent NHibernate. I realise that Fluent NHibernate is just NHibernate with nice mappings but I can't quite get my head around the configuration.
This is the code that I use to setup my SessionFactory
:
public static void Initialise()
{
Configuration config = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c
.Server("")
.Database("")
.Username("")
.Password("")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Map>()
.Conventions.AddFromAssemblyOf<EnumConvention>())
.BuildConfiguration();
DataBindingInterceptor interceptor = new DataBindingInterceptor();
SessionFactory = config
.SetInterceptor(interceptor)
.BuildSessionFactory();
}
From this question, I tried to create a method that looked like this to setup my validator:
private static void ConfigureValidator(Configuration config)
{
NHibernate.Validator.Cfg.Environment.SharedEngineProvider = new SharedValidatorProvider();
var nhvc = new NHVConfiguration();
nhvc.Properties[NHibernate.Validator.Cfg.Environment.ApplyToDDL] = "true";
nhvc.Properties[NHibernate.Validator.Cfg.Environment.AutoregisterListeners] = "true";
nhvc.Properties[NHibernate.Validator.Cfg.Environment.ValidatorMode] = "UseAttribute";
var ve = new ValidatorEngine();
ve.Configure(nhvc);
ValidatorInitializer.Initialize(config, ve);
}
However I got errors trying to find the namespace for SharedValidatorProvider
and NHVConfiguration
. I have the Castle.Core, FluentNHibernate, NHibernate, NHibernate.ByteCode.Castle, NHibernate.Validator and NHibernate.Validator.Specific DLLs referenced in the project and the following using
's:
using NHibernate.Validator.Engine;
using NHibernate.Validator.Event;
using NHibernate.Validator.Cfg;
using NHibernate.Cfg;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
I would really appreciate your help to get Fluent NHibernate and the NHibernate Validator working happily together. Hopefully this will become the definitive source when people try to do this in the future!
I think you want
NHibernateSharedEngineProvider
and NOTSharedValidatorProvider
in your code above. To useNHibernateSharedEngineProvider
you need the following using:NHVConfiguration
is a variable in that post that you are referring to and NOT a type. I think you need to look at that post more closely.Also just a note. If you ever have trouble finding a type the Object Browser in visual studio is your friend. It has the ability to search through all the assemblies you have included in your project.
Edit:
Below is what I'm currently doing modified what you want above but NHV is configured fluently instead: