2011-10-09 38 views
1

請考慮以下內容Fluent configuration;Fluent Nhibernate - ClassMaps in multiple,separate assemblies

FluentConfiguration config = Fluently.Configure(); 
     config.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.ConnectionString(ConfigurationManager.ConnectionStrings[dbKey].ConnectionString)); 

     // MemberMap is in the same assembly as this class, contains 
     // maps for member and role entities as part of a default 
     // membership service provider 
     MappingAssemblies.Add(typeof(MemberMap).Assembly); 

     foreach (Assembly mappingAssembly in MappingAssemblies) 
     { 
      // For each assembly that has been added to MappingAssemblies 
      // we add to the current mapping configuration. This allows 
      // me to drop this helper into any solution and it provide 
      // standardized membership abilities AS WELL AS manage 
      // the solutions unique ClassMaps 
      config.Mappings(m => 
       m.FluentMappings.AddFromAssembly(mappingAssembly) 
       ); 
     } 

     if (exportSchema) 
     { 
      config.ExposeConfiguration(cfg => 
        { 
         new SchemaExport(cfg) 
         .Create(true, true); 
        } 
       ); 
     } 

     _sessionFactory = config.BuildSessionFactory(); 

這個邏輯被保存在我的Global.asax應用程序啓動時調用的靜態類中。啓動配置看起來類似於;

Database.MappingAssemblies.Add(typeof(PageContentMap).Assembly); 
// This is the method detailed above 
Database.FluentConfigureSessionFactory("MySolutionsDb", true); 

這樣的想法是,我打包我的會員和角色的實體對象到相同的組件設置爲數據庫輔助對象,這樣我關心創建可以立即獲得我的標準化成員的能力,以及能夠任何解決方案簡單地創建自己的特定於解決方案的ClassMaps並將其添加到配置對象中。

問題是,熟悉的電話;

config.Mappings(m => 
       m.FluentMappings.AddFromAssembly(mappingAssembly) 
       ); 

似乎只能處理單個程序集。無論添加到列表中的是什麼,只添加最後一個程序集都會被映射。作爲上述的替代方案,我嘗試過參考MappingConfiguration(這是'm'代表config.Mappings(m =>)),但這也不起作用。很明顯,這樣一個m.FluentMappings.AddFromAssembly或實際上任何FluentMappings.Add方法的調用將覆蓋以前存在的內容,但肯定是有一種方法可以完成此任務嗎?這看起來不像是一個'怪異'的要求。

+0

只是好奇,但你爲什麼要儲存了在不同的組件與一個數據庫實體?這並不是說這不是一個有效的問題,但又只是好奇而已。 –

+0

您使用哪種版本的FNH?我記得有關於多個映射程序集的錯誤/限制 – Firo

+0

@Cole W.因此,成員和角色實體對象來自與靜態數據庫類相同的程序集,但仍需要.AddAssemblyOf ()調用。然後將其打包成一個.dll文件,並在我決定製定的新解決方案中引用。除了調用.AddAssemblyOf ()以將映射Member和Role對象到我的新解決方案數據庫之外,我還需要映射這個新解決方案中涉及的唯一實體對象;在上述成員和角色對象中,它們不在同一個程序集中。 – user407356

回答

1

老問題,但我設法解決它後看這個,所以我會盡力回答它。這是我如何做(的.ForEach()是從NHibernate.Linq擴展名):

config.Mappings(m => MappingAssemblies.ForEach(a => m.FluentMappings.AddFromAssembly(a))) 

我不得不這樣做自動映射的東西爲好,且有語法是有點不同。我有這標誌着我要自動映射所有類接口:

config.Mappings(m => 
    m.AutoMappings.Add(AutoMap.Assemblies(MappingAssemblies.ToArray()) 
    .Where(x => x.GetInterfaces().Contains(typeof(IAutoMappedEntity))))) 

另外,我沒有「MappingAssemblies」我手動設置,我注意到剛纔包括我的所有組件的偷懶的辦法,所以我配置看起來像這樣(使用SQLite,這是一個測試項目):

var MappingAssemblies = AppDomain.CurrentDomain.GetAssemblies() 
    .Where(a => a.FullName.StartsWith("MyCompanyName.")); 
Configuration configuration; 

var sessionFactory = Fluently.Configure() 
    .Database(SQLiteConfiguration.Standard.InMemory()) 
    // This adds fluent mappings 
    .Mappings(m => MappingAssemblies.ForEach(a => m.FluentMappings.AddFromAssembly(a))) 
    // This adds automapped classes using specific configuration 
    .Mappings(m => m.AutoMappings.Add(AutoMap.Assemblies(new MyAutomapConfiguration(), MappingAssemblies))) 
    // This adds automapped classes that just use my magic interface 
    .Mappings(m => m.AutoMappings.Add(AutoMap.Assemblies(MappingAssemblies.ToArray()).Where(x => x.GetInterfaces().Contains(typeof(IAutoMappedEntity))))) 
    .ExposeConfiguration(cfg => configuration = cfg) 
    .BuildSessionFactory();