NHibernate 约定 ModelMapper; IsRootEntity 和 IdEntity 有什么区别

标签 nhibernate nhibernate-mapping sharp-architecture convention-over-configur

我在玩 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 周围的文档稀少得可怜。

最佳答案

如果您考虑以下情况:

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

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

关于NHibernate 约定 ModelMapper; IsRootEntity 和 IdEntity 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13015547/

相关文章:

nhibernate - 使用 NHibernate ISqlExceptionConverter 的自定义异常

java - 使用继承时 Hibernate ManyToMany 不起作用

hibernate - NHibernate:(一或零)到(一或零)映射

c# - NHibernate:无法解析属性:empAlias

c# - NHibernate并发问题

nhibernate - Visual Studio .Net 中显示的 dll 版本不反射(reflect)引用的 dll 文件/产品版本

nhibernate - 使用 ActiveRecord 在 nHibernate 中急切加载延迟加载的实体

c# - NHibernate 父子关系和子只知道parentId(外键违规)

nhibernate - 从 NHibernate 的一级缓存中删除已删除的项目?或者: how to check if cached items have been deleted?

c# - 覆盖长文本字符串的流利 NHibernate nvarchar(MAX) 而不是 nvarchar(255)