1

I am trying to understand domain-driven design. The example program I am following uses NHibernate. It has an Entity base class as follows:

public abstract class Entity
{
    public virtual long Id { get; protected set; }

    public override bool Equals(object obj)
    {
        var other = obj as Entity;

        if (ReferenceEquals(other, null))
            return false;

        if (ReferenceEquals(this, other))
            return true;

        if (GetRealType() != other.GetRealType())
            return false;

        if (Id == 0 || other.Id == 0)
            return false;

        return Id == other.Id;
    }

    public static bool operator ==(Entity a, Entity b)
    {
        if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
            return true;

        if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
            return false;

        return a.Equals(b);
    }

    public static bool operator !=(Entity a, Entity b)
    {
        return !(a == b);
    }

    // Finally, we also need to implement the GetHashCode method.
    // It's important for two objects which are equal to each other to always
    // generate the same hash code.
    // Here the hash code depends on the object's type and identifier,
    // which are the parts of the object's identity
    // https://stackoverflow.com/q/371328/1977871
    public override int GetHashCode()
    {
        return (GetRealType().ToString() + Id).GetHashCode();
    }

    private Type GetRealType()
    {
        // Here is the question. What would the equavalent of the following in EF Core? 
        return NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);
    }
} 

I am quite new to NHibernate. So what does

NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);

do and how can I translate that to EF Core 6 ?

NHibernate creates proxy classes on top of entities and overrides all non-private members in them, and NHibernate creates those entities using reflection.

GetType method will return the type of the proxy, not the type of the underlying entity. So to fix this, we introduce a GetRealType method, which would retrieve the real type of the entity regardless of whether there is a proxy on top of it. It makes use of one of the utility methods in the NHibernate library.

So here is the question. How will this translate with EF Core 6? I only guess that EF Core also generates proxy similar to NHibernate. So then what would be the equivalent of

NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);

I am taking a look at NHibernateProxyHelper.cs and INHibernateProxy.cs but I'm unable to understand what this method does.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
VivekDev
  • 20,868
  • 27
  • 132
  • 202

1 Answers1

1

An EF-core equivalent is

Type GetRealType(object obj)
{
    var typ = obj.GetType();
    return obj is Microsoft.EntityFrameworkCore.Proxies.Internal.IProxyLazyLoader
        ? typ.BaseType
        : typ;
}

Disclaimer in IProxyLazyLoader's header:

You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

VivekDev
  • 20,868
  • 27
  • 132
  • 202
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291