From 0aeb640b2c0bc493d6f142bd0fe9cb0024819f53 Mon Sep 17 00:00:00 2001 From: max arshinov Date: Sun, 16 Oct 2022 18:18:16 +0200 Subject: [PATCH 1/5] AggregateRoot --- CSharpFunctionalExtensions/AggregateRoot.cs | 20 ++++++++++++++++++++ CSharpFunctionalExtensions/Entity/Entity.cs | 4 ++-- CSharpFunctionalExtensions/Entity/IEntity.cs | 10 ++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 CSharpFunctionalExtensions/AggregateRoot.cs create mode 100644 CSharpFunctionalExtensions/Entity/IEntity.cs diff --git a/CSharpFunctionalExtensions/AggregateRoot.cs b/CSharpFunctionalExtensions/AggregateRoot.cs new file mode 100644 index 00000000..da46fcc8 --- /dev/null +++ b/CSharpFunctionalExtensions/AggregateRoot.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace CSharpFunctionalExtensions +{ + public abstract class AggregateRoot : Entity + { + private readonly List _domainEvents = new List(); + public virtual IReadOnlyList 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 From 66c4e798ae530eb379fed315b322f9048a29d9ba Mon Sep 17 00:00:00 2001 From: max arshinov Date: Sun, 16 Oct 2022 18:19:45 +0200 Subject: [PATCH 2/5] IAggregateRoot --- CSharpFunctionalExtensions/AggregateRoot.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CSharpFunctionalExtensions/AggregateRoot.cs b/CSharpFunctionalExtensions/AggregateRoot.cs index da46fcc8..22b3d403 100644 --- a/CSharpFunctionalExtensions/AggregateRoot.cs +++ b/CSharpFunctionalExtensions/AggregateRoot.cs @@ -2,7 +2,12 @@ namespace CSharpFunctionalExtensions { - public abstract class AggregateRoot : Entity + public interface IAggregateRoot + { + IReadOnlyList DomainEvents { get; } + } + + public abstract class AggregateRoot : Entity, IAggregateRoot { private readonly List _domainEvents = new List(); public virtual IReadOnlyList DomainEvents => _domainEvents; From 3ec2f1b350628300315c40164d05a192baf57d9f Mon Sep 17 00:00:00 2001 From: max arshinov Date: Sun, 16 Oct 2022 21:59:39 +0200 Subject: [PATCH 3/5] Build fix --- CSharpFunctionalExtensions/AggregateRoot.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CSharpFunctionalExtensions/AggregateRoot.cs b/CSharpFunctionalExtensions/AggregateRoot.cs index 22b3d403..581f9f2e 100644 --- a/CSharpFunctionalExtensions/AggregateRoot.cs +++ b/CSharpFunctionalExtensions/AggregateRoot.cs @@ -4,13 +4,13 @@ namespace CSharpFunctionalExtensions { public interface IAggregateRoot { - IReadOnlyList DomainEvents { get; } + IEnumerable DomainEvents { get; } } public abstract class AggregateRoot : Entity, IAggregateRoot { private readonly List _domainEvents = new List(); - public virtual IReadOnlyList DomainEvents => _domainEvents; + public virtual IEnumerable DomainEvents => _domainEvents; protected virtual void Raise(TDomainEvent newEvent) { From e0806ae5e8262bc8b98e465a3d06ef3090a89747 Mon Sep 17 00:00:00 2001 From: max arshinov Date: Sun, 16 Oct 2022 23:14:49 +0200 Subject: [PATCH 4/5] TId --- CSharpFunctionalExtensions/AggregateRoot.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CSharpFunctionalExtensions/AggregateRoot.cs b/CSharpFunctionalExtensions/AggregateRoot.cs index 581f9f2e..0cced962 100644 --- a/CSharpFunctionalExtensions/AggregateRoot.cs +++ b/CSharpFunctionalExtensions/AggregateRoot.cs @@ -7,7 +7,7 @@ public interface IAggregateRoot IEnumerable DomainEvents { get; } } - public abstract class AggregateRoot : Entity, IAggregateRoot + public abstract class AggregateRoot : Entity, IAggregateRoot { private readonly List _domainEvents = new List(); public virtual IEnumerable DomainEvents => _domainEvents; From d017c7a278b15f7ac37177dee2b92c474071611b Mon Sep 17 00:00:00 2001 From: max arshinov Date: Sun, 16 Oct 2022 23:16:43 +0200 Subject: [PATCH 5/5] Cleanup --- CSharpFunctionalExtensions/AggregateRoot.cs | 5 ----- CSharpFunctionalExtensions/IAggregateRoot.cs | 9 +++++++++ 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 CSharpFunctionalExtensions/IAggregateRoot.cs diff --git a/CSharpFunctionalExtensions/AggregateRoot.cs b/CSharpFunctionalExtensions/AggregateRoot.cs index 0cced962..b6ed2973 100644 --- a/CSharpFunctionalExtensions/AggregateRoot.cs +++ b/CSharpFunctionalExtensions/AggregateRoot.cs @@ -2,11 +2,6 @@ namespace CSharpFunctionalExtensions { - public interface IAggregateRoot - { - IEnumerable DomainEvents { get; } - } - public abstract class AggregateRoot : Entity, IAggregateRoot { private readonly List _domainEvents = new List(); 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