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.