Skip to content

Commit

Permalink
Change Event to "hide" Version as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
fredimachado committed May 26, 2024
1 parent 751dcf4 commit 23b41f0
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/Cashier/NCafe.Cashier.Api/Projections/ProductCreated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
12 changes: 6 additions & 6 deletions src/Common/NCafe.Core/Domain/AggregateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IEvent> _pendingEvents = [];
private readonly List<Event> _pendingEvents = [];

protected void RaiseEvent(Event @event)
{
@event.Version = Version + 1;
((IEvent)@event).Version = Version + 1;

ApplyEvent(@event);

_pendingEvents.Add(@event);
}

public IEnumerable<IEvent> GetPendingEvents()
public IReadOnlyCollection<Event> GetPendingEvents()
{
return [.. _pendingEvents];
return _pendingEvents.AsReadOnly();
}

internal void ClearPendingEvents()
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Common/NCafe.Core/Domain/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
4 changes: 2 additions & 2 deletions src/Common/NCafe.Core/Domain/IEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace NCafe.Core.Domain;

public interface IEvent
internal interface IEvent
{
Guid Id { get; }
long Version { get; internal protected set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
{
}
8 changes: 4 additions & 4 deletions src/Common/NCafe.Core/Projections/IProjectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace NCafe.Core.Projections;

public delegate Guid GetModelId<in TEvent>(TEvent @event) where TEvent : class, IEvent;
public delegate void ModelUpdate<in TEvent, T>(TEvent @event, T model) where TEvent : class, IEvent;
public delegate Guid GetModelId<in TEvent>(TEvent @event) where TEvent : Event;
public delegate void ModelUpdate<in TEvent, T>(TEvent @event, T model) where TEvent : Event;

public interface IProjectionService<T> where T : ReadModel
{
Task Start(CancellationToken cancellationToken);
void OnCreate<TEvent>(Func<TEvent, T> handler) where TEvent : class, IEvent;
void OnUpdate<TEvent>(GetModelId<TEvent> getId, ModelUpdate<TEvent, T> update) where TEvent : class, IEvent;
void OnCreate<TEvent>(Func<TEvent, T> handler) where TEvent : Event;
void OnUpdate<TEvent>(GetModelId<TEvent> getId, ModelUpdate<TEvent, T> update) where TEvent : Event;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void SubscriptionDropped(StreamSubscription subscription, SubscriptionDr
_logger.LogError("Subscription Dropped.");
}

public void OnCreate<TEvent>(Func<TEvent, T> handler) where TEvent : class, IEvent
public void OnCreate<TEvent>(Func<TEvent, T> handler) where TEvent : Event
{
MapEventHandler<TEvent>(resolvedEvent =>
{
Expand All @@ -67,7 +67,7 @@ public void OnCreate<TEvent>(Func<TEvent, T> handler) where TEvent : class, IEve
});
}

public void OnUpdate<TEvent>(GetModelId<TEvent> getId, ModelUpdate<TEvent, T> update) where TEvent : class, IEvent
public void OnUpdate<TEvent>(GetModelId<TEvent> getId, ModelUpdate<TEvent, T> update) where TEvent : Event
{
MapEventHandler<TEvent>(resolvedEvent =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 23b41f0

Please sign in to comment.