diff --git a/src/Cashier/NCafe.Cashier.Api/Projections/ProductCreated.cs b/src/Cashier/NCafe.Cashier.Api/Projections/ProductCreated.cs index 45c9b59..49f1f96 100644 --- a/src/Cashier/NCafe.Cashier.Api/Projections/ProductCreated.cs +++ b/src/Cashier/NCafe.Cashier.Api/Projections/ProductCreated.cs @@ -2,9 +2,8 @@ namespace NCafe.Cashier.Api.Projections; -public record ProductCreated : IEvent +public record ProductCreated : Event { - public Guid Id { get; init; } public string Name { get; init; } public decimal Price { get; init; } } diff --git a/src/Common/NCafe.Core/Domain/AggregateRoot.cs b/src/Common/NCafe.Core/Domain/AggregateRoot.cs index ceb42e0..cee07d2 100644 --- a/src/Common/NCafe.Core/Domain/AggregateRoot.cs +++ b/src/Common/NCafe.Core/Domain/AggregateRoot.cs @@ -6,22 +6,22 @@ namespace NCafe.Core.Domain; public abstract class AggregateRoot { public Guid Id { get; protected set; } - public long Version { get; protected set; } = -1; + internal long Version { get; set; } = -1; - private readonly List _pendingEvents = []; + private readonly List _pendingEvents = []; protected void RaiseEvent(Event @event) { - @event.Version = Version + 1; + ((IEvent)@event).Version = Version + 1; ApplyEvent(@event); _pendingEvents.Add(@event); } - public IEnumerable GetPendingEvents() + public IReadOnlyCollection GetPendingEvents() { - return [.. _pendingEvents]; + return _pendingEvents.AsReadOnly(); } internal void ClearPendingEvents() @@ -31,7 +31,7 @@ internal void ClearPendingEvents() internal void ApplyEvent(Event @event) { - if (@event.Version != Version + 1) + if (((IEvent)@event).Version != Version + 1) { throw new InvalidVersionException(@event, this); } diff --git a/src/Common/NCafe.Core/Domain/Event.cs b/src/Common/NCafe.Core/Domain/Event.cs index 618a7b4..3e4bb08 100644 --- a/src/Common/NCafe.Core/Domain/Event.cs +++ b/src/Common/NCafe.Core/Domain/Event.cs @@ -7,5 +7,5 @@ public abstract record Event : IEvent public Guid Id { get; init; } [JsonInclude] - public long Version { get; internal protected set; } + long IEvent.Version { get; set; } } diff --git a/src/Common/NCafe.Core/Domain/IEvent.cs b/src/Common/NCafe.Core/Domain/IEvent.cs index 84c950b..d622656 100644 --- a/src/Common/NCafe.Core/Domain/IEvent.cs +++ b/src/Common/NCafe.Core/Domain/IEvent.cs @@ -1,6 +1,6 @@ namespace NCafe.Core.Domain; -public interface IEvent +internal interface IEvent { - Guid Id { get; } + long Version { get; internal protected set; } } diff --git a/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs b/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs index 0f57b61..8300eed 100644 --- a/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs +++ b/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs @@ -3,6 +3,6 @@ namespace NCafe.Core.Exceptions; public class InvalidVersionException(Event @event, AggregateRoot aggregateRoot) - : DomainException($"{@event.GetType().Name} Event v{@event.Version} cannot be applied to Aggregate {aggregateRoot.GetType().Name} : {aggregateRoot.Id} v{aggregateRoot.Version}") + : DomainException($"{@event.GetType().Name} Event v{((IEvent)@event).Version} cannot be applied to Aggregate {aggregateRoot.GetType().Name} : {aggregateRoot.Id} v{aggregateRoot.Version}") { } diff --git a/src/Common/NCafe.Core/Projections/IProjectionService.cs b/src/Common/NCafe.Core/Projections/IProjectionService.cs index db0868b..f00e3c5 100644 --- a/src/Common/NCafe.Core/Projections/IProjectionService.cs +++ b/src/Common/NCafe.Core/Projections/IProjectionService.cs @@ -3,12 +3,12 @@ namespace NCafe.Core.Projections; -public delegate Guid GetModelId(TEvent @event) where TEvent : class, IEvent; -public delegate void ModelUpdate(TEvent @event, T model) where TEvent : class, IEvent; +public delegate Guid GetModelId(TEvent @event) where TEvent : Event; +public delegate void ModelUpdate(TEvent @event, T model) where TEvent : Event; public interface IProjectionService where T : ReadModel { Task Start(CancellationToken cancellationToken); - void OnCreate(Func handler) where TEvent : class, IEvent; - void OnUpdate(GetModelId getId, ModelUpdate update) where TEvent : class, IEvent; + void OnCreate(Func handler) where TEvent : Event; + void OnUpdate(GetModelId getId, ModelUpdate update) where TEvent : Event; } diff --git a/src/Common/NCafe.Infrastructure/EventStore/EventStoreProjectionService.cs b/src/Common/NCafe.Infrastructure/EventStore/EventStoreProjectionService.cs index 845b0f5..4be0148 100644 --- a/src/Common/NCafe.Infrastructure/EventStore/EventStoreProjectionService.cs +++ b/src/Common/NCafe.Infrastructure/EventStore/EventStoreProjectionService.cs @@ -54,7 +54,7 @@ private void SubscriptionDropped(StreamSubscription subscription, SubscriptionDr _logger.LogError("Subscription Dropped."); } - public void OnCreate(Func handler) where TEvent : class, IEvent + public void OnCreate(Func handler) where TEvent : Event { MapEventHandler(resolvedEvent => { @@ -67,7 +67,7 @@ public void OnCreate(Func handler) where TEvent : class, IEve }); } - public void OnUpdate(GetModelId getId, ModelUpdate update) where TEvent : class, IEvent + public void OnUpdate(GetModelId getId, ModelUpdate update) where TEvent : Event { MapEventHandler(resolvedEvent => { diff --git a/src/Common/NCafe.Infrastructure/EventStore/EventStoreRepository.cs b/src/Common/NCafe.Infrastructure/EventStore/EventStoreRepository.cs index fc13dfe..8d487f6 100644 --- a/src/Common/NCafe.Infrastructure/EventStore/EventStoreRepository.cs +++ b/src/Common/NCafe.Infrastructure/EventStore/EventStoreRepository.cs @@ -47,8 +47,7 @@ public async Task Save(AggregateRoot aggregate) var streamName = ToStreamName(aggregate.Id, aggregate.GetType()); var pendingEvents = aggregate.GetPendingEvents().ToArray(); - var originalVersion = aggregate.Version - pendingEvents.Length; - var expectedVersion = originalVersion; + var expectedVersion = aggregate.Version - pendingEvents.Length; var eventsToAppend = pendingEvents .Select(e => e.AsEventData())