使用NHibernate的表每个子类的继承策略时,与Casting代理问题 [英] Issue with Casting proxies when using NHibernate table per subclass inheritance strategy

查看:154
本文介绍了使用NHibernate的表每个子类的继承策略时,与Casting代理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象基类并继承了poco实体。我使用流利的nhibernate 1.1自动映射使用表每个子类的继承。

类继承如下所示:

节点(抽象类)
位置:节点
资产:节点

节点类基本上是一个树结构。

  public abstract class Node 
{
public virtual int Id
{
get;
set;
}

公共虚拟节点ParentNode
{
get;
set;
}

public virtual ICollection< Node> ChildNodes
{
get;
set;


$ / code $ / pre

现在问题是当我得到一个资产对象从数据库和做
objAsset.ParentNode,我可以将父节点转换为资产或地方,但是如果我做类似objAsset.ParentNode.ParentNode.ParentNode,那么ParentNode显示为类型{Castle.Proxies.NodeProxy} 我无法将它投到地方或资产。

我知道在我的情况下,最终的结果应该是类型的地方,作为记录存在数据库在地方表,但由于某种原因,NHibernate不能延迟加载到实际的派生类型,它只能转换为基类节点。



有什么想法吗? >

等待
Nabeel

解决方案

http://ayende.com/Blog/archive/2009/09/03/answer-the-lazy-loaded-inheritance-一对一关联-orm.aspx

简而言之,代理从关联类型继承,而不是继承类型,因为在去数据库之前是不知道的(而且你不能改变对象的类型)

更新:可能的解决方法是泄漏对Node中的实际对象的引用:

preublic $ {public $ {$ {$ {return this; }}

这会为您提供相关的资产或地点。小心一点。


I have got an abstract base class and inherited poco entities. I am using table per subclass inheritance with fluent nhibernate 1.1 automapping.

The class inheritance looks like follows

Node (abstract class) Place : Node Asset : Node

Node class is basically a tree structure.

 public abstract class Node 
    {
        public virtual int Id
        {
            get;
            set;
        }

        public virtual Node ParentNode
        {
            get;
            set;
        }

        public virtual ICollection<Node> ChildNodes
        {
            get;
            set;
        }
}

Now the problem is when I get an object of Asset from database and do objAsset.ParentNode, I can cast ParentNode to Asset or Place, but if I do something like objAsset.ParentNode.ParentNode.ParentNode, then the ParentNode appears as of type "{Castle.Proxies.NodeProxy}" and I am unable to cast it to Place or Asset.

I know that in my case the end result should be of type Place, as the record exists in the database in Place table but for some reason NHibernate can't lazy load it to the actual derived type, it is only castable to base class Node.

Any idea?

Awaiting Nabeel

解决方案

This is well explained by Oren here: http://ayende.com/Blog/archive/2009/09/03/answer-the-lazy-loaded-inheritance-many-to-one-association-orm.aspx

In a nutshell, proxies inherit from the type of the association, not the inherited type, because that is not known before going to the DB (and you can't change the type of an object)

Update: a possible workaround is leaking a reference to the actual object in Node:

public virtual Node Self { get { return this; } }

That will give you the underlying Asset or Place. Be careful with it.

这篇关于使用NHibernate的表每个子类的继承策略时,与Casting代理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆