我在玩 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/