Skip to content

Commit

Permalink
Aggregate: Pre process in applied scope (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Jur Balledux <[email protected]>
  • Loading branch information
JurJean and Jur Balledux authored Jul 29, 2024
1 parent e20b232 commit 17ef988
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion props/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<Description>Qowaiv implements common, universal domain objects. These types form the base of your domain model.</Description>
<Authors>Ad van der Hoeven;Corniel Nobel;Erik Ammerlaan;Jack Kester;Marcel Strik;Mitchell Smit;Patrick Evers;Wilko Frieke</Authors>
<Authors>Ad van der Hoeven;Corniel Nobel;Erik Ammerlaan;Jack Kester;Jur Balledux;Marcel Strik;Mitchell Smit;Patrick Evers;Wilko Frieke</Authors>
<Owners>Qowaiv community</Owners>
<PackageTags>qowaiv domain model</PackageTags>
<Company>Qowaiv community</Company>
Expand Down
11 changes: 7 additions & 4 deletions src/Qowaiv.DomainModel/Aggregate_TAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ protected Result<TAggregate> ApplyEvents(params object[] events)
[Pure]
protected Result<TAggregate> Apply(IEnumerable<object> events)
{
Guard.NotNull(events, nameof(events));

var updated = Clone();
var append = AppendOnlyCollection.Empty.Add(events.Select(PreProcessEvent));
updated.Replay(append);
var append = AppendOnlyCollection.Empty;

foreach (var @event in Guard.NotNull(events, nameof(events)).Select(updated.PreProcessEvent))
{
updated.Dispatcher.When(@event);
append = append.Add(@event);
}

var result = updated.Validator.Validate(updated);
if (result.IsValid)
Expand Down
4 changes: 3 additions & 1 deletion src/Qowaiv.DomainModel/Qowaiv.DomainModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
<TargetFrameworks>netstandard2.0;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<OutputType>library</OutputType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
<PackageId>Qowaiv.DomainModel</PackageId>
<PackageReleaseNotes>
v1.1.2
- Aggregate: PreProcessEvent in applied scope. #44
v1.1.1
- CommandProcessor made thread-safe. #42
v1.1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Qowaiv.DomainModel;
using NUnit.Framework.Internal.Execution;
using Qowaiv.DomainModel;

namespace Event_sourced_aggregate_specs
{
Expand Down Expand Up @@ -70,6 +71,21 @@ public void skips_unknown_event_types()
var updated = aggregate.TestApplyChange(new NameUpdated("unknown")).Should().BeValid().Value;
updated.Version.Should().Be(1);
}

[Test]
public void pre_processed_in_applied_scope()
{
var aggregate = new TestPreProcessEventAggregate();
var updated = aggregate
.TestPreprocessEvents(new NameUpdated("Jimi Hendrix"), new NameUpdated("unknown"))
.Should().BeValid().Value;

updated.Buffer.Uncommitted.Should().BeEquivalentTo(new object[]
{
new NameUpdated("Jimi Hendrix"),
new NameUpdated("Jimi Hendrix"),
});
}
}

internal class TestApplyChangeAggregate : Aggregate<TestApplyChangeAggregate, Guid>
Expand All @@ -79,4 +95,31 @@ public TestApplyChangeAggregate()

public Result<TestApplyChangeAggregate> TestApplyChange(object @event) => ApplyEvent(@event);
}

internal class TestPreProcessEventAggregate : Aggregate<TestPreProcessEventAggregate, Guid>
{
private string Name = string.Empty;

public TestPreProcessEventAggregate()
: base(Guid.NewGuid(), Qowaiv.Validation.Abstractions.Validator.Empty<TestPreProcessEventAggregate>()) { }

public Result<TestPreProcessEventAggregate> TestPreprocessEvents(params object[] events) => ApplyEvents(events);

protected override object PreProcessEvent(object @event)
{
if (@event is not NameUpdated e)
{
return @event;
}

var newName = Name == string.Empty ? e.Name : Name;
return new NameUpdated(newName);
}


internal void When(NameUpdated @event)
{
Name = @event.Name;
}
}
}

0 comments on commit 17ef988

Please sign in to comment.