代码之家  ›  专栏  ›  技术社区  ›  gabe

在Fluent NHibernate automapping中,有没有办法通过命名空间忽略基类型/类?

  •  0
  • gabe  · 技术社区  · 14 年前

    目前,我使用以下代码来忽略Fluent NHibernate自动映射的基类型:

    AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
                .Conventions.Setup(GetConventions())
                .IgnoreBase<MyCore.BaseTypes.AmazingBaseType>()
                .IgnoreBase<MyCore.BaseTypes.AnotherAmazingBaseType>()
                .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
    

    MyCore.BaseTypes IgnoreBase()

    我试着用我的超控 ShouldMap(Type) 中的方法 DefaultAutomappingConfiguration -扩展类(即。 MyDefaultAutomappingConfiguration

    public class MyDefaultAutomappingConfiguration: DefaultAutomappingConfiguration {
        public override bool ShouldMap(Type type) {
            return !IsBaseType(type);
        }
    
        private bool IsBaseType(Type type) {
            return type.Namespace == typeof(MyCore.BaseTypes).Namespace;
        }
    }
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   David R. Longnecker    14 年前

    你的工作是什么 MyAutomappingConfiguration 看起来像?您可以覆盖 ShouldMap(Type) 比如:

    public override bool ShouldMap(System.Type type)
    {
      return 
        type.BaseType.Namespace == typeof (MyCore.BaseTypes.BaseIWant).Namespace;
    }
    
        2
  •  1
  •   Phill    14 年前

    如果您的基类型是抽象的,那么它们不会包含在映射中。

        3
  •  1
  •   Variant    14 年前

    您可以直接在您的配置中这样做:

    AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
                .Conventions.Setup(GetConventions())
                .Where(t => t.Namespace != "MyCore.BaseTypes")
                .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();