Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated exercises on events definitions and getting state to include …
…union type for events
- Loading branch information
1 parent
1cfa74b
commit d83b85b
Showing
8 changed files
with
293 additions
and
144 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
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
2 changes: 1 addition & 1 deletion
2
...EventsDefinition/EventsDefinitionTests.cs → ...nition/Solution1/EventsDefinitionTests.cs
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
119 changes: 119 additions & 0 deletions
119
...IntroductionToEventSourcing/Solved/01-EventsDefinition/Solution2/EventsDefinitionTests.cs
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace IntroductionToEventSourcing.EventsDefinition.Solution2; | ||
using static ShoppingCartEvent; | ||
|
||
// 1. Define your events and entity here | ||
|
||
// EVENTS | ||
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 | ||
{ | ||
public Guid ProductId { get; set; } | ||
public decimal UnitPrice { get; set; } | ||
|
||
public int Quantity { get; set; } | ||
|
||
public decimal TotalPrice => Quantity * UnitPrice; | ||
} | ||
|
||
public record ImmutablePricedProductItem( | ||
Guid ProductId, | ||
int Quantity, | ||
decimal UnitPrice | ||
) | ||
{ | ||
public decimal TotalPrice => Quantity * UnitPrice; | ||
} | ||
|
||
// ENTITY | ||
|
||
// regular one | ||
public class ShoppingCart | ||
{ | ||
public Guid Id { get; set; } | ||
public Guid ClientId { get; set; } | ||
public ShoppingCartStatus Status { get; set; } | ||
public IList<PricedProductItem> ProductItems { get; set; } = new List<PricedProductItem>(); | ||
public DateTime? ConfirmedAt { get; set; } | ||
public DateTime? CanceledAt { get; set; } | ||
} | ||
|
||
// immutable one | ||
public record ImmutableShoppingCart( | ||
Guid Id, | ||
Guid ClientId, | ||
ShoppingCartStatus Status, | ||
PricedProductItem[] ProductItems, | ||
DateTime? ConfirmedAt = null, | ||
DateTime? CanceledAt = null | ||
); | ||
|
||
public enum ShoppingCartStatus | ||
{ | ||
Pending = 1, | ||
Confirmed = 2, | ||
Canceled = 4 | ||
} | ||
|
||
public class EventsDefinitionTests | ||
{ | ||
[Fact] | ||
public void AllEventTypes_ShouldBeDefined() | ||
{ | ||
var shoppingCartId = Guid.NewGuid(); | ||
var clientId = Guid.NewGuid(); | ||
var pairOfShoes = new PricedProductItem | ||
{ | ||
ProductId = Guid.NewGuid(), | ||
Quantity = 1, | ||
UnitPrice = 100 | ||
}; | ||
|
||
var events = new object[] | ||
{ | ||
new ShoppingCartOpened(shoppingCartId, clientId), | ||
new ProductItemAddedToShoppingCart(shoppingCartId, pairOfShoes), | ||
new ProductItemRemovedFromShoppingCart(shoppingCartId, pairOfShoes), | ||
new ShoppingCartConfirmed(shoppingCartId, DateTime.UtcNow), | ||
new ShoppingCartCanceled(shoppingCartId, DateTime.UtcNow) | ||
}; | ||
|
||
const int expectedEventTypesCount = 5; | ||
events.Should().HaveCount(expectedEventTypesCount); | ||
events.GroupBy(e => e.GetType()).Should().HaveCount(expectedEventTypesCount); | ||
} | ||
} |
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
Oops, something went wrong.