Dapper

alt NuGet

Dapper is a super lightweight Micro-ORM usable for saga persistence with Microsoft SQL Server. Dapper.Contrib is used for inserts and updates. The methods are virtual, so if you'd rather write the SQL yourself it is supported.

If you do not write your own sql, the model requires you use the ExplicitKey attribute for the CorrelationId. And if you have properties that are not available as columns, you can use the Computed attribute to not include them in the generated SQL. If you are using event correlation using other properties, it's highly recommended that you create indices for performance.

public class OrderState :
    SagaStateMachineInstance
{
    [ExplicitKey]
    public Guid CorrelationId { get; set; }
    public string CurrentState { get; set; }

    public DateTime? OrderDate { get; set; }
}

Configuration

To configure Dapper as the saga repository for a saga, use the code shown below using the AddMassTransit container extension.

container.AddMassTransit(cfg =>
{
    cfg.AddSagaStateMachine<OrderStateMachine, OrderState>()
        .DapperRepository(connectionString);
});

Limitations

Table Names

The table name can only be the pluralized form of the class name. So OrderState would translate to table OrderStates. This applies even if you write your own SQL for updates and inserts.

Correlation Expressions

The expressions you can use for correlation is somewhat limited. These types of expressions are handled:

x => x.CorrelationId == someGuid;
x => x.IsDone;
x => x.CorrelationId == someGuid && x.IsDone;

You can use multiple && in the expression. What you can not use is || and negations. So a bool used like this x.IsDone can only be handled as true and nothing else.