Skip to content
Open
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: 4 additions & 3 deletions src/NEventLite/Core/Domain/AggregateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private async Task ApplyEventAsync(IEvent<AggregateRoot<TAggregateKey, TEventKey
{
if (CanApply(@event))
{
await DoApplyAsync(@event);
await DoApplyAsync(@event, isNew);

if (isNew)
{
Expand Down Expand Up @@ -140,7 +140,7 @@ private bool CanApply(IEvent<AggregateRoot<TAggregateKey, TEventKey>, TAggregate
&& (CurrentVersion == @event.TargetVersion));
}

private async Task DoApplyAsync(IEvent<AggregateRoot<TAggregateKey, TEventKey>, TAggregateKey, TEventKey> @event)
private async Task DoApplyAsync(IEvent<AggregateRoot<TAggregateKey, TEventKey>, TAggregateKey, TEventKey> @event, bool isNew)
{
if (StreamState == StreamState.NoStream)
{
Expand All @@ -149,7 +149,8 @@ private async Task DoApplyAsync(IEvent<AggregateRoot<TAggregateKey, TEventKey>,

if (_eventHandlerCache.ContainsKey(@event.GetType()))
{
await @event.InvokeOnAggregateAsync(this, _eventHandlerCache[@event.GetType()]);
var replayStatus = isNew ? ReplayStatus.Regular : ReplayStatus.Replay;
await @event.InvokeOnAggregateAsync(this, _eventHandlerCache[@event.GetType()], replayStatus);
}
else
{
Expand Down
8 changes: 8 additions & 0 deletions src/NEventLite/Core/ReplayStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NEventLite.Core
{
public enum ReplayStatus
{
Regular,
Replay
}
}
24 changes: 16 additions & 8 deletions src/NEventLite/Util/EventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ namespace NEventLite.Util
public static class EventExtensions
{
public static async Task InvokeOnAggregateAsync<TAggregateKey, TEventKey>(this IEvent<AggregateRoot<TAggregateKey, TEventKey>, TAggregateKey, TEventKey> @event,
AggregateRoot<TAggregateKey, TEventKey> aggregate, string methodName)
AggregateRoot<TAggregateKey, TEventKey> aggregate, string methodName, ReplayStatus replayStatus)
{
var method = ReflectionHelper.GetMethod(aggregate.GetType(), methodName, new Type[] { @event.GetType() }); //Find the right method

if (method != null)
object[] args;
if (method == null)
{
var task = method.Invoke(aggregate, new object[] { @event }); //invoke with the event as argument

if (task != null && task.GetType() == typeof(Task))
method = ReflectionHelper.GetMethod(aggregate.GetType(), methodName, new Type[] { @event.GetType(), typeof(ReplayStatus) }); // handler with replay status
if (method == null)
{
await (Task)task;
throw new AggregateEventOnApplyMethodMissingException($"No event Apply method found on {aggregate.GetType()} for {@event.GetType()}");
}
args = new object[] {@event, replayStatus};
}
else
{
throw new AggregateEventOnApplyMethodMissingException($"No event Apply method found on {aggregate.GetType()} for {@event.GetType()}");
args = new object[] {@event};
}

var task = method.Invoke(aggregate, args); //invoke with the event handler

if (task != null && task.GetType() == typeof(Task))
{
await (Task)task;
}

}
}
}
6 changes: 5 additions & 1 deletion src/NEventLite/Util/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ public static Dictionary<Type, string> FindEventHandlerMethodsInAggregate<TAggre

var voidMethods = aggregateType.GetMethodsBySig(typeof(void), typeof(InternalEventHandler),
true, eventType).ToList();
var voidMethodsWithReplay = aggregateType.GetMethodsBySig(typeof(void), typeof(InternalEventHandler),
true, eventType, typeof(ReplayStatus)).ToList();
var asyncMethods = aggregateType.GetMethodsBySig(typeof(Task), typeof(InternalEventHandler),
true, eventType).ToList();
var asyncMethodsWithReplay = aggregateType.GetMethodsBySig(typeof(Task), typeof(InternalEventHandler),
true, eventType, typeof(ReplayStatus)).ToList();

var methods = voidMethods.Union(asyncMethods).ToList();
var methods = voidMethods.Union(asyncMethods).Union(voidMethodsWithReplay).Union(asyncMethodsWithReplay).ToList();

if (methods.Any())
{
Expand Down