Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move BeforeSaveChanges before ProcessEvents #3008

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Marten;
using Marten.Events.Projections;
using Marten.Testing.Harness;
using Shouldly;
using Xunit;

namespace EventSourcingTests.Aggregation;

public class when_inline_event_enriched: OneOffConfigurationsContext
{
[Fact]
public async Task async_apply_with_enrich_event()
{
StoreOptions(o =>
{
o.Listeners.Add(new DocumentEnricher());
o.Projections.Snapshot<User>(SnapshotLifecycle.Inline);
});

var streamId = Guid.NewGuid();
theSession.Events.Append(streamId, new EnrichedUser());
await theSession.SaveChangesAsync();

var aggregate = await theSession.Query<User>().FirstAsync();
aggregate.Actor.ShouldBe("some user from identity");
}

public class DocumentEnricher: DocumentSessionListenerBase
{
public override void BeforeProcessChanges(IDocumentSession session)
{
var streams = session.PendingChanges.Streams();
foreach (var stream in streams)
{
foreach (var e in stream.Events)
{
if (e.Data is EnrichedUserEvent eueb)
{
eueb.Actor = "some user from identity";
}
}
}
}

public override Task BeforeProcessChangesAsync(IDocumentSession session, CancellationToken token)
{
return Task.Factory.StartNew(() => BeforeProcessChanges(session));
}
}

public record User
{
public Guid Id { get; set; }
public string Actor { get; set; }
private void Apply(EnrichedUser e) => Actor = e.Actor;
}

public record EnrichedUser: EnrichedUserEvent;

public abstract record EnrichedUserEvent
{
public string Actor { get; set; }
}
}
27 changes: 27 additions & 0 deletions src/Marten/IDocumentSessionListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public interface IDocumentSessionListener
/// <returns></returns>
Task AfterCommitAsync(IDocumentSession session, IChangeSet commit, CancellationToken token);

/// <summary>
/// Called just after IDocumentSession.SaveChanges() is called, but before
/// any event apply are made
/// </summary>
/// <param name="session"></param>
void BeforeProcessChanges(IDocumentSession session);

/// <summary>
/// Called just after IDocumentSession.SaveChanges() is called, but before
/// any event apply are made
/// </summary>
/// <param name="session"></param>
/// <param name="token"></param>
/// <returns></returns>
Task BeforeProcessChangesAsync(IDocumentSession session, CancellationToken token);

/// <summary>
/// Called just after IDocumentSession.SaveChanges() is called, but before
/// any database calls are made
Expand Down Expand Up @@ -107,6 +123,17 @@ public interface IDocumentSessionListener
/// </summary>
public abstract class DocumentSessionListenerBase: IDocumentSessionListener
{
public virtual void BeforeProcessChanges(IDocumentSession session)
{
// Nothing
}

public virtual Task BeforeProcessChangesAsync(IDocumentSession session, CancellationToken token)
{
// Nothing
return Task.CompletedTask;
}

public virtual void BeforeSaveChanges(IDocumentSession session)
{
// Nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void SaveChanges()
return;
}

foreach (var listener in Listeners) listener.BeforeProcessChanges(this);

try
{
Options.EventGraph.ProcessEvents(this);
Expand Down Expand Up @@ -68,6 +70,11 @@ public async Task SaveChangesAsync(CancellationToken token = default)
return;
}

foreach (var listener in Listeners)
{
await listener.BeforeProcessChangesAsync(this, token).ConfigureAwait(false);
}

try
{
await Options.EventGraph.ProcessEventsAsync(this, token).ConfigureAwait(false);
Expand Down Expand Up @@ -186,7 +193,6 @@ internal async Task ExecuteBatchAsync(IUpdateBatch batch, CancellationToken toke
{
try
{

await executeBeforeCommitListeners(batch).ConfigureAwait(false);

await Options.ResiliencePipeline.ExecuteAsync(
Expand Down