-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to use custom aggregations in Live lifecycle, easier recipes …
…for using explicit code. Closes GH-2941
- Loading branch information
1 parent
8bf381b
commit 297170e
Showing
6 changed files
with
165 additions
and
11 deletions.
There are no files selected for viewing
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
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
61 changes: 61 additions & 0 deletions
61
src/EventSourcingTests/Projections/using_explicit_code_for_live_aggregation.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,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using EventSourcingTests.Aggregation; | ||
using Marten; | ||
using Marten.Events; | ||
using Marten.Events.Aggregation; | ||
using Marten.Events.Projections; | ||
using Marten.Internal.Sessions; | ||
using Marten.Testing.Harness; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace EventSourcingTests.Projections; | ||
|
||
public class using_explicit_code_for_live_aggregation : OneOffConfigurationsContext | ||
{ | ||
|
||
[Fact] | ||
public async Task using_a_custom_projection_for_live_aggregation() | ||
{ | ||
StoreOptions(opts => | ||
{ | ||
opts.Projections.Add(new ExplicitCounter(), ProjectionLifecycle.Live); | ||
}); | ||
|
||
var streamId = theSession.Events.StartStream<SimpleAggregate>(new AEvent(), new AEvent(), new BEvent(), new CEvent(), new CEvent(), new CEvent()).Id; | ||
await theSession.SaveChangesAsync(); | ||
|
||
var aggregate = await theSession.Events.AggregateStreamAsync<SimpleAggregate>(streamId); | ||
aggregate.ACount.ShouldBe(2); | ||
aggregate.BCount.ShouldBe(1); | ||
aggregate.CCount.ShouldBe(3); | ||
aggregate.Id.ShouldBe(streamId); | ||
} | ||
} | ||
|
||
#region sample_using_simple_explicit_code_for_live_aggregation | ||
|
||
public class ExplicitCounter: CustomProjection<SimpleAggregate, Guid> | ||
{ | ||
public override SimpleAggregate Apply(SimpleAggregate snapshot, IReadOnlyList<IEvent> events) | ||
{ | ||
snapshot ??= new SimpleAggregate(); | ||
foreach (var e in events.Select(x => x.Data)) | ||
{ | ||
if (e is AEvent) snapshot.ACount++; | ||
if (e is BEvent) snapshot.BCount++; | ||
if (e is CEvent) snapshot.CCount++; | ||
if (e is DEvent) snapshot.DCount++; | ||
} | ||
|
||
// You have to explicitly return the new value | ||
// of the aggregated document no matter what! | ||
return snapshot; | ||
} | ||
} | ||
|
||
#endregion |
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