-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem: Interface as proxy with access="field"
PostPosted: Thu Oct 27, 2005 11:33 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Hi,

I've been having some trouble with using an Interface as a proxy when I have a property in the implementation where there is no setter. I raised this problem before in the following post but haven't been able to resolve it as yet: http://nhibernate.sourceforge.net/forum/viewtopic.php?t=751

I have many classes that I want to have properties which only implement a getter and have NHibernate use field access only so that it's not subject to any of the property restrictions. I also need to be able to implement Interfaces because I have an inheritance problem that can't be solved without without using them.

To paraphrase from the Hibernate docs in section 14.2:

Quote:
There are some gotchas to be aware of when extending this approach to polymorphic classes...instances of Cat will never be castable to DomesticCat, even if the underlying instance is an instance of DomesticCat. If you wish to avoid these problems your persistent classes must each implement an interface that declares its business methods.


Since that thread I've done some trawling on the Hibernate site to see whether anyone over there had a solution and it looks a bit like someone has: http://forum.hibernate.org/viewtopic.php?t=937087

The problem is that I can't quite work out how this solution can be implemented in NHibernate...the structure of C# classes is slightly different with regards to properties and I'm really floundering around here.

Does anyone out there have any thoughts on what I need to do to resolve this issue?

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 6:12 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
If anyone is interested at this late date I used the following solution as per the post I referenced in my last post:

Code:
   public class InterfaceAccessor : IPropertyAccessor
   {
      private static BasicPropertyAccessor _basicFactory = new BasicPropertyAccessor();
      private static FieldAccessor _directFactory = new FieldAccessor();

      #region IPropertyAccessor Members

      public ISetter GetSetter(Type theClass, string propertyName)
      {
         return new DeferredSetter(theClass, propertyName);
      }

      public NHibernate.Property.IGetter GetGetter(Type theClass, string propertyName)
      {
         try
         {
            return _basicFactory.GetGetter(theClass, propertyName);
         }
         catch(Exception e)
         {
            Debug.WriteLine("An exception was thrown here! " + e.Message + e.StackTrace);
            throw;
         }
      }

      #endregion

      private class DeferredSetter : NHibernate.Property.ISetter
      {
         private Type      _theClass;
         private string      _propertyName;
         private   ISetter      _theDelegate;

         public DeferredSetter(Type theClass, string propertyName)
         {
            _theClass = theClass;
            _propertyName = propertyName.Substring(0,1).ToLower() + propertyName.Substring(1);
         }

         #region ISetter Members

         public System.Reflection.PropertyInfo Property
         {
            get
            {
               // TODO:  Add DeferredSetter.Property getter implementation
               return null;
            }
         }

         public void Set(object target, object value)
         {
            if(_theDelegate == null)
               _theDelegate = directFactory.GetSetter(target.GetType(), _propertyName);

            _theDelegate.Set(target, value);
         }

         public string PropertyName
         {
            get
            {
               // TODO:  Add DeferredSetter.PropertyName getter implementation
               return null;
            }
         }

         #endregion

      }

   }


It's not compeltely finished, but it works for me.

Symon.


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