Skip to content

Commit

Permalink
Persist saga metadata
Browse files Browse the repository at this point in the history
- Add IPersistableSagaPool, IPersistableSagaPool<TSaga, TMetaData>, IPersistableSagaRepository<TSaga, TMetaData>
- Add mappings for PersistableSaga<>, PersistableSagaMetadataDto to SagaPersistAutoMapperProfile
- Add tests for mapping PersistableSaga<TMetadata> and PersistableSagaMetadataDto
- Create AddSaga overloads with TMetadata in ServiceCollectionExtensions
- Update reference architecture projects to persist saga metadata
  • Loading branch information
Jeff Martin authored and tonysneed committed Jun 17, 2023
1 parent c587ace commit 736fdeb
Show file tree
Hide file tree
Showing 30 changed files with 725 additions and 43 deletions.
7 changes: 7 additions & 0 deletions EventDriven.Sagas.sln
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "reference-archite
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventDriven.Sagas.DependencyInjection.Tests", "test\EventDriven.Sagas.DependencyInjection.Tests\EventDriven.Sagas.DependencyInjection.Tests.csproj", "{75635BC4-39E9-45D6-AB55-DE9AEE0670F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventDriven.Sagas.Persistence.Abstractions.Tests", "test\EventDriven.Sagas.Persistence.Abstractions.Tests\EventDriven.Sagas.Persistence.Abstractions.Tests.csproj", "{F61E2F95-2A59-4E0B-98E4-78204D78590D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -148,6 +150,10 @@ Global
{75635BC4-39E9-45D6-AB55-DE9AEE0670F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75635BC4-39E9-45D6-AB55-DE9AEE0670F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75635BC4-39E9-45D6-AB55-DE9AEE0670F1}.Release|Any CPU.Build.0 = Release|Any CPU
{F61E2F95-2A59-4E0B-98E4-78204D78590D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F61E2F95-2A59-4E0B-98E4-78204D78590D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F61E2F95-2A59-4E0B-98E4-78204D78590D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F61E2F95-2A59-4E0B-98E4-78204D78590D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -174,6 +180,7 @@ Global
{E64FBA81-81E0-429E-B83D-213BDD880284} = {2F6C2678-B38D-4784-AEDA-1DF293E1296A}
{C67BF2F3-1B5E-44EB-93A5-634FEC0E073F} = {2F6C2678-B38D-4784-AEDA-1DF293E1296A}
{75635BC4-39E9-45D6-AB55-DE9AEE0670F1} = {7340565F-A10B-4B7D-B75E-9320CAE5BD05}
{F61E2F95-2A59-4E0B-98E4-78204D78590D} = {7340565F-A10B-4B7D-B75E-9320CAE5BD05}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3BFC4ACA-7DDF-4583-A578-5CAB3893AE40}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EventDriven.CQRS.Abstractions.Commands;
using EventDriven.Sagas.Abstractions;
using EventDriven.Sagas.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using OrderService.Domain.OrderAggregate.Commands;
using OrderService.Helpers;
using OrderService.Repositories;
Expand All @@ -11,12 +11,12 @@ namespace OrderService.Domain.OrderAggregate.CommandHandlers;
public class StartCreateOrderSagaHandler : ICommandHandler<Order, StartCreateOrderSaga>
{
private readonly IOrderRepository _repository;
private readonly ISagaPool<CreateOrderSaga> _sagaPool;
private readonly IPersistableSagaPool<CreateOrderSaga, OrderMetadata> _sagaPool;
private readonly ILogger<StartCreateOrderSagaHandler> _logger;

public StartCreateOrderSagaHandler(
IOrderRepository repository,
ISagaPool<CreateOrderSaga> sagaPool,
IPersistableSagaPool<CreateOrderSaga, OrderMetadata> sagaPool,
ILogger<StartCreateOrderSagaHandler> logger)
{
_repository = repository;
Expand All @@ -32,11 +32,11 @@ public async Task<CommandResult<Order>> Handle(StartCreateOrderSaga command, Can

try
{
// Create saga
var saga = await _sagaPool.CreateSagaAsync();
// When you emerge from here its the persistable saga has been saved
var saga = await _sagaPool.CreateSagaAsync(command.OrderMetadata);

// Start create order saga
await saga.StartSagaAsync(command.Entity, command.OrderMetadata, cancellationToken);
await saga.StartSagaAsync(command.Entity, cancellationToken);

// Return created order
var order = await _repository.GetAsync(command.Entity.Id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;

Expand All @@ -10,12 +11,12 @@ namespace OrderService.Integration.Handlers;
public class CustomerCreditReleaseFulfilledEventHandler :
IntegrationEventHandler<CustomerCreditReleaseFulfilled>
{
private readonly ISagaPool<CreateOrderSaga> _sagaPool;
private readonly IPersistableSagaPool<CreateOrderSaga,OrderMetadata> _sagaPool;
private readonly IOrderRepository _orderRepository;
private readonly ILogger<CustomerCreditReleaseFulfilledEventHandler> _logger;

public CustomerCreditReleaseFulfilledEventHandler(
ISagaPool<CreateOrderSaga> sagaPool,
IPersistableSagaPool<CreateOrderSaga, OrderMetadata> sagaPool,
IOrderRepository orderRepository,
ILogger<CustomerCreditReleaseFulfilledEventHandler> logger)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;

Expand All @@ -10,12 +11,12 @@ namespace OrderService.Integration.Handlers;
public class CustomerCreditReserveFulfilledEventHandler :
IntegrationEventHandler<CustomerCreditReserveFulfilled>
{
private readonly ISagaPool<CreateOrderSaga> _sagaPool;
private readonly IPersistableSagaPool<CreateOrderSaga,OrderMetadata> _sagaPool;
private readonly IOrderRepository _orderRepository;
private readonly ILogger<CustomerCreditReserveFulfilledEventHandler> _logger;

public CustomerCreditReserveFulfilledEventHandler(
ISagaPool<CreateOrderSaga> sagaPool,
IPersistableSagaPool<CreateOrderSaga, OrderMetadata> sagaPool,
IOrderRepository orderRepository,
ILogger<CustomerCreditReserveFulfilledEventHandler> logger)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;

Expand All @@ -10,12 +11,12 @@ namespace OrderService.Integration.Handlers;
public class ProductInventoryReleaseFulfilledEventHandler :
IntegrationEventHandler<ProductInventoryReleaseFulfilled>
{
private readonly ISagaPool<CreateOrderSaga> _sagaPool;
private readonly IPersistableSagaPool<CreateOrderSaga, OrderMetadata> _sagaPool;
private readonly IOrderRepository _orderRepository;
private readonly ILogger<ProductInventoryReleaseFulfilledEventHandler> _logger;

public ProductInventoryReleaseFulfilledEventHandler(
ISagaPool<CreateOrderSaga> sagaPool,
IPersistableSagaPool<CreateOrderSaga,OrderMetadata> sagaPool,
IOrderRepository orderRepository,
ILogger<ProductInventoryReleaseFulfilledEventHandler> logger)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;

Expand All @@ -10,13 +11,13 @@ namespace OrderService.Integration.Handlers;
public class ProductInventoryReserveFulfilledEventHandler :
IntegrationEventHandler<ProductInventoryReserveFulfilled>
{
private readonly ISagaPool<CreateOrderSaga> _sagaPool;
private readonly IPersistableSagaPool<CreateOrderSaga, OrderMetadata> _sagaPool;
private readonly IOrderRepository _orderRepository;
private readonly ILogger<ProductInventoryReserveFulfilledEventHandler> _logger;
public Type? SagaType { get; set; } = typeof(CreateOrderSaga);

public ProductInventoryReserveFulfilledEventHandler(
ISagaPool<CreateOrderSaga> sagaPool,
IPersistableSagaPool<CreateOrderSaga,OrderMetadata> sagaPool,
IOrderRepository orderRepository,
ILogger<ProductInventoryReserveFulfilledEventHandler> logger)
{
Expand Down
6 changes: 3 additions & 3 deletions reference-architecture/OrderService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
builder.Services.AddMongoDbSettings<OrderDatabaseSettings, Order>(builder.Configuration);
builder.Services.AddMongoDbSettings<SagaConfigDatabaseSettings, SagaConfigurationDto>(builder.Configuration);
builder.Services.AddMongoDbSettings<SagaSnapshotDatabaseSettings, SagaSnapshotDto>(builder.Configuration);
builder.Services.AddMongoDbSettings<PersistableSagaDatabaseSettings, PersistableSagaDto>(builder.Configuration);
builder.Services.AddMongoDbSettings<PersistableSagaDatabaseSettings, PersistableSagaMetadataDto>(builder.Configuration);

// Add command and query handlers
builder.Services.AddHandlers(typeof(Program));
Expand All @@ -44,8 +44,8 @@
builder.Services.AddAppSettings<CreateOrderSagaConfigSettings>(builder.Configuration);

// Sagas
builder.Services.AddSaga<CreateOrderSaga, CreateOrderSagaConfigSettings, CreateOrderSagaCommandDispatcher,
SagaConfigRepository, SagaSnapshotRepository, PersistableSagaRepository<CreateOrderSaga>>(builder.Configuration);
builder.Services.AddSaga<CreateOrderSaga, OrderMetadata, CreateOrderSagaConfigSettings, CreateOrderSagaCommandDispatcher,
SagaConfigRepository, SagaSnapshotRepository, PersistableSagaRepository<CreateOrderSaga,OrderMetadata>>(builder.Configuration);

// Event Bus and event handlers
builder.Services.AddDaprEventBus(builder.Configuration);
Expand Down
Loading

0 comments on commit 736fdeb

Please sign in to comment.