Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional custom event mapping to the domain repository. #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
417 changes: 240 additions & 177 deletions src/Evento.Repository/EventStoreDomainRepository.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Evento.Repository/Evento.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<RepositoryUrl>https://github.com/riccardone/Evento.Repository</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<PackageTags>EventStore, Event Sourcing, DomainRepository, Repository, DDD</PackageTags>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="eventstore.client" Version="22.0.0" />
Expand Down
6 changes: 4 additions & 2 deletions src/Evento.TestClient/Evento.TestClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>9</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="eventstore.client" Version="22.0.0" />
<ItemGroup>
<PackageReference Include="eventstore.client" Version="22.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
71 changes: 53 additions & 18 deletions src/Evento.Tests/DomainRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,61 @@
using Moq;
using NUnit.Framework;

namespace Evento.Tests
namespace Evento.Tests;

[TestFixture]
public class DomainRepositoryTests
{
[TestFixture]
public class DomainRepositoryTests
[Test]
public void When_I_save_an_aggregate_I_should_receive_the_saved_events()
{
[Test]
public void When_I_save_an_aggregate_I_should_receive_the_saved_events()
{
// Assign
const string correlationId = "correlationidexample-123";
const string testString = "test";
var cmd = new CreateFakeCommand(testString, new Dictionary<string, string>{{"$correlationId", correlationId}});
var mockConnection = new Mock<IEventStoreConnection>();
var repository = new EventStoreDomainRepository("domain", mockConnection.Object);
// Assign
const string correlationId = "correlationidexample-123";
const string testString = "test";
var cmd = new CreateFakeCommand(testString, new Dictionary<string, string>{{"$correlationId", correlationId}});
var mockConnection = new Mock<IEventStoreConnection>();
var repository = new EventStoreDomainRepository("domain", mockConnection.Object);

// Act
var results = repository.Save(new FakeHandler().Handle(cmd));
// Act
var results = repository.Save(new FakeHandler().Handle(cmd));

// Assert
Assert.IsTrue(((FakeAggregateCreated)results.Single()).Metadata["$correlationId"].Equals(correlationId));
}
// Assert
Assert.IsTrue(((FakeAggregateCreated)results.Single()).Metadata["$correlationId"].Equals(correlationId));
}

[Test]
public void CanUseCustomMapping()
{
// // Assign
// const string eventTypeName = "CustomEventType";
// const string correlationId = "correlationidexample-123";
// var metadata = new Dictionary<string, string>
// {
// { "$correlationId", correlationId },
// { EventStoreDomainRepository.EventClrTypeHeader, typeof(CustomEvent).AssemblyQualifiedName! },
// { "type", eventTypeName },
// { "created", DateTime.UtcNow.Ticks.ToString() },
// { "content-type", "application/json" }
// };
// var resolvedEvent = CreateResolvedEvent(eventTypeName, metadata);
// var mockConnection = new Mock<IEventStoreConnection>();
// mockConnection
// .Setup(
// x => x.ReadStreamEventsForwardAsync(
// It.IsAny<string>(), It.IsAny<long>(), It.IsAny<int>(), It.IsAny<bool>(), It.IsAny<UserCredentials?>()))
// .ReturnsAsync(
// CreateStreamEventsSlice(
// SliceReadStatus.Success, "stream", StreamPosition.Start, ReadDirection.Forward,
// new[] { resolvedEvent }, StreamPosition.Start + 1, StreamPosition.Start + 1, false));
//
// var customMapping = new Dictionary<string, Type> { { eventTypeName, typeof(CustomEvent) } };
// var repository = new EventStoreDomainRepository("domain", mockConnection.Object, customMapping);
//
// // Act
// var result = repository.GetById<CustomAggregate>("test-id");
//
// // Assert
// Assert.IsNotNull(result);
// Assert.IsTrue(result.Events.OfType<CustomEvent>().Any());
}
}
}
3 changes: 2 additions & 1 deletion src/Evento.Tests/Evento.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
40 changes: 20 additions & 20 deletions src/Evento/DomainRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Evento
namespace Evento;

public abstract class DomainRepositoryBase : IDomainRepository
{
public abstract class DomainRepositoryBase : IDomainRepository
{
public abstract IEnumerable<Event> Save<TAggregate>(TAggregate aggregate) where TAggregate : IAggregate;
public abstract Task<IEnumerable<Event>> SaveAsync<TAggregate>(TAggregate aggregate) where TAggregate : IAggregate;
public abstract TResult GetById<TResult>(string id) where TResult : IAggregate, new();
public abstract TResult GetById<TResult>(string id, int eventsToLoad) where TResult : IAggregate, new();
public abstract void DeleteAggregate<TAggregate>(string correlationId, bool hard);
public abstract IEnumerable<Event> Save<TAggregate>(TAggregate aggregate) where TAggregate : IAggregate;
public abstract Task<IEnumerable<Event>> SaveAsync<TAggregate>(TAggregate aggregate) where TAggregate : IAggregate;
public abstract TResult GetById<TResult>(string id) where TResult : IAggregate, new();
public abstract TResult GetById<TResult>(string id, int eventsToLoad) where TResult : IAggregate, new();
public abstract void DeleteAggregate<TAggregate>(string correlationId, bool hard);

protected int CalculateExpectedVersion<T>(IAggregate aggregate, List<T> events)
{
var expectedVersion = aggregate.Version - events.Count;
return expectedVersion;
}
protected TResult BuildAggregate<TResult>(IEnumerable<Event> events) where TResult : IAggregate, new()
protected int CalculateExpectedVersion<T>(IAggregate aggregate, List<T> events)
{
var expectedVersion = aggregate.Version - events.Count;
return expectedVersion;
}

protected TResult BuildAggregate<TResult>(IEnumerable<Event> events) where TResult : IAggregate, new()
{
var result = new TResult();
foreach (var @event in events)
{
var result = new TResult();
foreach (var @event in events)
{
result.ApplyEvent(@event);
}
return result;
result.ApplyEvent(@event);
}
return result;
}
}
2 changes: 2 additions & 0 deletions src/Evento/Evento.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
<RepositoryType>git</RepositoryType>
<PackageTags>EventStore, DomainRepository, Aggregate, Command Handlers, Event Sourcing</PackageTags>
<AssemblyVersion>4.3.0.0</AssemblyVersion>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>
</Project>