【问题标题】:Fluent Nhibernate ClassMaps and Column OrderFluent Nhibernate ClassMaps 和列顺序
【发布时间】:2010-09-17 13:33:16
【问题描述】:

我们的实体有一组共同的属性。为了减少重复映射,我创建了一个映射身份和公共属性的基本 ClassMap。对于每个实体的 ClassMap,我只是将基础子类化,它工作得很好。对于一个新项目,我们还让 NH 为我们生成数据库模式。问题是,列的顺序是基类映射中的属性首先出现,然后是子类中映射的任何内容。此构建的要求是列以特定顺序出现。

为了解决这个问题,我做了以下操作。

public class BaseMap<T> : ClassMap<T> where T : Entity
{
    public BaseMap()
    {
        Id(x => x.Id);
        MapEntity();
        Map(x => x.CommonProperty1);
        Map(x => x.CommonProperty2);
        Map(x => x.CommonProperty3);
    }

    protected virtual void MapEntity()
    {
    }
}

public class SomeEntityMap : BaseMap<SomeEntity>
{
    public SomeEntity()
    {
        base.MapEntity();
    }

    protected override void MapEntity()
    {
        Map(x => x.SomeEntityProperty1);
        Map(x => x.SomeEntityProperty2);
        Map(x => x.SomeEntityProperty3);        
    }
}

这行得通,但感觉就像一个 hack。除了 hack 因素,这里还有什么问题吗?

【问题讨论】:

    标签: nhibernate fluent-nhibernate fluent


    【解决方案1】:

    如果您将基类和 map 方法抽象化,它会感觉不那么 hacky...

    public abstract class BaseMap<T> : ClassMap<T> where T : Entity
    {
        public BaseMap()
        {
            Id(x => x.Id);
            MapEntity();
            Map(x => x.CommonProperty1);
            Map(x => x.CommonProperty2);
            Map(x => x.CommonProperty3);
        }
    
        protected abstract void MapEntity();
    
    }
    
    public class SomeEntityMap : BaseMap<SomeEntity>
    {
        protected override void MapEntity()
        {
            Map(x => x.SomeEntityProperty1);
            Map(x => x.SomeEntityProperty2);
            Map(x => x.SomeEntityProperty3);        
        }
    }
    

    这会将公共属性列保留在表的末尾。请注意,在这些之后仍会添加外键列。我认为没有任何方法可以完全控制 w/fluent 的列顺序,除非您手动修改创建模式脚本。

    【讨论】:

      【解决方案2】:

      我只需要自己实现类似的东西。

      假设你有

      public class SomeEntity : Entity
      { 
          ...
      }
      

      一种不那么“黑客”的方式是:

      public abstract class BaseMap<T> : ClassMap<T> where T : Entity
      {
          public BaseMap()
          {
              Id(x => x.Id);
              Map(x => x.CommonProperty1);
              Map(x => x.CommonProperty2);
              Map(x => x.CommonProperty3);
          }
      }
      
      public class SomeEntityMap : BaseMap<SomeEntity>
      {
          public SomeEntity()
          {
              Map(x => x.SomeEntityProperty1);
              Map(x => x.SomeEntityProperty2);
              Map(x => x.SomeEntityProperty3);        
          }
      }
      

      最终结果相同,但您没有使用覆盖的方法来添加映射。它会自动处理。

      【讨论】:

      • ??... SomeEntityMap 甚至不会包含 CommonPropertyX 属性,更不用说 OP 所说的顺序了。
      • 哈哈。好叫Yogesh。我让 SomeEntityMap 继承 ClassMap,而它应该是 BaseMap。通过此更改,它确实包含了常见属性。
      • +1 没错。我也不明白为什么OP首先使用虚拟方法。
      • @Groo 因为他想让基类的列派生类中没有用构造函数实现的列之后
      猜你喜欢
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 2010-11-29
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多