Skip to content

Commit

Permalink
Allow customization of the snapshot projection
Browse files Browse the repository at this point in the history
  • Loading branch information
Xzelsius committed Oct 13, 2023
1 parent 21e172a commit 4ea941f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using Marten.Events.Aggregation;
using Marten.Events.Projections;
using Marten.Testing.Harness;
using NSubstitute;
using Xunit;

namespace EventSourcingTests.Projections;

public class inline_aggregation_with_custom_projection_configuration : OneOffConfigurationsContext
{
[Fact]
public void does_call_custom_projection_configuration()
{
var configureProjection = Substitute.For<Action<SingleStreamProjection<QuestParty>>>();

StoreOptions(_ =>
{
_.Projections.Snapshot<QuestParty>(SnapshotLifecycle.Inline, configureProjection);
});

configureProjection.Received(1).Invoke(Arg.Any<SingleStreamProjection<QuestParty>>());
}
}
27 changes: 24 additions & 3 deletions src/Marten/Events/Projections/ProjectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public MartenRegistry.DocumentMappingExpression<T> LiveStreamAggregation<T>(
Action<AsyncOptions> asyncConfiguration = null
)
{
var expression = singleStreamProjection<T>(ProjectionLifecycle.Live, asyncConfiguration);
var expression = singleStreamProjection<T>(ProjectionLifecycle.Live, null, asyncConfiguration);

// Hack to address https://github.com/JasperFx/marten/issues/2610
_options.Storage.MappingFor(typeof(T)).SkipSchemaGeneration = true;
Expand All @@ -190,17 +190,36 @@ public MartenRegistry.DocumentMappingExpression<T> LiveStreamAggregation<T>(
/// <param name="lifecycle">Override the snapshot lifecycle. The default is Inline</param>
/// <param name="asyncConfiguration">
/// Optional configuration including teardown instructions for the usage of this
/// projection within the async projection daempon
/// projection within the async projection daemon
/// </param>
/// <returns>The extended storage configuration for document T</returns>
public MartenRegistry.DocumentMappingExpression<T> Snapshot<T>(
SnapshotLifecycle lifecycle,
Action<AsyncOptions> asyncConfiguration = null
) =>
singleStreamProjection<T>(lifecycle.Map(), null, asyncConfiguration);

/// <summary>
/// Perform automated snapshot on each event for selected entity type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="lifecycle">Override the snapshot lifecycle. The default is Inline</param>
/// <param name="configureProjection">Use it to further customize the projection.</param>
/// <param name="asyncConfiguration">
/// Optional configuration including teardown instructions for the usage of this
/// projection within the async projection daemon
/// </param>
/// <returns>The extended storage configuration for document T</returns>
public MartenRegistry.DocumentMappingExpression<T> Snapshot<T>(
SnapshotLifecycle lifecycle,
Action<SingleStreamProjection<T>> configureProjection,
Action<AsyncOptions> asyncConfiguration = null
) =>
singleStreamProjection<T>(lifecycle.Map(), asyncConfiguration);
singleStreamProjection<T>(lifecycle.Map(), configureProjection, asyncConfiguration);

private MartenRegistry.DocumentMappingExpression<T> singleStreamProjection<T>(
ProjectionLifecycle lifecycle,
Action<SingleStreamProjection<T>> configureProjection = null,
Action<AsyncOptions> asyncConfiguration = null
)
{
Expand All @@ -209,6 +228,8 @@ private MartenRegistry.DocumentMappingExpression<T> singleStreamProjection<T>(

var source = new SingleStreamProjection<T> { Lifecycle = lifecycle };

configureProjection?.Invoke(source);

asyncConfiguration?.Invoke(source.Options);

source.AssembleAndAssertValidity();
Expand Down

0 comments on commit 4ea941f

Please sign in to comment.