Please take a look any previous posts as they may give more context to the code samples below.
Idempotence is the property of certain operations in mathematics and computer science, that can be applied multiple times without changing the result beyond the initial application.
In my previous post Idempotent Commands, I looked at creating idempotency at the persistence level if you are using an ACID compliance database.
But what happens if you aren’t? What other options do you have?
Well if you are building a domain model and are creating Aggregates, you could create idempotent aggregates.
If there is enough value and complexity, I will create a domain model with Aggregates that will contain domain behavior and business logic.
This is example is very trivial and I wouldn’t be creating an aggregate for basic CRUD.
However, if we did have a complex domain and aggregate, we can pass in an OperationId to our Aggregate, prior to calling any behavioral methods in order to check if we have already executed that operation.
Now in our Command Handler, we can call SetOperation and pass our MessageId from our Envelope<T>.
And as our previous example, here is our NancyFX Module (similar to ASP.NET Controller) which is sending our Command via MediatR.
I have not brought up concurrency as this is something that will be determined based on your data storage solution.
How do you?
How do you handle idempotent commands? I’d love to hear about other implementations or uses. Comment below or on twitter.
- Idempotent Commands
- Identify Commands & Events
- Thin Controllers with CQRS and MediatR
- Query Objects instead of Repositories
- Mediator Pattern using MediatR and Unity
- Query Objects with a Mediator