什么相当于 EF Core 6 中的 NHibernateProxyHelper.GetClassWithoutInitializingProxy?
What would be equivalent to NHibernateProxyHelper.GetClassWithoutInitializingProxy in EF Core 6?
我正在尝试了解领域驱动设计。我下面的示例程序使用 NHibernate。它有一个 Entity
基础 class 如下:
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
//
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);
}
}
我对 NHibernate 很陌生。那么
NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);
做,我如何将其转换为 EF Core 6?
NHibernate 在实体之上创建代理 classes 并覆盖其中的所有非私有成员,并且 NHibernate 使用反射创建这些实体。
GetType
方法将 return 代理的类型,而不是底层实体的类型。因此,为了解决这个问题,我们引入了一个 GetRealType
方法,它会检索实体的真实类型,而不管它上面是否有代理。它利用了 NHibernate 库中的一种实用方法。
所以问题来了。这将如何转换为 EF Core 6?我只是猜测 EF Core 也会生成类似于 NHibernate 的代理。那么什么相当于
NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);
我正在看NHibernateProxyHelper.cs
和 INHibernateProxy.cs 但我无法理解此方法的作用。
EF-core 等价于
Type GetRealType(object obj)
{
var typ = obj.GetType();
return obj is Microsoft.EntityFrameworkCore.Proxies.Internal.IProxyLazyLoader
? typ.BaseType
: typ;
}
IProxyLazyLoader 的 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.
我正在尝试了解领域驱动设计。我下面的示例程序使用 NHibernate。它有一个 Entity
基础 class 如下:
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
//
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);
}
}
我对 NHibernate 很陌生。那么
NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);
做,我如何将其转换为 EF Core 6?
NHibernate 在实体之上创建代理 classes 并覆盖其中的所有非私有成员,并且 NHibernate 使用反射创建这些实体。
GetType
方法将 return 代理的类型,而不是底层实体的类型。因此,为了解决这个问题,我们引入了一个 GetRealType
方法,它会检索实体的真实类型,而不管它上面是否有代理。它利用了 NHibernate 库中的一种实用方法。
所以问题来了。这将如何转换为 EF Core 6?我只是猜测 EF Core 也会生成类似于 NHibernate 的代理。那么什么相当于
NHibernateProxyHelper.GetClassWithoutInitializingProxy(this);
我正在看NHibernateProxyHelper.cs 和 INHibernateProxy.cs 但我无法理解此方法的作用。
EF-core 等价于
Type GetRealType(object obj)
{
var typ = obj.GetType();
return obj is Microsoft.EntityFrameworkCore.Proxies.Internal.IProxyLazyLoader
? typ.BaseType
: typ;
}
IProxyLazyLoader 的 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.