0

我有对象层次结构 Parent->Child (延迟加载默认设置为 true) 现在我正在从数据库中加载所有 Parent 对象。所有子对象都将具有 ChildProxyGUID 类型。

然后我写

IList<Parent> parentList = NHibernateHelper.List<Parent>();
foreach(Parent parent in parentList)
{
  if(!NHibernateUtil.IsInitialized(parent.Child))
  {
    NHibernateUtil.Initialize(parent.Child);
    if(parent.Child.GetType() != typeof(Child)) //parent.Child.GetType() return me proxy type
      throw new ArgumentException("wrong type");
  }
}

如何将 parent.Child 转换为 Real 类型“Child”。由于系统检查,我需要真实类型(儿童)。这个例子在现实生活中很简单,我有一个非常复杂的映射和关系。

有什么想法吗?

4

1 回答 1

0

尝试:

var realObject = session.GetSessionImplementation()
                        .PersistenceContext.Unproxy(parent.Child)

但是,让您的代码依赖这种类型的检查是个坏主意,因为它违反了LSP,从而创建了更难维护的代码。

于 2010-08-01T22:48:12.907 回答