Login

Username

Password





Register   Reset password

This is a read-only archive of the forum.

# dagda1 5/22/2006 10:38 AM
Hi all,

I am developing a custom module and I am trying to utilise the generic methods in CommonDao. One thing that I don't seem to be able to get my head round is that when I call the GetObjectByIdmethod like this:

public Asset Load(int id)
{
return (Asset)_commonDao.GetObjectById(typeof(Asset), id);
}

The type returned is not of type Asset as i would expect but of the rather cryptically named NHibernate type:

CProxyTypeAssetdomain_INHibernateProxy_ISerializable2

I have tried casting it and I cannot use the is operator to determine it's type.

Can anyone tell me what is going on behind the scenes??

Thanks

Paul
# martijnb 5/22/2006 3:17 PM
NHibernate is returning a proxy of your Asset.
This is a subclass of your Asset, so I'd expect that casting shouldn't be a problem. What if you cast it with the 'as' construction?
return _commonDao.GetObjectById(typeof(Asset), id) as Asset;

Btw, NHibernate doesn't generate proxies if you don't mark your classes as lazy or with the proxy attribute, so that's maybe another option (that is, if you don't need lazy loading of many-to-one relations).
# dagda1 5/25/2006 12:50 AM
Hi Martin,

The cast does not work, it still comes out as type CProxyTypeAssetdomain_INHibernateProxy_ISerializable2. My code does some type checking e.g.

if (_asset is ocd.ImageAsset)
Display((ocd.ImageAsset)_asset);
else if (_asset is ocd.FlashAsset)
Display((ocd.FlashAsset)_asset)

As the type is CProxyTypeAssetdomain_INHibernateProxy_ISerializable2 this code will not run. I am struggling to see the point of this proxy class and why I cannot re-cast it.

Maybe these questions belong more in an NHibernate group. Can you explain why I would want to use a proxy?

Thanks

Paul
# martijnb 5/25/2006 4:35 AM
dagda1Maybe these questions belong more in an NHibernate group.

Well, yes perhaps that's a better place.
Can you explain why I would want to use a proxy?

I don't think proxy is the default, so it must be defined somewhere in your mapping files.
You'd want to use proxies in scenarios where you want to lazy load related many-to-one objects (for example: OrderItem -> Product where the Product is a proxy because you might not know if you need the Product at all).