Interface ICommandHandler<TCommand>
- Namespace
- Dynamicweb.CoreUI.Data
- Assembly
- Dynamicweb.CoreUI.dll
Defines the dedicated handler for TCommand.
public interface ICommandHandler<in TCommand> where TCommand : CommandBase
Type Parameters
TCommandThe command type this handler processes.
Remarks
No dependency registration is required; discovery is handled internally. Implementations are expected to be internal and sealed.
using Dynamicweb.CoreUI.Data;
using Dynamicweb.Ecommerce.Orders;
namespace Dynamicweb.Examples.UI.Commands;
/// <summary>
/// Example of an <see cref="ICommandHandler{TCommand}"/> - where the behaviour of a
/// <see cref="OrderChangeCommentExampleCommand"/> lives.
/// </summary>
/// <remarks>
/// <see langword="internal"/> and <see langword="sealed"/>, with no dependency registration: the handler is
/// discovered from the loaded add-in types and constructed through the container, so services arrive as constructor
/// parameters instead of being resolved from a static locator. That is what makes the handler unit testable.
/// </remarks>
/// <param name="orderService">Loads and saves the order being changed.</param>
internal sealed class OrderChangeCommentExampleCommandHandler(OrderService orderService) : ICommandHandler<OrderChangeCommentExampleCommand>
{
/// <inheritdoc />
public CommandResult Handle(OrderChangeCommentExampleCommand command)
{
var order = orderService.GetById(command.OrderId);
if (order is null)
{
return new()
{
Status = CommandResult.ResultType.NotFound,
Message = "The selected Order could not be found",
};
}
order.Comment = command.Comment;
orderService.Save(order);
return new()
{
Status = CommandResult.ResultType.Ok,
};
}
}
using System.Collections.Generic;
using Dynamicweb.CoreUI.Data;
using Dynamicweb.Ecommerce.Orders;
namespace Dynamicweb.Examples.UI.Commands;
/// <summary>
/// Example of the handler for a list command. A list command uses the same one-argument
/// <see cref="ICommandHandler{TCommand}"/> contract as a plain command - the selected ids are simply part of the
/// command.
/// </summary>
/// <param name="orderService">Loads and saves the selected orders.</param>
internal sealed class OrderChangeCommentBulkExampleCommandHandler(OrderService orderService) : ICommandHandler<OrderChangeCommentBulkExampleCommand>
{
/// <inheritdoc />
public CommandResult Handle(OrderChangeCommentBulkExampleCommand command)
{
List<Order> orders = [];
foreach (var orderId in command.Ids)
{
if (orderService.GetById(orderId) is { } order)
orders.Add(order);
}
if (orders.Count == 0)
{
return new()
{
Status = CommandResult.ResultType.NotFound,
Message = "The selected Orders could not be found",
};
}
foreach (var order in orders)
{
order.Comment = command.Comment;
orderService.Save(order);
}
return new()
{
Status = CommandResult.ResultType.Ok,
Message = $"Updated {orders.Count} order(s)",
};
}
}
Methods
Handle(TCommand)
Handles command and returns the result.
CommandResult Handle(TCommand command)
Parameters
commandTCommandThe command to handle.