NHibernate Forge
The official new home for the NHibernate for .NET community

Setup NHV fluently with your IoC container

Wiki Page Hierarchy

Pages

Page Details

First published by:
Jose Romaniello
on 04-25-2009
Last revision by:
Jose Romaniello
on 04-25-2009
2 people found this article useful.
Article
Comments (1)
History (5)
100% of people found this useful

Setup NHV fluently with your IoC container

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

Recent Comments

By: JuanCho Posted on 04-25-2009 23:46

Great post Jose, makes NHV implementation a lot clearer now

View All
Powered by Community Server (Commercial Edition), by Telligent Systems