-->
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.  [ 7 posts ] 
Author Message
 Post subject: Detecting Proxy access to properties
PostPosted: Tue Apr 25, 2006 10:54 am 
Beginner
Beginner

Joined: Wed Oct 05, 2005 5:35 am
Posts: 47
Location: France
Guys, is there an elegant way to distinguish when a property is accessed by a "real" class or by a proxy?
E.g.: say I want to implement some checks or some special behaviour on a getter of my class. But I don't want to trigger this behaviour when the property get's proxied.

Like:

Code:

Get
       If  <!!!this is not a Proxy!!!>  Then
            Validate(Me, "_notes")
        End If
        Return _notes
End Get


OK, I could check the Assembly name and figure out that it doesn't belong to my current one - hense, it's a proxy. But that's a bit ugly, no?
Could Proxy have some attribute saying "Hallo, I am a Proxy!" or something?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 5:35 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Hmmm. No answers here, but maybe there's a static method somewhere that you can pass an object which will return a boolean result indicating whether or not the object is a proxy...

Another possibility is that proxies might implement an interface like INHibernateProxy and you could do something like this (C#, sorry):

Code:
if(! typeof(INHibernateProxy).IsAssignableFrom(this.GetType()))
{
  //I'm not a proxy, so validate me!
}


Just a thought.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 5:43 pm 
Beginner
Beginner

Joined: Wed Oct 05, 2005 5:35 am
Posts: 47
Location: France
Thanks Symon,

I did think of "typeof(INHibernateProxy)" - but my objects don't know what it is :) - they don't reference NH directly.
Ended up with:
Code:
        Friend Overridable Function IsProxy() As Boolean
            Return Me.GetType.Name.Contains("INHibernateProxy")

        End Function


in BusinessBase class, so I can do a IsProxy check everywhere. A bit ugly, but seems to work.

Alex


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 7:10 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
that will slow you down considerably. You should try to avoid using reflection on properties because they are used so often.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 8:58 am 
Beginner
Beginner

Joined: Wed Oct 05, 2005 5:35 am
Posts: 47
Location: France
Yes... That's why I was looking for a "elegant" solution.
But if the choice is between reflection and having reference to NH in business objects just for that - I guess I'd rather go with reflection.

OR - maybe I am missing something obvious (-secret hope).
Thx The Shark


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 10:04 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
[damn, i pressed the wrong button, maybe double posting]

Then do it in the setter of the property and only when needed. so:


Code:
private bool isPropertyXProxy = true;
public int PropertyX
{
    get {return propertyX; }   // nice and fast
    set { if(isPropertyXProxy)  // check if it is still so
             {
                  isPropertyXProxy = this.gettype().equals(typeof(myClass));
              }
             propertyX = value;
}


This way, once the property is set to be not a proxy, you loose the reflection hit.
Now you can change the getter to do:
if(!isPropertyXProxy)
ValidateTheHellOutOfMe();

Let me know if this solution would make you happy.

You can also store the booleans in a list, if you don't like all the loose boolean-fields.
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 10:27 am 
Beginner
Beginner

Joined: Wed Oct 05, 2005 5:35 am
Posts: 47
Location: France
Txh again - will give a shot!


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