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.  [ 4 posts ] 
Author Message
 Post subject: Proxy - setting fields through reflection
PostPosted: Tue Jun 12, 2007 9:45 am 
Beginner
Beginner

Joined: Tue Mar 27, 2007 4:54 am
Posts: 47
If I have this simple class...

public class Foo
{
[id-stuff]

private int _bar;
public virtual int Bar
{
get
{
return _bar;
}
}
}

...and map it something like this...
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="xxx"
namespace="xxx"
default-access="field.camelcase-underscore">
<class name="Foo">
[id-stuff]
<property name="Foo" />
</class>
</hibernate-mapping>

...a proxy default will be created (I'm using version 1.2 where lazy loading is on by default) when recreating the entity from datasource. Fine. However - I'm a bit confused what happen if I set the _bar field using reflection in a Foo instance? These changes cannot be seen in its property. I can see the reason behind it, while not working towards a Foo instance but a proxy and this proxy obviously don't pick up changes to the underlying field at run-time.

I've tried to experiment in what situation this will lead to unwanted behaviour? Is setting the (private) field a no-no when working with proxies? Or will the changes always be included when later persisted?
What is the best practise here? If you want a property with no setter but in some rare situation you still need to be able to change it - what to do using nhib + lazy loading?

Any ideas?
Thanks, Roger


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 4:37 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Have you tried using a protected setter and then setting the property through reflection instead of the field? That way the proxy's setter should get called ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 7:41 am 
Beginner
Beginner

Joined: Tue Mar 27, 2007 4:54 am
Posts: 47
Absolutly, that will work. I'm a bit uncertain though when you can trust the "reflections directly against fields" will work or not.


Top
 Profile  
 
 Post subject: Re: Proxy - setting fields through reflection
PostPosted: Thu Jun 14, 2007 7:55 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
rogermoore wrote:
I've tried to experiment in what situation this will lead to unwanted behaviour? Is setting the (private) field a no-no when working with proxies? Or will the changes always be included when later persisted?
What is the best practise here? If you want a property with no setter but in some rare situation you still need to be able to change it - what to do using nhib + lazy loading?

Any ideas?
Thanks, Roger


I assume the issue isn't the persistence of the value, but having the proxy know that the object needs to be lazy loaded if you just have a proxy? I believe it will work fine if the object is already loaded.

That being said, I typically take a different approach. Instead of using a field accessor, I use an explicitly implemented interface. The main property only has a getter, but a property of the same name exists on the interface as well which happens to have a setter. NHibernate will automatically first check to see if there is a property which exists for the name you gave it, if there is no getter or setter for that property name it checks all explicitly implemented interfaces and uses the getter or setter of that interface if the property name matches. It's very simple to use, and works well.

Then within your code you could actually cast your object as the interface you are using and access the property that way. This way you could change the value of your "read-only" property without using reflection. Normal action won't be to cast your object as an interface to set a value, so typical usage should still be read-only. It's a little bit more work, but overall seems a little clearer to me. That being said I also understand the argument of cluttering up the objects with basically unnecessary interfaces.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.