-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplified the serialisation handling
- Loading branch information
1 parent
c8220cd
commit 9f0fe9a
Showing
8 changed files
with
267 additions
and
50 deletions.
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
...s/SnapshotTesting/PackageSnapshotTests.my_assembly_has_no_public_api_changes.verified.txt
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,172 @@ | ||
[assembly: System.Runtime.Versioning.TargetFramework(".NETCoreApp,Version=v8.0", FrameworkDisplayName=".NET 8.0")] | ||
namespace HotelManagement.EventStore | ||
{ | ||
public class CommandHandler<T> | ||
{ | ||
public CommandHandler(System.Func<T, object, T> evolve, System.Func<T> getInitial) { } | ||
public System.Threading.Tasks.Task Handle(HotelManagement.EventStore.IEventStore eventStore, string id, System.Func<T, object[]> handle, System.Threading.CancellationToken ct) { } | ||
} | ||
public class EventSerializer : HotelManagement.EventStore.IEventSerializer | ||
{ | ||
public EventSerializer(HotelManagement.EventStore.EventTypeMapping mapping, HotelManagement.EventStore.EventTransformations transformations, HotelManagement.EventStore.StreamTransformations? streamTransformations = null) { } | ||
public object? Deserialize(HotelManagement.EventStore.SerializedEvent serializedEvent) { } | ||
public System.Collections.Generic.List<object?> Deserialize(System.Collections.Generic.List<HotelManagement.EventStore.SerializedEvent> events) { } | ||
public HotelManagement.EventStore.SerializedEvent Serialize(object @event) { } | ||
} | ||
public class EventTransformations | ||
{ | ||
public EventTransformations() { } | ||
public HotelManagement.EventStore.EventTransformations Register<TEvent>(string eventTypeName, System.Func<System.Text.Json.JsonDocument, TEvent> transformJson) | ||
where TEvent : notnull { } | ||
public HotelManagement.EventStore.EventTransformations Register<TOldEvent, TEvent>(string eventTypeName, System.Func<TOldEvent, TEvent> transformEvent) | ||
where TOldEvent : notnull | ||
where TEvent : notnull { } | ||
public bool TryTransform(string eventTypeName, string json, out object? result) { } | ||
} | ||
public class EventTypeMapping | ||
{ | ||
public EventTypeMapping() { } | ||
public HotelManagement.EventStore.EventTypeMapping CustomMap(System.Type eventType, params string[] eventTypeNames) { } | ||
public HotelManagement.EventStore.EventTypeMapping CustomMap<T>(params string[] eventTypeNames) { } | ||
public string ToName(System.Type eventType) { } | ||
public string ToName<TEventType>() { } | ||
public System.Type? ToType(string eventTypeName) { } | ||
} | ||
public interface IEventSerializer | ||
{ | ||
object? Deserialize(HotelManagement.EventStore.SerializedEvent serializedEvent); | ||
System.Collections.Generic.List<object?> Deserialize(System.Collections.Generic.List<HotelManagement.EventStore.SerializedEvent> events); | ||
HotelManagement.EventStore.SerializedEvent Serialize(object @event); | ||
} | ||
public interface IEventStore | ||
{ | ||
System.Threading.Tasks.ValueTask AppendToStream(string streamId, System.Collections.Generic.IEnumerable<object> newEvents, System.Threading.CancellationToken ct = default); | ||
System.Threading.Tasks.ValueTask<object[]> ReadStream(string streamId, System.Threading.CancellationToken ct = default); | ||
} | ||
public class InMemoryEventStore : HotelManagement.EventStore.IEventStore | ||
{ | ||
public InMemoryEventStore(HotelManagement.EventStore.EventSerializer eventSerializer) { } | ||
public System.Threading.Tasks.ValueTask AppendToStream(string streamId, System.Collections.Generic.IEnumerable<object> newEvents, System.Threading.CancellationToken _ = default) { } | ||
public System.Threading.Tasks.ValueTask<object[]> ReadStream(string streamId, System.Threading.CancellationToken _ = default) { } | ||
} | ||
public class SerializedEvent : System.IEquatable<HotelManagement.EventStore.SerializedEvent> | ||
{ | ||
public SerializedEvent(string EventType, string Data, string MetaData = "") { } | ||
public string Data { get; init; } | ||
public string EventType { get; init; } | ||
public string MetaData { get; init; } | ||
} | ||
public class StreamTransformations | ||
{ | ||
public StreamTransformations() { } | ||
public HotelManagement.EventStore.StreamTransformations Register(System.Func<System.Collections.Generic.List<HotelManagement.EventStore.SerializedEvent>, System.Collections.Generic.List<HotelManagement.EventStore.SerializedEvent>> transformJson) { } | ||
public System.Collections.Generic.List<HotelManagement.EventStore.SerializedEvent> Transform(System.Collections.Generic.List<HotelManagement.EventStore.SerializedEvent> events) { } | ||
} | ||
} | ||
namespace HotelManagement.GuestStayAccounts | ||
{ | ||
public class ChargeRecorded : System.IEquatable<HotelManagement.GuestStayAccounts.ChargeRecorded> | ||
{ | ||
public ChargeRecorded(string GuestStayAccountId, decimal Amount, System.DateTimeOffset RecordedAt) { } | ||
public decimal Amount { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
public System.DateTimeOffset RecordedAt { get; init; } | ||
} | ||
public class CheckIn : System.IEquatable<HotelManagement.GuestStayAccounts.CheckIn> | ||
{ | ||
public CheckIn(string ClerkId, string GuestStayId, string RoomId, System.DateTimeOffset Now) { } | ||
public string ClerkId { get; init; } | ||
public string GuestStayId { get; init; } | ||
public System.DateTimeOffset Now { get; init; } | ||
public string RoomId { get; init; } | ||
} | ||
public class CheckOut : System.IEquatable<HotelManagement.GuestStayAccounts.CheckOut> | ||
{ | ||
public CheckOut(string ClerkId, string GuestStayAccountId, System.DateTimeOffset Now) { } | ||
public string ClerkId { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
public System.DateTimeOffset Now { get; init; } | ||
} | ||
public class GuestCheckedIn : System.IEquatable<HotelManagement.GuestStayAccounts.GuestCheckedIn> | ||
{ | ||
public GuestCheckedIn(string GuestStayAccountId, string GuestStayId, string RoomId, string ClerkId, System.DateTimeOffset CheckedInAt) { } | ||
public System.DateTimeOffset CheckedInAt { get; init; } | ||
public string ClerkId { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
public string GuestStayId { get; init; } | ||
public string RoomId { get; init; } | ||
} | ||
public class GuestCheckedOut : System.IEquatable<HotelManagement.GuestStayAccounts.GuestCheckedOut> | ||
{ | ||
public GuestCheckedOut(string GuestStayAccountId, string ClerkId, System.DateTimeOffset CheckedOutAt) { } | ||
public System.DateTimeOffset CheckedOutAt { get; init; } | ||
public string ClerkId { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
} | ||
public class GuestCheckoutFailed : System.IEquatable<HotelManagement.GuestStayAccounts.GuestCheckoutFailed> | ||
{ | ||
public GuestCheckoutFailed(string GuestStayAccountId, string ClerkId, HotelManagement.GuestStayAccounts.GuestCheckoutFailed.FailureReason Reason, System.DateTimeOffset FailedAt) { } | ||
public string ClerkId { get; init; } | ||
public System.DateTimeOffset FailedAt { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
public HotelManagement.GuestStayAccounts.GuestCheckoutFailed.FailureReason Reason { get; init; } | ||
public enum FailureReason | ||
{ | ||
NotOpened = 0, | ||
BalanceNotSettled = 1, | ||
} | ||
} | ||
public class GuestStayAccount : System.IEquatable<HotelManagement.GuestStayAccounts.GuestStayAccount> | ||
{ | ||
public static readonly HotelManagement.GuestStayAccounts.GuestStayAccount Initial; | ||
public GuestStayAccount(string Id, [System.Runtime.CompilerServices.DecimalConstant(0, 0, 0u, 0u, 0u)] decimal Balance, HotelManagement.GuestStayAccounts.GuestStayAccountStatus Status = 1) { } | ||
public bool IsSettled { get; } | ||
public decimal Balance { get; init; } | ||
public string Id { get; init; } | ||
public HotelManagement.GuestStayAccounts.GuestStayAccountStatus Status { get; init; } | ||
public static HotelManagement.GuestStayAccounts.GuestStayAccount Evolve(HotelManagement.GuestStayAccounts.GuestStayAccount state, object @event) { } | ||
public static string GuestStayAccountId(string guestStayId, string roomId, System.DateOnly checkInDate) { } | ||
} | ||
public static class GuestStayAccountDecider | ||
{ | ||
public static HotelManagement.GuestStayAccounts.GuestCheckedIn CheckIn(HotelManagement.GuestStayAccounts.CheckIn command, HotelManagement.GuestStayAccounts.GuestStayAccount state) { } | ||
public static object CheckOut(HotelManagement.GuestStayAccounts.CheckOut command, HotelManagement.GuestStayAccounts.GuestStayAccount state) { } | ||
public static HotelManagement.GuestStayAccounts.ChargeRecorded RecordCharge(HotelManagement.GuestStayAccounts.RecordCharge command, HotelManagement.GuestStayAccounts.GuestStayAccount state) { } | ||
public static HotelManagement.GuestStayAccounts.PaymentRecorded RecordPayment(HotelManagement.GuestStayAccounts.RecordPayment command, HotelManagement.GuestStayAccounts.GuestStayAccount state) { } | ||
} | ||
public class GuestStayAccountService | ||
{ | ||
public GuestStayAccountService(HotelManagement.EventStore.IEventStore eventStore) { } | ||
public System.Threading.Tasks.Task CheckIn(HotelManagement.GuestStayAccounts.CheckIn command, System.Threading.CancellationToken ct = default) { } | ||
public System.Threading.Tasks.Task CheckOut(HotelManagement.GuestStayAccounts.CheckOut command, System.Threading.CancellationToken ct = default) { } | ||
public System.Threading.Tasks.Task RecordCharge(HotelManagement.GuestStayAccounts.RecordCharge command, System.Threading.CancellationToken ct = default) { } | ||
public System.Threading.Tasks.Task RecordPayment(HotelManagement.GuestStayAccounts.RecordPayment command, System.Threading.CancellationToken ct = default) { } | ||
} | ||
public enum GuestStayAccountStatus | ||
{ | ||
NotExisting = 0, | ||
Opened = 1, | ||
CheckedOut = 2, | ||
} | ||
public class PaymentRecorded : System.IEquatable<HotelManagement.GuestStayAccounts.PaymentRecorded> | ||
{ | ||
public PaymentRecorded(string GuestStayAccountId, decimal Amount, System.DateTimeOffset RecordedAt) { } | ||
public decimal Amount { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
public System.DateTimeOffset RecordedAt { get; init; } | ||
} | ||
public class RecordCharge : System.IEquatable<HotelManagement.GuestStayAccounts.RecordCharge> | ||
{ | ||
public RecordCharge(string GuestStayAccountId, decimal Amount, System.DateTimeOffset Now) { } | ||
public decimal Amount { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
public System.DateTimeOffset Now { get; init; } | ||
} | ||
public class RecordPayment : System.IEquatable<HotelManagement.GuestStayAccounts.RecordPayment> | ||
{ | ||
public RecordPayment(string GuestStayAccountId, decimal Amount, System.DateTimeOffset Now) { } | ||
public decimal Amount { get; init; } | ||
public string GuestStayAccountId { get; init; } | ||
public System.DateTimeOffset Now { get; init; } | ||
} | ||
} |
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
19 changes: 9 additions & 10 deletions
19
Sample/EventsVersioning/Talk/HotelManagement/EventStore/CommandHandler.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 |
---|---|---|
@@ -1,24 +1,23 @@ | ||
namespace HotelManagement.EventStore; | ||
|
||
public class CommandHandler<T, TEvent>( | ||
IEventStore eventStore, | ||
Func<T, TEvent, T> evolve, | ||
public class CommandHandler<T>( | ||
Func<T, object, T> evolve, | ||
Func<T> getInitial | ||
) where TEvent : notnull | ||
) | ||
{ | ||
public async Task GetAndUpdate( | ||
Guid id, | ||
Func<T, TEvent[]> handle, | ||
public async Task Handle( | ||
IEventStore eventStore, | ||
string id, | ||
Func<T, object[]> handle, | ||
CancellationToken ct | ||
) | ||
{ | ||
var events = await eventStore.ReadStream<TEvent>(id, ct); | ||
var events = await eventStore.ReadStream(id, ct); | ||
|
||
var state = events.Aggregate(getInitial(), evolve); | ||
|
||
var result = handle(state); | ||
|
||
if(result.Length > 0) | ||
await eventStore.AppendToStream(id, result.Cast<object>(), ct); | ||
await eventStore.AppendToStream(id, result, ct); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
Sample/EventsVersioning/Talk/HotelManagement/GuestStayAccounts/ApplicationLogic.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,35 @@ | ||
using HotelManagement.EventStore; | ||
|
||
namespace HotelManagement.GuestStayAccounts; | ||
|
||
public class GuestStayAccountService(IEventStore eventStore) | ||
{ | ||
private readonly CommandHandler<GuestStayAccount> commandHandler = | ||
new(GuestStayAccount.Evolve, () => GuestStayAccount.Initial); | ||
|
||
public Task CheckIn(CheckIn command, CancellationToken ct = default) | ||
{ | ||
var guestStayAccountId = GuestStayAccount.GuestStayAccountId( | ||
command.GuestStayId, command.RoomId, DateOnly.FromDateTime(command.Now.Date) | ||
); | ||
|
||
return commandHandler.Handle(eventStore, guestStayAccountId, | ||
state => [GuestStayAccountDecider.CheckIn(command, state)], ct | ||
); | ||
} | ||
|
||
public Task RecordCharge(RecordCharge command, CancellationToken ct = default) => | ||
commandHandler.Handle(eventStore, command.GuestStayAccountId, | ||
state => [GuestStayAccountDecider.RecordCharge(command, state)], ct | ||
); | ||
|
||
public Task RecordPayment(RecordPayment command, CancellationToken ct = default) => | ||
commandHandler.Handle(eventStore, command.GuestStayAccountId, | ||
state => [GuestStayAccountDecider.RecordPayment(command, state)], ct | ||
); | ||
|
||
public Task CheckOut(CheckOut command, CancellationToken ct = default) => | ||
commandHandler.Handle(eventStore, command.GuestStayAccountId, | ||
state => [GuestStayAccountDecider.CheckOut(command, state)], ct | ||
); | ||
} |
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.