15

I have a base class DomainObject for all my business objects I am using with NHibernate. It contains the Id property.

public abstract class DomainObject
{
    public virtual int Id { get; private set; }
}

I would like to write an IEqualityComparer to compare my domain objects. If two objects have the same Id and are the same kind of object they should be equal. However when I use GetType() to get the type of the object, it will return the NHibernate proxy type. So this code:

bool IEqualityComparer.Equals(object x, object y)
{
    // null checking code skipped here
    if(x is DomainObject && y is DomainObject)
    {
            return ((DomainObject) x).Id == ((DomainObject) y).Id
                    && x.GetType() == y.GetType();
    }
    return x.Equals(y);
}

Doesn't work correctly, because the type of x is Asset but the type of y is AssetProxy21879bba3e9e47edbbdc2a546445c657.

So, how do I get the entity type on an object that may be a NHibernate proxy object? i.e. in the example above Asset instead of AssetProxy21879bba3e9e47edbbdc2a546445c657?

Jeff Walker Code Ranger
  • 4,634
  • 1
  • 43
  • 62

4 Answers4

23

You can get the real type of a proxy with:

NHibernateUtil.GetClass(x);

or you can add a method to DomainObject like:

public virtual Type GetTypeUnproxied()
{
    return GetType();
}

Which is really slick and doesn't depend directly on NHibernate.

Alternatively, one can approach the problem by saying you need to get the true object, rather than the proxy, which, if the session is handy, can be done with:

session.PersistenceContext.Unproxy(x);

As mentioned in another answer, if you're trying to implement equals it would be a good idea to check out the Sharp architecture implementation of Equals.

Jeff Walker Code Ranger
  • 4,634
  • 1
  • 43
  • 62
  • 1
    Note that if you're using Castle Windsor the GetClass might return the Proxy type. See this thread for another way to solve it: http://stackoverflow.com/a/1420816/348841 – Jonas Stensved Feb 10 '12 at 15:46
2

To get real object instead of proxy you can use

session.PersistenceContext.Unproxy(proxyObject)

But I think you should look at Sharp architecture implementation for Equals.

Sly
  • 15,046
  • 12
  • 60
  • 89
  • It should be noted that he's probably more interested in the GetTypeUnproxied() method in http://github.com/codai/Sharp-Architecture/blob/master/src/SharpArch/SharpArch.Core/DomainModel/BaseObject.cs - but +1 for a good reference impl. – DanP Sep 21 '10 at 10:13
  • In this case I don't actually have the session available to me in this code. Thanks for the reference to Equals in Sharp architecture, it doesn't apply to me becuase I am writing an equals for tests so it doesn't have to handle all the cases, but it was still a good reference. And thanks Dan to the link to the GetTypeUnproxied() code, that is more what I am interested in. – Jeff Walker Code Ranger Sep 21 '10 at 19:11
0

You can implement a backdoor property as described here to get the actual nonproxied instance.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
-1

i took a different aproach in a production project. I simply have a global HiLow Id-Generator which generates Id's unique for all types then i can:

// in DomainEntity
public override bool Equals(object obj)
{
    var other = obj as DomainEntity;
    if (Id == 0) // IsTransient()
        return ReferenceEquals(this, other);
    else
        return (other != null) && (Id == other.Id);
}

// Comparer
bool IEqualityComparer.Equals(object x, object y)
{
    return object.Equals(x, y);
}
Firo
  • 30,626
  • 4
  • 55
  • 94