From 8ef01d8f456b82d835e0b9a711806624066f7650 Mon Sep 17 00:00:00 2001 From: Ben Edwards Date: Mon, 25 Mar 2024 14:19:55 +1100 Subject: [PATCH] Ensure that applying events to an aggregate returns the aggregate even if there are no events to apply. --- .../GeneratedAggregateProjectionBase.CodeGen.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Marten/Events/Aggregation/GeneratedAggregateProjectionBase.CodeGen.cs b/src/Marten/Events/Aggregation/GeneratedAggregateProjectionBase.CodeGen.cs index 51639ba48f..c7bd6a29c3 100644 --- a/src/Marten/Events/Aggregation/GeneratedAggregateProjectionBase.CodeGen.cs +++ b/src/Marten/Events/Aggregation/GeneratedAggregateProjectionBase.CodeGen.cs @@ -280,14 +280,11 @@ private void buildLiveAggregationType(GeneratedAssembly assembly) buildMethod.DerivedVariables.Add(Variable.For("(IQuerySession)session")); - buildMethod.Frames.Code("if (!events.Any()) return null;"); - - buildMethod.Frames.Add(new DeclareAggregateFrame(typeof(T))); - - var callCreateAggregateFrame = new CallCreateAggregateFrame(_createMethods); - // This is the existing snapshot passed into the LiveAggregator var snapshot = buildMethod.Arguments.Single(x => x.VariableType == typeof(T)); + buildMethod.Frames.Code($"if (!events.Any()) return {snapshot.Usage};"); + + var callCreateAggregateFrame = new CallCreateAggregateFrame(_createMethods); callCreateAggregateFrame.CoalesceAssignTo(snapshot); buildMethod.Frames.Add(callCreateAggregateFrame);