The object-relational mapper for .NET


This page is converted from the old nhforge.org Wiki. First published by: Jose Romaniello on 04-25-2009, Last revision by: Jose Romaniello on 04-25-2009

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.

    //Register the service for ISharedEngineProvider

    //Assign the shared engine provider for NHV.
    NHibernate.Validator.Cfg.Environment.SharedEngineProvider =
    //Configure validation framework fluently
    var configure = new FluentConfiguration();


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.

© NHibernate Community 2024