Table of Contents

Class Command

Namespace
Dynamicweb.CoreUI.Data
Assembly
Dynamicweb.CoreUI.dll

A command whose behaviour lives in an ICommandHandler<TCommand>.

public abstract class Command : CommandBase
Inheritance
Command
Inherited Members

Remarks

Implementations hold data only. Nothing is resolved from the dependency container until Handle() is called, so a command can be instantiated freely to describe an action to the client.

using Dynamicweb.CoreUI.Data;
using Dynamicweb.CoreUI.Data.Validation;

namespace Dynamicweb.Examples.UI.Commands;

/// <summary>
/// Example of a <see cref="Command"/> - the shape to use when the command's own properties are the payload.
/// The work it describes lives in <see cref="OrderChangeCommentExampleCommandHandler"/>; this class holds data only.
/// </summary>
/// <remarks>
/// Validation attributes such as <see cref="RequiredAttribute"/> are checked by the framework before the handler runs.
/// </remarks>
public sealed class OrderChangeCommentExampleCommand : Command
{
    /// <summary>
    /// Gets or sets the id of the order to update. Required - the command is rejected without it.
    /// </summary>
    [Required]
    public required string OrderId { get; set; }

    /// <summary>
    /// Gets or sets the comment to store on the order. Optional.
    /// </summary>
    public string? Comment { get; set; }
}
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,
        };
    }
}

Methods

Handle()

Dispatches this command to the registered handler.

public override sealed CommandResult Handle()

Returns

CommandResult
To top