diff --git a/src/NexusMods.EventSourcing.Abstractions/IEventIngester.cs b/src/NexusMods.EventSourcing.Abstractions/IEventIngester.cs index d7ce0a4c..71eb93db 100644 --- a/src/NexusMods.EventSourcing.Abstractions/IEventIngester.cs +++ b/src/NexusMods.EventSourcing.Abstractions/IEventIngester.cs @@ -8,5 +8,10 @@ namespace NexusMods.EventSourcing.Abstractions; /// public interface IEventIngester { + /// + /// Ingests the given event into the context. + /// + /// + /// public ValueTask Ingest(IEvent @event); } diff --git a/src/NexusMods.EventSourcing.Abstractions/IEventSerializer.cs b/src/NexusMods.EventSourcing.Abstractions/IEventSerializer.cs new file mode 100644 index 00000000..7166fea5 --- /dev/null +++ b/src/NexusMods.EventSourcing.Abstractions/IEventSerializer.cs @@ -0,0 +1,18 @@ +namespace NexusMods.EventSourcing.Abstractions; + +public interface IEventSerializer +{ + /// + /// Serializes the given event into a byte array. + /// + /// + /// + public byte[] Serialize(IEvent @event); + + /// + /// Deserializes the given byte array into an event. + /// + /// + /// + public IEvent Deserialize(byte[] data); +} diff --git a/src/NexusMods.EventSourcing/EventSerializer.cs b/src/NexusMods.EventSourcing/EventSerializer.cs index 313df57d..e684d9dd 100644 --- a/src/NexusMods.EventSourcing/EventSerializer.cs +++ b/src/NexusMods.EventSourcing/EventSerializer.cs @@ -8,7 +8,7 @@ namespace NexusMods.EventSourcing; -public class EventSerializer +public class EventSerializer : IEventSerializer { public EventSerializer(IEnumerable events) { diff --git a/tests/NexusMods.EventSourcing.TestModel/Events/AddMod.cs b/tests/NexusMods.EventSourcing.TestModel/Events/AddMod.cs index a43e6032..22eee809 100644 --- a/tests/NexusMods.EventSourcing.TestModel/Events/AddMod.cs +++ b/tests/NexusMods.EventSourcing.TestModel/Events/AddMod.cs @@ -7,21 +7,24 @@ namespace NexusMods.EventSourcing.TestModel.Events; [EventId("7DC8F80B-50B6-43B7-B805-43450E9F0C2B")] [MemoryPackable] -public partial class AddMod : IEvent +public partial record AddMod(string Name, bool Enabled, EntityId ModId, EntityId LoadoutId) : IEvent { - public required string Name { get; init; } = string.Empty; - public required bool Enabled { get; init; } = true; - public required EntityId Id { get; init; } - public required EntityId LoadoutId { get; init; } - public async ValueTask Apply(T context) where T : IEventContext { - context.New(Id); - context.Emit(Id, Mod._name, Name); - context.Emit(Id, Mod._enabled, Enabled); - context.Emit(Id, Mod._loadout, LoadoutId); - context.Emit(LoadoutId, Loadout._mods, Id); + context.New(ModId); + context.Emit(ModId, Mod._name, Name); + context.Emit(ModId, Mod._enabled, Enabled); + context.Emit(ModId, Mod._loadout, LoadoutId); + context.Emit(LoadoutId, Loadout._mods, ModId); } - public static AddMod Create(string name, EntityId loadoutId) => new() { Name = name, Enabled = true, Id = EntityId.NewId(), LoadoutId = loadoutId }; + /// + /// Creates a event that adds a new mod to the given loadout giving it the given name. + /// + /// + /// + /// + /// + public static AddMod Create(string name, EntityId loadoutId, bool enabled = true) + => new(name, enabled, EntityId.NewId(), loadoutId); } diff --git a/tests/NexusMods.EventSourcing.Tests/Contexts/InMemoryEventStore.cs b/tests/NexusMods.EventSourcing.TestModel/InMemoryEventStore.cs similarity index 91% rename from tests/NexusMods.EventSourcing.Tests/Contexts/InMemoryEventStore.cs rename to tests/NexusMods.EventSourcing.TestModel/InMemoryEventStore.cs index 379d6863..41a7a97b 100644 --- a/tests/NexusMods.EventSourcing.Tests/Contexts/InMemoryEventStore.cs +++ b/tests/NexusMods.EventSourcing.TestModel/InMemoryEventStore.cs @@ -1,11 +1,9 @@ -using MemoryPack; -using MemoryPack.Formatters; using NexusMods.EventSourcing.Abstractions; -using NexusMods.EventSourcing.TestModel.Events; -namespace NexusMods.EventSourcing.Tests.Contexts; +namespace NexusMods.EventSourcing.TestModel; -public class InMemoryEventStore(EventSerializer serializer) : IEventStore +public class InMemoryEventStore(TSerializer serializer) : IEventStore +where TSerializer : IEventSerializer { private readonly Dictionary> _events = new(); diff --git a/tests/NexusMods.EventSourcing.Tests/BasicFunctionalityTests.cs b/tests/NexusMods.EventSourcing.Tests/BasicFunctionalityTests.cs index 6667b0f4..b65c1765 100644 --- a/tests/NexusMods.EventSourcing.Tests/BasicFunctionalityTests.cs +++ b/tests/NexusMods.EventSourcing.Tests/BasicFunctionalityTests.cs @@ -67,7 +67,7 @@ public async void CanDeleteEntities() loadout.Mods.Count().Should().Be(2); - await _ctx.Add(new DeleteMod(modEvent1.Id, loadoutEvent.Id)); + await _ctx.Add(new DeleteMod(modEvent1.ModId, loadoutEvent.Id)); loadout.Mods.Count().Should().Be(1); diff --git a/tests/NexusMods.EventSourcing.Tests/Contexts/TestContext.cs b/tests/NexusMods.EventSourcing.Tests/Contexts/TestContext.cs index bec5c647..edabf27a 100644 --- a/tests/NexusMods.EventSourcing.Tests/Contexts/TestContext.cs +++ b/tests/NexusMods.EventSourcing.Tests/Contexts/TestContext.cs @@ -1,12 +1,13 @@ using System.Runtime.CompilerServices; using Microsoft.Extensions.Logging; using NexusMods.EventSourcing.Abstractions; +using NexusMods.EventSourcing.TestModel; namespace NexusMods.EventSourcing.Tests.Contexts; public class TestContext(ILogger logger, EventSerializer serializer) : IEntityContext { - private readonly InMemoryEventStore _store = new(serializer); + private readonly InMemoryEventStore _store = new(serializer); private readonly Dictionary _entities = new(); private readonly Dictionary> _values = new();