Skip to content

Commit

Permalink
Moved Versioning sample to file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Dec 5, 2021
1 parent 0b870e5 commit b6f1565
Show file tree
Hide file tree
Showing 11 changed files with 748 additions and 755 deletions.
3 changes: 3 additions & 0 deletions EventSourcing.NetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Serialization", "Core.Serialization\Core.Serialization.csproj", "{8C102EF7-ED16-43AC-9511-DB20BDE5E743}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EventsVersioning", "EventsVersioning", "{0C3B70E2-26A6-4707-A537-74D649EDC2B8}"
ProjectSection(SolutionItems) = preProject
Sample\EventsVersioning\README.md = Sample\EventsVersioning\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECommerce.V1", "Sample\EventsVersioning\ECommerce.V1\ECommerce.V1.csproj", "{7B7E8DF0-8D21-4BC0-8A61-966A34E77DD8}"
EndProject
Expand Down
73 changes: 36 additions & 37 deletions Sample/EventsVersioning/ECommerce.V1/Events.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
using System;

namespace ECommerce.V1
namespace ECommerce.V1;

public record ProductItem(
Guid ProductId,
int Quantity
);

public record PricedProductItem(
ProductItem ProductItem,
decimal UnitPrice
);

public record ShoppingCartInitialized(
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 enum ShoppingCartStatus
{
public record ProductItem(
Guid ProductId,
int Quantity
);

public record PricedProductItem(
ProductItem ProductItem,
decimal UnitPrice
);

public record ShoppingCartInitialized(
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 enum ShoppingCartStatus
{
Pending = 1,
Confirmed = 2,
Cancelled = 3
}
Pending = 1,
Confirmed = 2,
Cancelled = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,74 @@
using Xunit;
using V1 = ECommerce.V1;

namespace EventsVersioning.Tests.Downcasters
namespace EventsVersioning.Tests.Downcasters;

public class ChangedStructure
{
public class ChangedStructure
{
public record Client(
Guid Id,
string Name = "Unknown"
);
public record Client(
Guid Id,
string Name = "Unknown"
);

public record ShoppingCartInitialized(
Guid ShoppingCartId,
Client Client
);
public record ShoppingCartInitialized(
Guid ShoppingCartId,
Client Client
);

public static V1.ShoppingCartInitialized Downcast(
ShoppingCartInitialized newEvent
)
{
return new V1.ShoppingCartInitialized(
newEvent.ShoppingCartId,
newEvent.Client.Id
);
}
public static V1.ShoppingCartInitialized Downcast(
ShoppingCartInitialized newEvent
)
{
return new V1.ShoppingCartInitialized(
newEvent.ShoppingCartId,
newEvent.Client.Id
);
}

public static V1.ShoppingCartInitialized Downcast(
string newEventJson
)
{
var newEvent = JsonDocument.Parse(newEventJson).RootElement;
public static V1.ShoppingCartInitialized Downcast(
string newEventJson
)
{
var newEvent = JsonDocument.Parse(newEventJson).RootElement;

return new V1.ShoppingCartInitialized(
newEvent.GetProperty("ShoppingCartId").GetGuid(),
newEvent.GetProperty("Client").GetProperty("Id").GetGuid()
);
}
return new V1.ShoppingCartInitialized(
newEvent.GetProperty("ShoppingCartId").GetGuid(),
newEvent.GetProperty("Client").GetProperty("Id").GetGuid()
);
}

[Fact]
public void UpcastObjects_Should_BeForwardCompatible()
{
// Given
var newEvent = new ShoppingCartInitialized(
Guid.NewGuid(),
new Client( Guid.NewGuid(), "Oskar the Grouch")
);
[Fact]
public void UpcastObjects_Should_BeForwardCompatible()
{
// Given
var newEvent = new ShoppingCartInitialized(
Guid.NewGuid(),
new Client( Guid.NewGuid(), "Oskar the Grouch")
);

// When
var @event = Downcast(newEvent);
// When
var @event = Downcast(newEvent);

@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(newEvent.ShoppingCartId);
@event.ClientId.Should().Be(newEvent.Client.Id);
}
@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(newEvent.ShoppingCartId);
@event.ClientId.Should().Be(newEvent.Client.Id);
}

[Fact]
public void UpcastJson_Should_BeForwardCompatible()
{
// Given
var newEvent = new ShoppingCartInitialized(
Guid.NewGuid(),
new Client( Guid.NewGuid(), "Oskar the Grouch")
);
// When
var @event = Downcast(
JsonSerializer.Serialize(newEvent)
);
[Fact]
public void UpcastJson_Should_BeForwardCompatible()
{
// Given
var newEvent = new ShoppingCartInitialized(
Guid.NewGuid(),
new Client( Guid.NewGuid(), "Oskar the Grouch")
);
// When
var @event = Downcast(
JsonSerializer.Serialize(newEvent)
);

@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(newEvent.ShoppingCartId);
@event.ClientId.Should().Be(newEvent.Client.Id);
}
@event.Should().NotBeNull();
@event.ShoppingCartId.Should().Be(newEvent.ShoppingCartId);
@event.ClientId.Should().Be(newEvent.Client.Id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Text.Json;

namespace EventsVersioning.Tests.Serializers
namespace EventsVersioning.Tests.Serializers;

public static class SystemTextJsonSerializer
{
public static class SystemTextJsonSerializer
{
public static string Serialize(this object @event) =>
JsonSerializer.Serialize(@event);
public static string Serialize(this object @event) =>
JsonSerializer.Serialize(@event);


public static T? Deserialize<T>(this string @event) =>
JsonSerializer.Deserialize<T>(@event);
}
}
public static T? Deserialize<T>(this string @event) =>
JsonSerializer.Deserialize<T>(@event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@
using Xunit;
using V1 = ECommerce.V1;

namespace EventsVersioning.Tests.SimpleMappings
namespace EventsVersioning.Tests.SimpleMappings;

public class NewNotRequiredProperty
{
public class NewNotRequiredProperty
public record ShoppingCartInitialized(
Guid ShoppingCartId,
Guid ClientId,
// Adding new not required property as nullable
DateTime? IntializedAt
);

[Fact]
public void Should_BeForwardCompatible()
{
public record ShoppingCartInitialized(
Guid ShoppingCartId,
Guid ClientId,
// Adding new not required property as nullable
DateTime? IntializedAt
);

[Fact]
public void Should_BeForwardCompatible()
{
// Given
var oldEvent = new V1.ShoppingCartInitialized(Guid.NewGuid(), Guid.NewGuid());
var json = JsonSerializer.Serialize(oldEvent);

// When
var @event = JsonSerializer.Deserialize<ShoppingCartInitialized>(json);

@event.Should().NotBeNull();
@event!.ShoppingCartId.Should().Be(oldEvent.ShoppingCartId);
@event.ClientId.Should().Be(oldEvent.ClientId);
@event.IntializedAt.Should().BeNull();
}

[Fact]
public void Should_BeBackwardCompatible()
{
// Given
var @event = new ShoppingCartInitialized(Guid.NewGuid(), Guid.NewGuid(), DateTime.UtcNow);
var json = JsonSerializer.Serialize(@event);

// When
var oldEvent = JsonSerializer.Deserialize<V1.ShoppingCartInitialized>(json);

oldEvent.Should().NotBeNull();
oldEvent!.ShoppingCartId.Should().Be(@event.ShoppingCartId);
oldEvent.ClientId.Should().Be(@event.ClientId);
}
// Given
var oldEvent = new V1.ShoppingCartInitialized(Guid.NewGuid(), Guid.NewGuid());
var json = JsonSerializer.Serialize(oldEvent);

// When
var @event = JsonSerializer.Deserialize<ShoppingCartInitialized>(json);

@event.Should().NotBeNull();
@event!.ShoppingCartId.Should().Be(oldEvent.ShoppingCartId);
@event.ClientId.Should().Be(oldEvent.ClientId);
@event.IntializedAt.Should().BeNull();
}

[Fact]
public void Should_BeBackwardCompatible()
{
// Given
var @event = new ShoppingCartInitialized(Guid.NewGuid(), Guid.NewGuid(), DateTime.UtcNow);
var json = JsonSerializer.Serialize(@event);

// When
var oldEvent = JsonSerializer.Deserialize<V1.ShoppingCartInitialized>(json);

oldEvent.Should().NotBeNull();
oldEvent!.ShoppingCartId.Should().Be(@event.ShoppingCartId);
oldEvent.ClientId.Should().Be(@event.ClientId);
}
}
Loading

0 comments on commit b6f1565

Please sign in to comment.