Creating a multi-tenant asp.net mvc 3 application with one application instance / several databases for each tenant. A separate βmasterβ database will also be created, in which data specific to the tenant will be placed (included functions, information about connecting to a db connection, etc.). New to NHibernate and IOC (Castle Windsor) and used this tutorial to get a basic CRUD setup.
The following is what I use (from the above tutorial) to use NHibernate:
public class PersistenceFacility : AbstractFacility
{
protected override void Init()
{
var config = BuildDatabaseConfiguration();
Kernel.Register(
Component.For<ISessionFactory>()
.UsingFactoryMethod(config.BuildSessionFactory),
Component.For<ISession>()
.UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
.LifeStyle.PerWebRequest);
}
private Configuration BuildDatabaseConfiguration()
{
return Fluently.Configure()
.Database(SetupDatabase)
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<SectionMap>()
.Conventions.AddFromAssemblyOf<TableNameConvention>();
})
.ExposeConfiguration(ConfigurePersistence)
.BuildConfiguration();
}
protected virtual AutoPersistenceModel CreateMappingModel()
{
var m = AutoMap.Assembly(typeof(EntityBase).Assembly)
.Where(IsDomainEntity)
.OverrideAll(ShouldIgnoreProperty)
.IgnoreBase<EntityBase>();
return m;
}
protected virtual IPersistenceConfigurer SetupDatabase()
{
return MsSqlConfiguration.MsSql2008
.DefaultSchema("dbo")
.UseOuterJoin()
.ProxyFactoryFactory(typeof(ProxyFactoryFactory))
.ConnectionString(x => x.FromConnectionStringWithKey("MasterDB"))
.ShowSql();
}
protected virtual void ConfigurePersistence(Configuration config)
{
SchemaMetadataUpdater.QuoteTableAndColumns(config);
}
protected virtual bool IsDomainEntity(Type t)
{
return typeof(EntityBase).IsAssignableFrom(t);
}
private void ShouldIgnoreProperty(IPropertyIgnorer property)
{
property.IgnoreProperties(p => p.MemberInfo.HasAttribute<DoNotMapAttribute>());
}
}
, , , /URL- , , "master" db, . , , , SessionFactory . - , ( ) . /, , . [B] , Castle Windsor. , , , , .
!
: ASP.NET MVC 3,.NET 4, Castle Windsor + Fluent NHibernate + NHibernate ( NuGet)