Commit e6f8f8b9 authored by 阿星Plus's avatar 阿星Plus

Entities

parent c9b20d97
using System;
using System.Collections.Generic;
namespace Plus.Domain.Entities
{
[Serializable]
public abstract class Entity : Entity<int>, IEntity
{
}
[Serializable]
public abstract class Entity<TPrimaryKey> : IEntity<TPrimaryKey>
{
public virtual TPrimaryKey Id { get; set; }
public virtual bool IsTransient()
{
return EqualityComparer<TPrimaryKey>.Default.Equals(Id, default);
}
public override bool Equals(object obj)
{
if (obj == null || !(obj is Entity<TPrimaryKey>))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
Entity<TPrimaryKey> entity = (Entity<TPrimaryKey>)obj;
if (IsTransient() && entity.IsTransient())
{
return false;
}
Type type = GetType();
Type type2 = entity.GetType();
if (!type.IsAssignableFrom(type2) && !type2.IsAssignableFrom(type))
{
return false;
}
return Id.Equals(entity.Id);
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
public static bool operator ==(Entity<TPrimaryKey> left, Entity<TPrimaryKey> right)
{
if (Equals(left, null))
{
return Equals(right, null);
}
return left.Equals(right);
}
public static bool operator !=(Entity<TPrimaryKey> left, Entity<TPrimaryKey> right)
{
return !(left == right);
}
public override string ToString()
{
return $"[{GetType().Name} {Id}]";
}
}
}
\ No newline at end of file
namespace Plus.Domain.Entities
{
public interface IEntity
public interface IEntity : IEntity<int>
{
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment