6

我在玩 Sharp Architecture Lite,它强调约定优于配置,并试图了解 NHibernate是如何ConventionModelMapper工作的。具体来说,我无法区分下面的 IsRootEntity 和 IsEntity 方法(顺便说一句,Entity是 Sharp Arch 附带的抽象类):

     internal static class Conventions
        {
        public static void WithConventions(this ConventionModelMapper mapper, Configuration configuration) {
                Type baseEntityType = typeof(Entity);

                mapper.IsEntity((type, declared) => IsEntity(type));
                mapper.IsRootEntity((type, declared) => baseEntityType.Equals(type.BaseType));

        public static bool IsEntity(Type type) {
                return typeof(Entity).IsAssignableFrom(type)
                       && typeof(Entity) != type
                       && !type.IsInterface;
            }
    }

我收集到该IsEntity方法用于告诉 NHibernate 哪些类有资格映射/持久化到数据库。但是,我一生都无法弄清楚该IsRootEntity方法的作用。周围的文档ConventionModelMapper非常稀少。

4

1 回答 1

1

如果你考虑这种情况:

class B : Entity { ... }
class A : B { ... }

映射它们时,A 和 B 都是实体(IsEntity 应该为它们返回 true),并且 NHibernate 会将 A 映射为 B 的子类。但是,Entity 本身不应该被映射,因为它是所有实体的基类(通常你不希望映射此基类),因此 IsRootEntity 将为 Entity 返回 true,为其所有子类返回 false - 因此表明不应映射 Entity,因为它是“根”类

于 2012-10-23T09:02:59.713 回答