Mit*_*h A 6 nhibernate nhibernate-mapping convention-over-configur sharp-architecture
我正在玩Sharp Architecture Lite,它强调约定优于配置,并试图理解NHibernate的ConventionModelMapper工作原理.具体来说,我无法区分下面的IsRootEntity和IsEntity方法(BTW,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;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道该IsEntity方法用于告诉NHibernate哪些类符合数据库的映射/持久性.但是,我不能为我的生活弄清楚IsRootEntity方法的作用.周围的文档ConventionModelMapper非常稀少.
如果你考虑一下这种情况:
class B : Entity { ... }
class A : B { ... }
Run Code Online (Sandbox Code Playgroud)
映射它们时,A 和 B 都是实体(IsEntity 应该为它们返回 true),并且 NHibernate 会将 A 映射为 B 的子类。但是,Entity 本身不应该被映射,因为它是所有实体的基类(通常是您不希望映射此基类),因此 IsRootEntity 将为 Entity 返回 true,为其所有子类返回 false - 从而表明 Entity 不应被映射,因为它是“根”类
| 归档时间: |
|
| 查看次数: |
1052 次 |
| 最近记录: |