2011-10-09 107 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();