These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: convert proxy to object
PostPosted: Fri Feb 22, 2008 12:17 pm 
Newbie

Joined: Fri Feb 22, 2008 12:05 pm
Posts: 3
Hi,

I am using Nhibernate 1.2.1 v with oracle 10g DB and .NET framework 2.0.

My question is basically to get the actual object from a proxy. My scenario is like this:

Class A
{
B b;
public virtual B prop
{
get { return b;}
set { b=value;}
}
}

Class B
{

}

Class C: B
{

}

The mapping file for class A has lazy= true @ class level. And a many-to-one with class B. When i try to access A.prop, it returns me the proxy (which is fine). But how do i cast it to the actual object , which could be a object of B or C (since C inherits from B).
Any help will be highly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 1:46 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
I'm a little bit confused by your question. You already did cast your object as your real type. The proxy type inherits your class's type and then throughout the application you reference it with your type. It's not actually possible to reference it as the proxy since that proxy type is generated at run-time.

If you mean that you want to obtain the inner type within the proxy, one that does not add the proxy behaviors (possibly for type checking with Inheritance), then the only way I know to do is to do the same thing the NHibernate's SessionImpl.UnProxy method does it. All classes it uses are public, so you could put that same implementation in your code, but I would probably advice against it.

Ideally, come up with a way to work around whatever it is you need this for. Maybe you just shouldn't set this object to lazy if it matters that much?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 2:18 pm 
Newbie

Joined: Fri Feb 22, 2008 12:05 pm
Posts: 3
Thank you for the reply.

The issue is that type returned by the A.prop property supprts the B interface but the actual type is actualy a C. In the application, I want to test the returned object to see if it's a C and then down cast it so that I can call me methods/properties that are unique to the C class. By returning proxy objects to the applciation, this becomes impossible.

You have suggested using the UnProxy method to convert the returned proxy object into the actual instance, but advised against using it. Can you explain why?

This has been a long-standing problem in nhib 1.0.x and we were hoping 1.2 addressed it. Using proxy objects helps make the lazy loading work but its very bad when the application has to code around aspects where the implementation details of the persistence framework is exposed to the client of the model.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 2:24 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
You can't use the UnProxy method. It's a private method on the SessionImpl class. However, the classes and methods which it uses internally are all public.

I say not to use it because it is intended to be functionality which is private to the framework, and there is no guarantee that the use of it's internals won't break with a future release.

I would really recommend setting lazy="false" if you are trying to determine specific types. Most likely loading a few many-to-one properties will not noticeably impact your performance. With proper indexes it should really not cause an issue at all. This may be one of those items where we are using premature optimization. Any chance you could change the lazy setting so no proxy would be used?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 6:37 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
If you want to do type comparisons you can get the actual type of an entity by passing the proxy to NHibernateUtil.GetClass which will then return the actual type of the proxied object. Once you know the type you should be able to cast the proxied object to that type since the proxy is necessarily a subclass of that type.

Note, however that this will perforce hydrate the object if the proxy hasn't been initialized.

Cheers,

Symon.

_________________
Symon Rottem
http://blog.symbiotic-development.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.