Skip to content

Instantly share code, notes, and snippets.

@brendanjerwin
Created March 12, 2009 01:52
Show Gist options
  • Save brendanjerwin/77861 to your computer and use it in GitHub Desktop.
Save brendanjerwin/77861 to your computer and use it in GitHub Desktop.
A configuration class I use to configure my applications at startup.
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Validator.Cfg;
using NHibernate.Validator.Engine;
using StructureMap;
using StructureMap.Attributes;
namespace Models.Data.Util
{
public static class Configuration
{
private static ValidatorEngine ConfigureValidator(NHibernate.Cfg.Configuration nhibernateConfiguration)
{
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(nhibernateConfiguration, ve);
return ve;
}
private static ISessionFactory ConfigureNHibernate(IPersistenceConfigurer databaseConfigurer, out ValidatorEngine validatorEngine, NHibernate.Cfg.Configuration cfg)
{
ValidatorEngine ve = null;
var factory = Fluently.Configure(cfg)
.Database(databaseConfigurer)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PatientMap>())
.ExposeConfiguration(c =>
{
ve = ConfigureValidator(c);
c.SetProperty("adonet.batch_size", "5");
c.SetProperty("generate_statistics", "true");
})
.BuildSessionFactory();
validatorEngine = ve;
return factory;
}
public static void ConfigureDataAccess(string sqlServerAddress, string username,
string password, string database, InstanceScope sessionScope)
{
ObjectFactory.Initialize(i => ConfigureDataAccess(i, sqlServerAddress, username, password, database, sessionScope));
ObjectFactory.AssertConfigurationIsValid();
}
public static void ConfigureDataAccess(IPersistenceConfigurer databaseConfigurer, InstanceScope sessionScope, out NHibernate.Cfg.Configuration cfg)
{
NHibernate.Cfg.Configuration configuration = null;
ObjectFactory.Initialize(i => ConfigureDataAccess(i, databaseConfigurer, sessionScope, out configuration));
ObjectFactory.AssertConfigurationIsValid();
cfg = configuration;
}
public static void ConfigureDataAccess(IInitializationExpression i, string sqlServerAddress, string username,
string password, string database, InstanceScope sessionScope)
{
NHibernate.Cfg.Configuration cfg;
ConfigureDataAccess(i, MsSqlConfiguration.MsSql2005.ConnectionString(c =>
c.Server(sqlServerAddress).Username(username).Password(password).Database(database)),sessionScope, out cfg);
}
public static void ConfigureDataAccess(IInitializationExpression i, IPersistenceConfigurer databaseConfigurer, InstanceScope sessionScope, out NHibernate.Cfg.Configuration cfg)
{
cfg = new NHibernate.Cfg.Configuration();
ValidatorEngine validatorEngine;
i.ForRequestedType<ISessionFactory>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.IsThis(ConfigureNHibernate(databaseConfigurer, out validatorEngine, cfg));
i.ForRequestedType<ISession>()
.CacheBy(sessionScope)
.TheDefault.Is.ConstructedBy(() =>
ObjectFactory.GetInstance<ISessionFactory>
().OpenSession());
i.ForRequestedType<ValidatorEngine>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.IsThis(validatorEngine);
//More StructureMap configuration goes here:
//i.ForRequestedType<ISomeModelRepository>()
// .CacheBy(InstanceScope.PerRequest)
// .TheDefaultIsConcreteType<SomeModelRepository>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment