diff --git a/CSharpFunctionalExtensions/AggregateRoot.cs b/CSharpFunctionalExtensions/AggregateRoot.cs new file mode 100644 index 00000000..b6ed2973 --- /dev/null +++ b/CSharpFunctionalExtensions/AggregateRoot.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace CSharpFunctionalExtensions +{ + public abstract class AggregateRoot : Entity, IAggregateRoot + { + private readonly List _domainEvents = new List(); + public virtual IEnumerable DomainEvents => _domainEvents; + + protected virtual void Raise(TDomainEvent newEvent) + { + _domainEvents.Add(newEvent); + } + + protected virtual void ClearEvents() + { + _domainEvents.Clear(); + } + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions/Entity/Entity.cs b/CSharpFunctionalExtensions/Entity/Entity.cs index ae9d7d6e..a5bf3701 100644 --- a/CSharpFunctionalExtensions/Entity/Entity.cs +++ b/CSharpFunctionalExtensions/Entity/Entity.cs @@ -1,6 +1,6 @@ namespace CSharpFunctionalExtensions { - public abstract class Entity + public abstract class Entity : IEntity { public virtual TId Id { get; protected set; } @@ -30,7 +30,7 @@ public override bool Equals(object obj) return Id.Equals(other.Id); } - private bool IsTransient() + public bool IsTransient() { return Id is null || Id.Equals(default(TId)); } diff --git a/CSharpFunctionalExtensions/Entity/IEntity.cs b/CSharpFunctionalExtensions/Entity/IEntity.cs new file mode 100644 index 00000000..848f0bab --- /dev/null +++ b/CSharpFunctionalExtensions/Entity/IEntity.cs @@ -0,0 +1,10 @@ +namespace CSharpFunctionalExtensions +{ + public interface IEntity + { + TId Id { get; } + bool Equals(object obj); + bool IsTransient(); + int GetHashCode(); + } +} \ No newline at end of file diff --git a/CSharpFunctionalExtensions/IAggregateRoot.cs b/CSharpFunctionalExtensions/IAggregateRoot.cs new file mode 100644 index 00000000..0297fbe5 --- /dev/null +++ b/CSharpFunctionalExtensions/IAggregateRoot.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace CSharpFunctionalExtensions +{ + public interface IAggregateRoot + { + IEnumerable DomainEvents { get; } + } +} \ No newline at end of file