Browse pages
LS2002: Command Handler Dispatches Another Command
LS2002 is a compiler check that reports a command handler, the type that runs the state change, dispatching a nested command, which splits one change across 2 separate runs so the commit, the authorization, and the failure handling no longer cover the whole operation. This page states what the rule reports, where the warning sits, and how to keep the change in the single handler.
Reference#
What the rule enforces#
LS2002 enforces Keep one state-changing Use case in one Command pipeline (litespec/backend/dotnet/application#keep-one-state-changing-use-case-in-one-command-pipeline). It runs in Application and Infrastructure. Its category is LiteSpec.Layers. It reports a warning and is enabled by default, as the Analyzers overview describes.
What the rule reports#
The rule reports a call through ICommandMediator inside a command handler, which is a class whose name ends in CommandHandler or that implements ICommandHandler. The diagnostic sits on the invoked method name.
Failing example#
internal sealed class PlaceOrderCommandHandler(ICommandMediator mediator)
{
public Task HandleAsync(PlaceOrderCommand command, CancellationToken cancellationToken) =>
mediator.SendAsync(command, cancellationToken);
}Passing example#
internal sealed class PlaceOrderCommandHandler(IOrderRepository orders)
{
public void Handle(PlaceOrderCommand command) => orders.Store(command);
}How to fix#
Keep the whole state change in the one handler: call the repository and domain behavior directly instead of dispatching a nested command.
Notes#
What the rule ignores#
The rule does not report calls to repositories or other collaborators, or handlers that never touch the mediator. It does not run in Domain, Application.Abstractions, or WebApi.