Skip to content

Commit

Permalink
little bit of input validation on projection registration to prevent …
Browse files Browse the repository at this point in the history
…confusion on Snapshot registrations
  • Loading branch information
jeremydmiller committed Jan 24, 2025
1 parent 2e97d85 commit af8187d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/EventSourcingTests/Projections/SingleStreamProjectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using EventSourcingTests.Aggregation;
using Marten;
using Marten.Events;
Expand Down Expand Up @@ -41,4 +42,15 @@ public void do_not_set_mapping_to_UseVersionFromMatchingStream_when_rich_append(

mapping.UseVersionFromMatchingStream.ShouldBeFalse();
}

[Fact]
public void you_cannot_accidentally_try_to_add_single_stream_projection_through_the_snapshot()
{
var options = new StoreOptions();

Should.Throw<InvalidOperationException>(() =>
{
options.Projections.Snapshot<AllGood>(SnapshotLifecycle.Inline);
});
}
}
4 changes: 4 additions & 0 deletions src/Marten/Events/Projections/ProjectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ private MartenRegistry.DocumentMappingExpression<T> singleStreamProjection<T>(
Action<AsyncOptions> asyncConfiguration = null
)
{
if (typeof(T).CanBeCastTo<IProjectionSource>())
throw new InvalidOperationException(
$"This registration mechanism can only be used for an aggregate type that is 'self-aggregating'. Please use the Projections.Add() API instead to register {typeof(T).FullNameInCode()}");

// Make sure there's a DocumentMapping for the aggregate
var expression = _options.Schema.For<T>();

Expand Down

0 comments on commit af8187d

Please sign in to comment.