Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion samples/EasyWay.Samples/Domain/SampleAggregateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ public sealed class SampleAggregateRoot : AggregateRoot
{
internal SampleAggregateRoot()
{
Add(new CreatedSampleAggragete());
Apply(new CreatedSampleAggragete());
}

private void When(CreatedSampleAggragete @event)
{

}

public void SampleMethod()
Expand Down
13 changes: 12 additions & 1 deletion source/EasyWay/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using EasyWay.Internals.BusinessRules;
using EasyWay.Internals.Clocks;
using EasyWay.Internals.DomainEvents;
using EasyWay.Internals.Entities;
using EasyWay.Internals.GuidGenerators;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace EasyWay
{
Expand All @@ -26,14 +28,23 @@ protected static void Check(BusinessRule businessRule)
}
}

protected void Add<TDomainEvent>(TDomainEvent domainEvent)
protected void Apply<TDomainEvent>(TDomainEvent domainEvent)
where TDomainEvent : DomainEvent
{
if (domainEvent is null)
{
throw new DomainEventCannotBeNullException<TDomainEvent>();
}

var method = GetType().GetMethod("When", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

if (method is null)
{
throw new NotImplementedWhenMethodException<TDomainEvent>();
}

method.Invoke(this, new object[] { domainEvent });

var domainEventContext = new DomainEventContext()
{
EventId = GuidGenerator.New,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EasyWay.Internals.DomainEvents
namespace EasyWay.Internals.Entities
{
internal sealed class DomainEventCannotBeNullException<TDomainEvent> : EasyWayException
where TDomainEvent : DomainEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EasyWay.Internals.Entities
{
internal sealed class NotImplementedWhenMethodException<TDomainEvent> : EasyWayException
where TDomainEvent : DomainEvent
{
internal NotImplementedWhenMethodException()
: base("Not implemented 'When' method for " + typeof(TDomainEvent).Name) { }
}
}
2 changes: 1 addition & 1 deletion tests/EasyWay.Tests/Entities/AddDomainEventToEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EasyWay.Internals.DomainEvents;
using EasyWay.Internals.Entities;
using EasyWay.Tests.Entities.SeedWorks;

namespace EasyWay.Tests.Entities
Expand Down
8 changes: 6 additions & 2 deletions tests/EasyWay.Tests/Entities/SeedWorks/TestEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace EasyWay.Tests.Entities.SeedWorks
using static EasyWay.Tests.Entities.AddDomainEventToEntity;

namespace EasyWay.Tests.Entities.SeedWorks
{
internal sealed class TestEntity : Entity
{
Expand All @@ -10,7 +12,9 @@ public void CheckBusinessRule(BusinessRule businessRule)
public void AddDomainEvent<TDomainEvent>(TDomainEvent domainEvent)
where TDomainEvent : DomainEvent
{
Add(domainEvent);
Apply(domainEvent);
}

private void When(TestDomainEvent @event) { }
}
}
Loading