Question

I'm looking for the best way to configure NHibernate Validator on a classic ASP.net app that is using NHibernate Burrow to manage NH sessions.
How do I get it to register the interecptors automatically?

Was it helpful?

Solution

OK, here is what I ended up doing.
First I set up a SharedEngineProvider in global.asax Application_Start event.

protected void Application_Start(object sender, EventArgs e)
{
     InitializeValidator();
}

private void InitializeValidator()
{
            NHibernateSharedEngineProvider provider = new NHibernateSharedEngineProvider();
            NHVConfigurationBase config = new NHVConfigurationBase();
            config.Properties[Environment.ApplyToDDL] = "true";
            config.Properties[Environment.AutoregisterListeners] = "true";
            config.Properties[Environment.ValidatorMode] = ValidatorMode.UseAttribute.ToString();
            config.Mappings.Add(new MappingConfiguration(Assembly.GetAssembly(typeof(User)).FullName, null));
            provider.GetEngine().Configure(config);
            Environment.SharedEngineProvider = provider;
 }

Since Burrow intercepts requests for pages to start the NH Session I decided to hook on the PreInit event to integrate the validator with the current NH session. I have a custom base page (a good practice I think) so I added this method there:

protected void Page_PreInit(object sender, EventArgs args)
{
    Configuration cfg = new BurrowFramework().BurrowEnvironment.GetNHConfig("PersistenceUnit1");
    ValidatorInitializer.Initialize(cfg);
}

So far it seems to be working fine.

To get a reference to the validator engine I'm using:

validatorEngine = Environment.SharedEngineProvider.GetEngine();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top