-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added LoadState to the same interface (for now)
- Loading branch information
1 parent
6e40c94
commit c9c6da2
Showing
3 changed files
with
47 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using JetBrains.Annotations; | ||
|
||
namespace Eventuous { | ||
public abstract record AggregateState<T> where T : AggregateState<T> { | ||
public abstract T When(object @event); | ||
public virtual T When(object @event) { | ||
var eventType = @event.GetType(); | ||
if (!_handlers.ContainsKey(eventType)) return (T) this; | ||
|
||
return _handlers[eventType](@event); | ||
} | ||
|
||
[PublicAPI] | ||
protected void On<TEvent>(Func<TEvent, T> handle) { | ||
if (!_handlers.TryAdd(typeof(TEvent), x => handle((TEvent) x))) { | ||
throw new InvalidOperationException($"Duplicate handler for {typeof(TEvent).Name}"); | ||
} | ||
} | ||
|
||
readonly Dictionary<Type, Func<object, T>> _handlers = new(); | ||
} | ||
|
||
public abstract record AggregateState<T, TId> : AggregateState<T> | ||
where T : AggregateState<T, TId> | ||
where TId : AggregateId { | ||
public TId Id { get; protected init; } = null!; | ||
|
||
internal T SetId(TId id) => (T) this with { Id = id }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters