Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Created April 5, 2011 16:15
Show Gist options
  • Save hyrmn/903914 to your computer and use it in GitHub Desktop.
Save hyrmn/903914 to your computer and use it in GitHub Desktop.
app_start calls this structuremap bootstrapper
public static class Bootstrapper
{
public static void Initialize()
{
StructureMapConfigurator.ConfigureObjectFactory();
ObjectFactory.GetInstance<CitySearchService>().Initialize();
ObjectFactory.GetInstance<StatisticsService>().Initialize();
}
}
public sealed class NHibernateRegistry : Registry
{
public NHibernateRegistry()
{
var fluentConfig = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("kfdb"))
.ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
.Cache(
c => c.ProviderClass("NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache"))
.UseReflectionOptimizer()
.DoNot.ShowSql())
.Mappings(m =>
{
m.HbmMappings.AddFromAssemblyOf<IEntity>();
m.FluentMappings.AddFromAssemblyOf<IEntity>();
});
For<ISessionFactory>().Singleton().Use(fluentConfig.BuildSessionFactory());
For<ISession>().HybridHttpOrThreadLocalScoped().Use(c => c.GetInstance<ISessionFactory>().OpenSession());
}
}
public static class StructureMapConfigurator
{
public static void ConfigureObjectFactory()
{
ObjectFactory.Initialize(i =>
{
i.IgnoreStructureMapConfig = true;
i.For(typeof (IDataMapper<>)).Use(typeof (DataMapper<>));
i.AddRegistry(new NHibernateRegistry());
i.AddRegistry(new ServiceRegistry());
i.Scan(s =>
{
s.Assembly("KennelFinder");
s.WithDefaultConventions();
});
});
}
}
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType != null)
return ObjectFactory.GetInstance(controllerType) as Controller;
return base.GetControllerInstance(controllerType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment