Class TransactionalCommandBehavior<TMessage, TResponse>
- Namespace
- Trellis.EntityFrameworkCore
- Assembly
- Trellis.EntityFrameworkCore.dll
Pipeline behavior that automatically commits staged changes after a successful command handler.
The behavior calls next to run the handler (which stages changes via repositories),
then calls CommitAsync(CancellationToken) if the handler returned success.
If the handler returned a failure Result<TValue>, no commit occurs and the
staged changes are discarded when the DbContext is disposed.
This behavior is constrained to Mediator.ICommand<TResponse> messages — queries are not wrapped and incur no overhead.
Atomicity: EF Core wraps each SaveChanges call in an implicit database
transaction, so all staged changes within a single handler are committed atomically.
Cross-aggregate operations that share the same DbContext
are automatically transactional.
public sealed class TransactionalCommandBehavior<TMessage, TResponse> : IPipelineBehavior<TMessage, TResponse> where TMessage : ICommand<TResponse> where TResponse : IResult, IFailureFactory<TResponse>
Type Parameters
TMessageThe command type.
TResponseThe result type, constrained to IResult and IFailureFactory<TSelf>.
- Inheritance
-
TransactionalCommandBehavior<TMessage, TResponse>
- Implements
-
IPipelineBehavior<TMessage, TResponse>
- Inherited Members
- Extension Methods
Constructors
TransactionalCommandBehavior(IUnitOfWork)
Pipeline behavior that automatically commits staged changes after a successful command handler.
The behavior calls next to run the handler (which stages changes via repositories),
then calls CommitAsync(CancellationToken) if the handler returned success.
If the handler returned a failure Result<TValue>, no commit occurs and the
staged changes are discarded when the DbContext is disposed.
This behavior is constrained to Mediator.ICommand<TResponse> messages — queries are not wrapped and incur no overhead.
Atomicity: EF Core wraps each SaveChanges call in an implicit database
transaction, so all staged changes within a single handler are committed atomically.
Cross-aggregate operations that share the same DbContext
are automatically transactional.
public TransactionalCommandBehavior(IUnitOfWork unitOfWork)
Parameters
unitOfWorkIUnitOfWork
Methods
Handle(TMessage, MessageHandlerDelegate<TMessage, TResponse>, CancellationToken)
public ValueTask<TResponse> Handle(TMessage message, MessageHandlerDelegate<TMessage, TResponse> next, CancellationToken cancellationToken)
Parameters
messageTMessagenextMessageHandlerDelegate<TMessage, TResponse>cancellationTokenCancellationToken
Returns
- ValueTask<TResponse>