2017-01-30 15:46:43 +01:00
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork
2016-09-07 13:52:26 -07:00
{
2016-11-21 12:41:36 +01:00
using System ;
2017-03-14 18:02:28 +01:00
using MediatR ;
using System.Collections.Generic ;
2016-11-21 12:41:36 +01:00
2016-09-08 16:33:23 -07:00
public abstract class Entity
2016-09-07 13:52:26 -07:00
{
2016-09-08 16:33:23 -07:00
int? _requestedHashCode ;
2016-11-21 12:41:36 +01:00
int _Id ;
2016-09-08 16:33:23 -07:00
2017-03-16 18:52:02 -07:00
private List < IAsyncNotification > _domainEvents ;
2017-03-14 18:02:28 +01:00
2016-11-21 12:41:36 +01:00
public virtual int Id
2016-09-08 16:33:23 -07:00
{
get
{
return _Id ;
}
protected set
{
_Id = value ;
}
}
2017-03-16 18:52:02 -07:00
public List < IAsyncNotification > DomainEvents = > _domainEvents ;
public void AddDomainEvent ( IAsyncNotification eventItem )
2017-03-14 18:02:28 +01:00
{
2017-03-16 18:52:02 -07:00
_domainEvents = _domainEvents ? ? new List < IAsyncNotification > ( ) ;
_domainEvents . Add ( eventItem ) ;
2017-03-14 18:02:28 +01:00
}
2017-03-16 18:52:02 -07:00
public void RemoveDomainEvent ( IAsyncNotification eventItem )
2017-03-14 18:02:28 +01:00
{
2017-03-16 18:52:02 -07:00
if ( _domainEvents is null ) return ;
_domainEvents . Remove ( eventItem ) ;
2017-03-14 18:02:28 +01:00
}
2016-09-08 16:33:23 -07:00
public bool IsTransient ( )
{
2016-11-21 12:41:36 +01:00
return this . Id = = default ( Int32 ) ;
2016-09-08 16:33:23 -07:00
}
public override bool Equals ( object obj )
{
if ( obj = = null | | ! ( obj is Entity ) )
return false ;
if ( Object . ReferenceEquals ( this , obj ) )
return true ;
2017-05-04 12:29:00 -07:00
if ( this . GetType ( ) ! = obj . GetType ( ) )
return false ;
2016-09-08 16:33:23 -07:00
Entity item = ( Entity ) obj ;
if ( item . IsTransient ( ) | | this . IsTransient ( ) )
return false ;
else
return item . Id = = this . Id ;
}
public override int GetHashCode ( )
{
if ( ! IsTransient ( ) )
{
if ( ! _requestedHashCode . HasValue )
_requestedHashCode = this . Id . GetHashCode ( ) ^ 31 ; // XOR for random distribution (http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx)
return _requestedHashCode . Value ;
}
else
return base . GetHashCode ( ) ;
}
2016-11-21 12:41:36 +01:00
2016-09-08 16:33:23 -07:00
public static bool operator = = ( Entity left , Entity right )
{
if ( Object . Equals ( left , null ) )
return ( Object . Equals ( right , null ) ) ? true : false ;
else
return left . Equals ( right ) ;
}
public static bool operator ! = ( Entity left , Entity right )
{
return ! ( left = = right ) ;
}
2016-09-07 13:52:26 -07:00
}
}