2011-05-06 95 views
3

創建一個多租戶asp.net mvc 3應用程序,每個租戶只有一個應用程序實例/多個數據庫。還將有一個單獨的'主'數據庫,將容納租戶特定的信息(啓用功能,租戶數據庫連接信息等)。 NHibernate &國際奧委會(溫莎城堡)和使用this tutorial新手都可以獲得基本的CRUD設置。多租戶w/NHibernate +溫莎城堡(單個應用程序,多個數據庫)

以下是我使用(從上面提到的教程),以「用」的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。道歉,因爲castle site似乎是一個很好的資源,但不像我這樣的新手容易理解。

謝謝!

環境:ASP.NET MVC 3,.NET 4,城堡溫莎+功能NHibernate + NHibernate的(通過的NuGet)

回答

1

通常是真實所採取的方法是將具有多個會話工廠(每個租戶一個),並使用IHandlersSelector根據請求中的某些數據選擇合適的人。

關於關於文檔不易理解的評論,如果您想指出不太容易遵循的部分,我們非常希望改進它。

+0

IHandlerSelector看起來不錯,我發現Ayende和Mike Hadlow的帖子也是如此。但仍然不知道如何使用它從主數據庫獲取特定於租戶的數據庫連接信息,然後使用它爲該租戶創建新的會話工廠,類似於您的'持久性設置'對主數據庫的操作方式。關於關於文件的評論,我認爲更多的細節和明確的現實世界的例子可能會讓那些不熟悉IOC概念的人更容易。謝謝! – saasn00b 2011-05-06 17:53:52