sexta-feira, 24 de julho de 2015

NHibernate - EntityBase

using System;

namespace Lapuinka.Data
{
    public interface IVersionedEntity
    {
    }

    public interface IEntity
    {
        Type GetEntityType();
    }

    public interface IEntity : IEntity
    {
        TId Id { get; set; }
    }

    public interface IEntity
        : IEntity
        where TEntity : IEntity
    {
    }

    [Serializable]
    public abstract class EntityBase
        : IEntity
        where TEntity : EntityBase
    {
        #region [ Attributes ]

        private int? _transientHashCode;

        #endregion
       
        #region [ Properties ]
       
        ///
        /// Gets or sets the id.
        ///
        /// The id.
        public virtual TId Id
        {
            get; set;
        }

        #endregion

        #region [ Overrides ]

        ///
        /// Determines whether the specified is equal to the current .
        ///
        public override bool Equals(object obj)
        {
            var x = obj as EntityBase;

            if (ReferenceEquals(x, null))
            {
                return false;
            }

            if (IsTransient() && x.IsTransient())
            {
                return ReferenceEquals(this, x);
            }

            return (Id.Equals(x.Id));
        }

        private static bool Equals(EntityBase first, EntityBase second)
        {
            if (ReferenceEquals(first, null) && ReferenceEquals(second, null))
            {
                return true;
            }

            if (ReferenceEquals(first, null) || ReferenceEquals(second, null))
            {
                return false;
            }

            if (first.IsTransient() && second.IsTransient())
            {
                return ReferenceEquals(first, second);
            }

            return (first.Id.Equals(second.Id));
        }

        ///
        /// Serves as a hash function for a particular type.
        /// If persistent, uses Id; if transient, uses temporary code.
        /// (This will not work if the entity is evicted and reloaded)
        ///
        public override int GetHashCode()
        {
            return GetHashCodeInternal();
        }

        ///
        /// Serves as a hash function for a particular type.
        /// If persistent, uses Id; if transient, uses temporary code.
        /// (This will not work if the entity is evicted and reloaded)
        ///
        private int GetHashCodeInternal()
        {
            if (_transientHashCode.HasValue)
            {
                return _transientHashCode.Value;
            }

            if (IsTransient())
            {
                _transientHashCode = base.GetHashCode();
                return _transientHashCode.Value;
            }

            return Id.GetHashCode();
        }

        ///
        /// Returns a that represents the current .
        ///
        public override string ToString()
        {
            return GetType().Name + " = "
                + (IsTransient() ? "(Transient)" : Id.ToString());
        }
       
        #endregion

        #region [ Comparison ]

        ///
        /// Implements the operator == (Equals).
        ///
        public static bool operator ==(EntityBase first, EntityBase second)
        {
            return Equals(first, second);
        }

        ///
        /// Implements the operator != (Not Equals).
        ///
        public static bool operator !=(EntityBase first, EntityBase second)
        {
            return !(Equals(first, second));
        }

        #endregion

        #region [ Helper Methods ]

        ///
        /// Determines whether this instance is transient.
        ///
        public virtual bool IsTransient()
        {
            return Equals(Id, default(TId));
        }

        public virtual Type GetEntityType()
        {
            return typeof (TEntity);
        }

        #endregion
    }
}


Postar um comentário