| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using Abp.Extensions;
- namespace Abp.Domain.Entities
- {
- /// <summary>
- /// A shortcut of <see cref="Entity{TPrimaryKey}"/> for most used primary key type (<see cref="int"/>).
- /// </summary>
- [Serializable]
- public abstract class Entity : Entity<int>, IEntity
- {
- }
- /// <summary>
- /// Basic implementation of IEntity interface.
- /// An entity can inherit this class of directly implement to IEntity interface.
- /// </summary>
- /// <typeparam name="TPrimaryKey">Type of the primary key of the entity</typeparam>
- [Serializable]
- public abstract class Entity<TPrimaryKey> : IEntity<TPrimaryKey>
- {
- /// <summary>
- /// Unique identifier for this entity.
- /// </summary>
- public virtual TPrimaryKey Id { get; set; }
- /// <summary>
- /// Checks if this entity is transient (it has not an Id).
- /// </summary>
- /// <returns>True, if this entity is transient</returns>
- public virtual bool IsTransient()
- {
- if (EqualityComparer<TPrimaryKey>.Default.Equals(Id, default(TPrimaryKey)))
- {
- return true;
- }
- //Workaround for EF Core since it sets int/long to min value when attaching to dbcontext
- if (typeof(TPrimaryKey) == typeof(int))
- {
- return Convert.ToInt32(Id) <= 0;
- }
- if (typeof(TPrimaryKey) == typeof(long))
- {
- return Convert.ToInt64(Id) <= 0;
- }
- return false;
- }
- /// <inheritdoc/>
- public override bool Equals(object obj)
- {
- if (obj == null || !(obj is Entity<TPrimaryKey>))
- {
- return false;
- }
- //Same instances must be considered as equal
- if (ReferenceEquals(this, obj))
- {
- return true;
- }
- //Transient objects are not considered as equal
- var other = (Entity<TPrimaryKey>)obj;
- if (IsTransient() && other.IsTransient())
- {
- return false;
- }
- //Must have a IS-A relation of types or must be same type
- var typeOfThis = GetType();
- var typeOfOther = other.GetType();
- if (!typeOfThis.GetTypeInfo().IsAssignableFrom(typeOfOther) && !typeOfOther.GetTypeInfo().IsAssignableFrom(typeOfThis))
- {
- return false;
- }
- if (this is IMayHaveTenant && other is IMayHaveTenant &&
- this.As<IMayHaveTenant>().TenantId != other.As<IMayHaveTenant>().TenantId)
- {
- return false;
- }
- if (this is IMustHaveTenant && other is IMustHaveTenant &&
- this.As<IMustHaveTenant>().TenantId != other.As<IMustHaveTenant>().TenantId)
- {
- return false;
- }
- return Id.Equals(other.Id);
- }
- /// <inheritdoc/>
- public override int GetHashCode()
- {
- if (Id == null)
- {
- return 0;
- }
- return Id.GetHashCode();
- }
- /// <inheritdoc/>
- public static bool operator ==(Entity<TPrimaryKey> left, Entity<TPrimaryKey> right)
- {
- if (Equals(left, null))
- {
- return Equals(right, null);
- }
- return left.Equals(right);
- }
- /// <inheritdoc/>
- public static bool operator !=(Entity<TPrimaryKey> left, Entity<TPrimaryKey> right)
- {
- return !(left == right);
- }
- /// <inheritdoc/>
- public override string ToString()
- {
- return $"[{GetType().Name} {Id}]";
- }
- }
- }
|