NHibernate:获取所引用抽象实体的具体类型 [英] NHibernate: Get concrete type of referenced abstract entity
问题描述
我有以下课程:
public abstract class FooBase
{
public virtual Guid Id { get; set; }
}
public class FooTypeA : FooBase
{
public virtual string TypeAStuff { get; set; }
}
public class Bar
{
public virtual Guid Id { get; set; }
public virtual FooBase Foo { get; }
}
FooBase和FooTypeA使用每类表层次结构模式进行映射. 条形图是这样映射的:
FooBase and FooTypeA are mapped using the table-per-class-heirarchy pattern. Bar is mapped like this:
public class BarDbMap : ClassMap<Bar>
{
public BarDbMap()
{
Id(x => x.Id);
References(x => x.Foo)
.LazyLoad();
}
}
因此,当我加载Bar时,其Foo属性只是一个代理.
So when I load a Bar, its Foo property is only a proxy.
如何获取Foo的子类类型(即FooTypeA)?
How do I get the subclass type of Foo (i.e. FooTypeA)?
我已经阅读了很多NH文档和论坛帖子.他们描述了获取父类型而不是子类的工作方式.
I have read through a lot of NH docs and forum posts. They describe ways of getting that work for getting the parent type, but not the subclass.
如果我尝试取消代理,则会收到类似以下错误: object是FooBase的未初始化代理
If I try to unproxy the class, I receive errors like: object was an uninitialized proxy for FooBase
推荐答案
我研究出如何避免收到的异常.这是取消代理FooBase的方法:
I worked out how to avoid the exception I was receiving. Here is a method that unproxies FooBase:
public static T Unproxy<T>(this T obj, ISession session)
{
if (!NHibernateUtil.IsInitialized(obj))
{
NHibernateUtil.Initialize(obj);
}
if (obj is INHibernateProxy)
{
return (T) session.GetSessionImplementation().PersistenceContext.Unproxy(obj);
}
return obj;
}
这篇关于NHibernate:获取所引用抽象实体的具体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!