Nhibernate与实体基类
我试图实现抽象的基本实体类,它覆盖了equals和GetHashcode…这是我的实体基类Nhibernate与实体基类,nhibernate,Nhibernate,我试图实现抽象的基本实体类,它覆盖了equals和GetHashcode…这是我的实体基类 public abstract class Entity<TId> { public virtual TId Id { get; protected set; } protected virtual int Version { get; set; } public override bool Equals(object obj) { return Equals(obj as Entit
public abstract class Entity<TId>
{
public virtual TId Id { get; protected set; }
protected virtual int Version { get; 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(other) &&
Equals(Id, other.Id))
{
var otherType = other.GetUnproxiedType();
var thisType = GetUnproxiedType();
return thisType.IsAssignableFrom(otherType) ||
otherType.IsAssignableFrom(thisType);
}
return false;
}
public override int GetHashCode()
{
if (Equals(Id, default(TId)))
return base.GetHashCode();
return Id.GetHashCode();
}
我对如何设置基类的ID属性有点困惑。任何人都可以就此向我提出建议,我将非常感谢任何帮助。您只需要将类型传递给继承的基类 请参见实体中的注释:
public class Product : Entity<Guid>
{
// The ProductId property is no longer needed as the
// Id property on the base class will be of type Guid
// and can serve as the Id
//public virtual Guid ProductId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual Decimal UnitPrice { get; set; }
}
public class Customer : Entity<int>
{
// The CustomerID property is no longer needed as the
// Id property on the base class will be of type int
// and can serve as the Id
// public virtual int CustomerID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int Age { get; set; }
}
公共类产品:实体
{
//ProductId属性不再需要作为
//基类上的Id属性将为Guid类型
//可以作为身份证
//公共虚拟Guid ProductId{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟字符串描述{get;set;}
公共虚拟十进制单价{get;set;}
}
公共类客户:实体
{
//CustomerID属性不再需要作为
//基类上的Id属性的类型将为int
//可以作为身份证
//公共虚拟int CustomerID{get;set;}
公共虚拟字符串FirstName{get;set;}
公共虚拟字符串LastName{get;set;}
公共虚拟整数{get;set;}
}
现在,在NHibernate映射文件中,只需指定Id属性的数据库列
public class Product : Entity<Guid>
{
// The ProductId property is no longer needed as the
// Id property on the base class will be of type Guid
// and can serve as the Id
//public virtual Guid ProductId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual Decimal UnitPrice { get; set; }
}
public class Customer : Entity<int>
{
// The CustomerID property is no longer needed as the
// Id property on the base class will be of type int
// and can serve as the Id
// public virtual int CustomerID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int Age { get; set; }
}