Interface ICommandHandler<TCommand, TModel>
- Namespace
- Dynamicweb.CoreUI.Data
- Assembly
- Dynamicweb.CoreUI.dll
Defines the dedicated handler for TCommand and the model it carries.
public interface ICommandHandler<in TCommand, in TModel> where TCommand : CommandBase<in TModel> where TModel : new()
Type Parameters
TCommandThe command type this handler processes.
TModelThe model carried by the command.
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;
using Dynamicweb.Examples.UI.Models;
namespace Dynamicweb.Examples.UI.Commands;
/// <summary>
/// Example of an <see cref="ICommandHandler{TCommand, TModel}"/> - the two-argument handler used when the command
/// carries a model.
/// </summary>
/// <remarks>
/// The model arrives as a parameter rather than being read off the command, and it is never null: the framework has
/// already loaded the current model and merged the posted changes onto it before the handler is reached.
/// </remarks>
/// <param name="orderService">Loads and saves the order being changed.</param>
internal sealed class OrderChangeExampleCommandHandler(OrderService orderService)
: ICommandHandler<OrderChangeExampleCommand, OrderChangeExampleDataModel>
{
/// <inheritdoc />
public CommandResult Handle(OrderChangeExampleCommand command, OrderChangeExampleDataModel model)
{
var order = orderService.GetById(model.OrderId);
if (order is null)
{
return new()
{
Status = CommandResult.ResultType.NotFound,
Message = "The selected Order could not be found",
};
}
order.CustomerName = model.CustomerName;
order.Comment = model.Comment;
orderService.Save(order);
return new()
{
Status = CommandResult.ResultType.Ok,
Model = model,
};
}
}
Methods
Handle(TCommand, TModel)
Handles command with model and returns the result.
CommandResult Handle(TCommand command, TModel model)
Parameters
commandTCommandThe command to handle.
modelTModelThe model carried by
command.