Skip to content

Commit

Permalink
Used Events definition as Disciminated Union in exercises
Browse files Browse the repository at this point in the history
Replaced When method name into Evolve, as it's now more aligned with how people are doing it.
  • Loading branch information
oskardudycz committed Apr 26, 2024
1 parent d83b85b commit 451f4f5
Show file tree
Hide file tree
Showing 31 changed files with 392 additions and 306 deletions.
2 changes: 1 addition & 1 deletion Core.ElasticSearch/Projections/ElasticSearchProjection.cs
Expand Up @@ -30,7 +30,7 @@ public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken
var entity = (await elasticClient.GetAsync<TView>(id, i => i.Index(indexName), ct).ConfigureAwait(false))?.Source ??
(TView) Activator.CreateInstance(typeof(TView), true)!;

entity.When(eventEnvelope);
entity.Evolve(eventEnvelope);

await elasticClient.IndexAsync(
entity,
Expand Down
2 changes: 1 addition & 1 deletion Core.EventStoreDB/Events/AggregateStreamExtensions.cs
Expand Up @@ -30,7 +30,7 @@ await foreach (var @event in readResult)
{
var eventData = @event.Deserialize();

aggregate.When(eventData!);
aggregate.Evolve(eventData!);
}

return aggregate;
Expand Down
Expand Up @@ -33,7 +33,7 @@ public async Task Handle(EventEnvelope<TEvent> eventEnvelope, CancellationToken
if (entity.LastProcessedPosition >= eventLogPosition)
return;

entity.When(@event);
entity.Evolve(@event);

entity.LastProcessedPosition = eventLogPosition;

Expand Down
4 changes: 2 additions & 2 deletions Core.Tests/AggregateWithWhenTests.cs
Expand Up @@ -55,7 +55,7 @@ public class Invoice: Aggregate<string>
public InvoiceSendMethod SentVia { get; private set; }
public DateTime SentAt { get; private set; }

public override void When(object @event)
public override void Evolve(object @event)
{
switch (@event)
{
Expand Down Expand Up @@ -125,7 +125,7 @@ public void AggregationWithWhenShouldGetTheCurrentState()
// 3. Apply each event on the entity.
foreach (var @event in events)
{
invoice.When(@event);
invoice.Evolve(@event);
}

invoice.Id.Should().Be(invoiceInitiated.Number);
Expand Down
2 changes: 1 addition & 1 deletion Core/Aggregates/Aggregate.cs
Expand Up @@ -12,7 +12,7 @@ public abstract class Aggregate<T>: IAggregate<T> where T : notnull

[NonSerialized] private readonly Queue<object> uncommittedEvents = new();

public virtual void When(object @event) { }
public virtual void Evolve(object @event) { }

public object[] DequeueUncommittedEvents()
{
Expand Down
2 changes: 1 addition & 1 deletion Core/ProcessManagers/ProcessManager.cs
Expand Up @@ -29,7 +29,7 @@ public EventOrCommand[] DequeuePendingMessages()
protected void ScheduleCommand(object @event) =>
scheduledCommands.Enqueue(EventOrCommand.Command(@event));

public virtual void When(object @event)
public virtual void Evolve(object @event)
{
}
}
2 changes: 1 addition & 1 deletion Core/Projections/IProjection.cs
Expand Up @@ -2,7 +2,7 @@

public interface IProjection
{
void When(object @event);
void Evolve(object @event);
}

public interface IVersionedProjection: IProjection
Expand Down
Expand Up @@ -25,7 +25,7 @@ public ShoppingCart Build()

foreach (var @event in eventsToApply)
{
cart.When(@event);
cart.Evolve(@event);
}

return cart;
Expand Down
Expand Up @@ -23,7 +23,7 @@ public class ShoppingCartDetails: IVersionedProjection
public long Version { get; set; }


public void When(object @event)
public void Evolve(object @event)
{
switch (@event)
{
Expand Down
Expand Up @@ -14,7 +14,7 @@ public class CartHistory: IVersionedProjection
public string Description { get; set; } = default!;
public ulong LastProcessedPosition { get; set; }

public void When(object @event)
public void Evolve(object @event)
{
switch (@event)
{
Expand Down
Expand Up @@ -17,7 +17,7 @@ public class ShoppingCartShortInfo: IVersionedProjection

public ulong LastProcessedPosition { get; set; }

public void When(object @event)
public void Evolve(object @event)
{
switch (@event)
{
Expand Down
Expand Up @@ -29,7 +29,7 @@ public class ShoppingCart: Aggregate

private ShoppingCart(){}

public override void When(object @event)
public override void Evolve(object @event)
{
switch (@event)
{
Expand Down
Expand Up @@ -17,5 +17,6 @@ Events model:

![events](./assets/events.jpg)

There are two alternative solutions:
1. Simple events structure [Solution1/EventsDefinitionTests.cs](./Solution1/EventsDefinitionTests.cs).
2. Events structure with union type to give option to tell that Shopping Cart event is one of the defined types [Solution2/EventsDefinitionTests.cs](./Solution2/EventsDefinitionTests.cs). Read more in [Union types in C#](https://event-driven.io/en/union_types_in_csharp/).
Expand Up @@ -4,32 +4,39 @@
using Xunit;

namespace IntroductionToEventSourcing.AppendingEvents;
using static ShoppingCartEvent;

// EVENTS
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
);

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
);

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
);
public abstract record ShoppingCartEvent
{
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
): ShoppingCartEvent;

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
): ShoppingCartEvent;

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
): ShoppingCartEvent;

// This won't allow
private ShoppingCartEvent(){}
}

// VALUE OBJECTS
public record PricedProductItem(
Expand Down
Expand Up @@ -4,32 +4,39 @@
using Xunit;

namespace IntroductionToEventSourcing.AppendingEvents;
using static ShoppingCartEvent;

// EVENTS
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
);

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
);

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
);
public abstract record ShoppingCartEvent
{
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
): ShoppingCartEvent;

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
): ShoppingCartEvent;

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
): ShoppingCartEvent;

// This won't allow
private ShoppingCartEvent(){}
}

// VALUE OBJECTS
public record PricedProductItem(
Expand Down
Expand Up @@ -4,32 +4,39 @@
using Xunit;

namespace IntroductionToEventSourcing.GettingStateFromEvents.Immutable;
using static ShoppingCartEvent;

// EVENTS
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
);

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
);

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
);
public abstract record ShoppingCartEvent
{
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
): ShoppingCartEvent;

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
): ShoppingCartEvent;

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
): ShoppingCartEvent;

// This won't allow
private ShoppingCartEvent(){}
}

// VALUE OBJECTS
public record PricedProductItem(
Expand Down
Expand Up @@ -4,32 +4,39 @@
using Xunit;

namespace IntroductionToEventSourcing.GettingStateFromEvents.Mutable;
using static ShoppingCartEvent;

// EVENTS
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
);

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
);

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
);

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
);
public abstract record ShoppingCartEvent
{
public record ShoppingCartOpened(
Guid ShoppingCartId,
Guid ClientId
): ShoppingCartEvent;

public record ProductItemAddedToShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ProductItemRemovedFromShoppingCart(
Guid ShoppingCartId,
PricedProductItem ProductItem
): ShoppingCartEvent;

public record ShoppingCartConfirmed(
Guid ShoppingCartId,
DateTime ConfirmedAt
): ShoppingCartEvent;

public record ShoppingCartCanceled(
Guid ShoppingCartId,
DateTime CanceledAt
): ShoppingCartEvent;

// This won't allow
private ShoppingCartEvent(){}
}

// VALUE OBJECTS
public class PricedProductItem
Expand Down

0 comments on commit 451f4f5

Please sign in to comment.