-
-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathMartenAsyncCommandBus.cs
More file actions
30 lines (25 loc) · 948 Bytes
/
MartenAsyncCommandBus.cs
File metadata and controls
30 lines (25 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Core.Commands;
using Marten;
using Microsoft.Extensions.DependencyInjection;
namespace Core.Marten.Commands;
/// <summary>
/// Note: This is an example of the outbox pattern for Command Bus using Marten
/// For production use mature tooling like Wolverine, MassTransit or NServiceBus
/// </summary>
public class MartenAsyncCommandBus(IDocumentSession documentSession): IAsyncCommandBus
{
public const string CommandsStreamId = "__commands";
public Task Schedule<TCommand>(TCommand command, CancellationToken ct = default) where TCommand: notnull
{
documentSession.Events.Append(CommandsStreamId, command);
return documentSession.SaveChangesAsync(ct);
}
}
public static class Config
{
public static IServiceCollection AddMartenAsyncCommandBus(
this IServiceCollection services
) =>
services.AddScoped<IAsyncCommandBus, MartenAsyncCommandBus>()
.AddCommandForwarder();
}