【问题标题】:Fluent Nhibernate - ClassMaps in multiple, separate assembliesFluent Nhibernate - 多个单独程序集中的 ClassMaps
【发布时间】:2011-12-03 23:44:24
【问题描述】:

考虑以下 Fluent 配置;

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);

所以我的想法是我已经将我的 Member 和 Role 实体对象打包到与 Database helper 对象相同的程序集中,这样我关心创建的任何解决方案都可以立即获得我的标准化成员资格,并且能够简单地创建它的拥有解决方案特定的 ClassMap 并将它们添加到配置对象中。

问题是,熟悉的调用;

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

似乎只能处理单个程序集。添加到列表中的内容无关紧要,只会映射添加的最后一个程序集。作为上述方法的替代方法,我尝试引用 MappingConfiguration (这是 config.Mappings(m => ) 中的 'm' 代表),但这也不起作用。很明显,对m.FluentMappings.AddFromAssembly 或任何FluentMappings.Add 方法的调用将覆盖以前存在的内容,但肯定有办法做到这一点吗?这似乎不是一个“奇怪”的要求。

【问题讨论】:

  • 只是好奇,但为什么要将与一个数据库相关的实体存储在不同的程序集中?并不是说这不是一个有效的问题,而是再次好奇。
  • 您使用哪个版本的 FNH?我记得有一个关于多个映射程序集的错误/限制
  • @Cole W. 因此,成员和角色实体对象来自与静态数据库类相同的程序集,但仍需要 .AddAssemblyOf() 调用。然后将其打包成 .dll 并在我决定制作的新解决方案中引用。除了对 .AddAssemblyOf() 的调用之外,为了将成员和角色对象映射到我的新解决方案数据库,我还需要映射这个新解决方案中涉及的唯一实体对象;在上述 Member 和 Role 对象中,它们不会在同一个程序集中。
  • @Firo - 如果我右键单击 FlientNHibernate.dll 并查看“产品版本”,则显示为:1.3.0.717

标签: nhibernate fluent-nhibernate mapping


【解决方案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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 2011-02-09
    • 2011-02-02
    • 2012-06-12
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多