diff --git a/src/EventSourcingTests/Bugs/Bug_fetch_for_writing_cache.cs b/src/EventSourcingTests/Bugs/Bug_fetch_for_writing_cache.cs index 7d961b528a..f305f3cf4c 100644 --- a/src/EventSourcingTests/Bugs/Bug_fetch_for_writing_cache.cs +++ b/src/EventSourcingTests/Bugs/Bug_fetch_for_writing_cache.cs @@ -5,6 +5,7 @@ using Marten.Testing.Harness; using System; using System.Threading.Tasks; +using Shouldly; using Xunit; namespace EventSourcingTests.Bugs; @@ -29,12 +30,14 @@ public async Task Test() await theSession.SaveChangesAsync(); var test = await theSession.Events.FetchForWriting(streamKey); + test.Aggregate.Name.ShouldBe("foo"); + test.AppendOne(new NamedEvent2("bar")); //await theSession.Events.AppendOptimistic(streamKey, new NamedEvent2("bar")); If I commented the two lines above and uncommented this one it works fine await theSession.SaveChangesAsync(); test = await theSession.Events.FetchForWriting(streamKey); - Assert.Equal("bar", test.Aggregate.Name); + test.Aggregate.Name.ShouldBe("bar"); } } diff --git a/src/Marten/Events/Fetching/FetchInlinedPlan.cs b/src/Marten/Events/Fetching/FetchInlinedPlan.cs index 58088c41ee..0996b9de45 100644 --- a/src/Marten/Events/Fetching/FetchInlinedPlan.cs +++ b/src/Marten/Events/Fetching/FetchInlinedPlan.cs @@ -14,28 +14,31 @@ internal class FetchInlinedPlan: IAggregateFetchPlan where { private readonly EventGraph _events; private readonly IEventIdentityStrategy _identityStrategy; - private readonly IDocumentStorage _storage; - internal FetchInlinedPlan(EventGraph events, IEventIdentityStrategy identityStrategy, - IDocumentStorage storage) + internal FetchInlinedPlan(EventGraph events, IEventIdentityStrategy identityStrategy) { _events = events; _identityStrategy = identityStrategy; - _storage = storage; } public async Task> FetchForWriting(DocumentSessionBase session, TId id, bool forUpdate, CancellationToken cancellation = default) { - await _identityStrategy.EnsureEventStorageExists(session, cancellation).ConfigureAwait(false); - await session.Database.EnsureStorageExistsAsync(typeof(TDoc), cancellation).ConfigureAwait(false); - + IDocumentStorage storage = null; if (session.Options.Events.UseIdentityMapForInlineAggregates) { + storage = (IDocumentStorage)session.Options.Providers.StorageFor().IdentityMap; // Opt into the identity map mechanics for this aggregate type just in case // you're using a lightweight session session.UseIdentityMapFor(); } + else + { + storage = session.StorageFor(); + } + + await _identityStrategy.EnsureEventStorageExists(session, cancellation).ConfigureAwait(false); + await session.Database.EnsureStorageExistsAsync(typeof(TDoc), cancellation).ConfigureAwait(false); if (forUpdate) { @@ -47,7 +50,7 @@ public async Task> FetchForWriting(DocumentSessionBase sessio builder.StartNewCommand(); - var handler = new LoadByIdHandler(_storage, id); + var handler = new LoadByIdHandler(storage, id); handler.ConfigureCommand(builder, session); long version = 0; @@ -86,6 +89,19 @@ public async Task> FetchForWriting(DocumentSessionBase sessio public async Task> FetchForWriting(DocumentSessionBase session, TId id, long expectedStartingVersion, CancellationToken cancellation = default) { + IDocumentStorage storage = null; + if (session.Options.Events.UseIdentityMapForInlineAggregates) + { + storage = (IDocumentStorage)session.Options.Providers.StorageFor(); + // Opt into the identity map mechanics for this aggregate type just in case + // you're using a lightweight session + session.UseIdentityMapFor(); + } + else + { + storage = session.StorageFor(); + } + await _identityStrategy.EnsureEventStorageExists(session, cancellation).ConfigureAwait(false); await session.Database.EnsureStorageExistsAsync(typeof(TDoc), cancellation).ConfigureAwait(false); @@ -95,7 +111,7 @@ public async Task> FetchForWriting(DocumentSessionBase sessio builder.StartNewCommand(); - var handler = new LoadByIdHandler(_storage, id); + var handler = new LoadByIdHandler(storage, id); handler.ConfigureCommand(builder, session); long version = 0; diff --git a/src/Marten/Events/Fetching/InlineFetchPlanner.cs b/src/Marten/Events/Fetching/InlineFetchPlanner.cs index 201af3645d..c54f026c4b 100644 --- a/src/Marten/Events/Fetching/InlineFetchPlanner.cs +++ b/src/Marten/Events/Fetching/InlineFetchPlanner.cs @@ -12,7 +12,7 @@ public bool TryMatch(IDocumentStorage storage, IEventIdent { if (projection.Lifecycle == ProjectionLifecycle.Inline) { - plan = new FetchInlinedPlan(options.EventGraph, identity, storage); + plan = new FetchInlinedPlan(options.EventGraph, identity); return true; } }