Proxies of entities that are mapped with inheritance are always just
proxies of the base class because NHibernate can't know which subclass
it is when it creates the proxy.

I don't know how GetUnproxiedType() exactly works but I know that it
only works if you are using Castle as proxy factory. So in case you
use any other proxy factory (or update to NHibernate 3.2 and use the
default proxy factory there), you should use a different method to get
the real type.

I'm using the following method:

public static Type GetUnproxiedType(Type type) {
         while (type.Assembly.IsDynamic && type.BaseType != null) {
            type = type.BaseType;
         }

         return type;
      }


On Apr 16, 4:46 pm, Can Gencer <[email protected]> wrote:
> In NHibernate 3.0 Cookbook, there is a sample implementation for a
> base Entity type. The equals is implemented like this:
>
> public abstract class Entity<TId>
> {
>   public virtual TId Id { get; protected set; }
>
>   public override bool Equals(object obj)
>   {
>     return Equals(obj as Entity<TId>);
>   }
>
>   private static bool IsTransient(Entity<TId> obj)
>   {
>      return obj != null && Equals(obj.Id, default(TId));
>   }
>
>   private Type GetUnproxiedType()
>   {
>      return GetType();
>   }
>
>   public virtual bool Equals(Entity<TId> other)
>   {
>     if (other == null) return false;
>     if (ReferenceEquals(this, other)) return true;
>
>     if (!IsTransient(this) && !IsTransient(this) && Equals(Id,
> other.Id))
>     {
>       var otherType = other.GetUnproxiedType();
>       var thisType = GetUnproxiedType();
>       return thisType.IsAssignableFrom(otherType) ||
>          otherType.IsAssignableFrom(thisType);
>     }
>     return false;
>   }
>
> }
>
> The reason for the GetUnproxiedType() method is this: There is an
> abstract base class Product, a concrete class Book which inherits from
> Product and a dynamic proxy class ProductProxy used by NHibernate for
> lazy loading. If a ProductProxy representing a Book and a concrete
> Book have the same Ids, they should be treated as equal. However I
> don't really see why calling GetType() on a ProductProxy instance
> should return Product in this case, and how it helps. Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to