﻿namespace Studio_V.NCore.Domain
{
    /// <summary>
    /// Базовый объект домена
    /// </summary>
    public abstract class DomainObject
    {
        /// <summary> Идентификационный номер </summary>
        public virtual int Id { get; protected set; }

        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is DomainObject))
                return false;
            
            return this == (DomainObject) obj;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public static bool operator ==(DomainObject obj1, DomainObject obj2)
        {
            if ((object)obj1 == null && (object)obj2 == null)
                return true;

            if ((object) obj1 == null || (object) obj2 == null)
                return false;

            if (obj1.Id != obj2.Id)
                return false;

            return true;
        }

        public static bool operator !=(DomainObject obj1, DomainObject obj2)
        {
            return !(obj1 == obj2);
        }

        protected bool Equals(DomainObject other)
        {
            return _id == other._id;
        }

        private int _id = PlaceholderId;
        private const int PlaceholderId = -1;
    }
}