100% of people found this useful
The reasons behind why you should
implement an IsharedEngineProvider are well explained here.
In this howto I will show a easy way to
achieve that.
First of all this is my implementation
of ISharedEngineProvider:
public class CastleSharedEngineProvider : ISharedEngineProvider
{
private readonly ValidatorEngine _validatorEngine;
public CastleSharedEngineProvider(ValidatorEngine validatorEngine)
{
_validatorEngine = validatorEngine;
}
public ValidatorEngine GetEngine()
{
return _validatorEngine;
}
}
as you can see is very straightforward.
Second I will show you how to setup
your IoC. I use a castle container for this sample:
private static void ConfigureValidator()
{
//Create a new ValidatorEngine.
var ve = new ValidatorEngine();
//Register the ValidatorEngine component for singleton.
container.Register(Component.For<ValidatorEngine>()
.Instance(ve).LifeStyle.Singleton);
//Register the service for ISharedEngineProvider
container.Register(Component.For<ISharedEngineProvider>()
.ImplementedBy<CastleSharedEngineProvider>());
//Assign the shared engine provider for NHV.
NHibernate.Validator.Cfg.Environment.SharedEngineProvider =
container.Resolve<ISharedEngineProvider>();
//Configure validation framework fluently
var configure = new FluentConfiguration();
configure.Register(
Assembly.Load("SGF.Dominio")
.ValidationDefinitions()
)
.SetDefaultValidatorMode(ValidatorMode.UseAttribute)
.IntegrateWithNHibernate.ApplyingDDLConstraints().And.RegisteringListeners();
ve.Configure(configure);
}
There are three things that you need remember:
-You can't initialize or configure
NHV before you have assigned the SharedEngineProvider.
-Never create a new instance of
ValidatorEngine, pick the singleton from the IoC or the
SharedEngineprovider
-If you use DDL Constraints or
Listeners for Nhibernate you need to tell NHV what is the NH config
that need to be populate with this info.
ValidatorInitializer.Initialize(nhConfiguration);