Table of Contents

Class Command<T>

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

A command carrying T, whose behaviour lives in an ICommandHandler<TCommand, TModel>.

public abstract class Command<T> : CommandBase<T> where T : new()

Type Parameters

T

The model carried by this command.

Inheritance
Command<T>
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.Examples.UI.Models;

namespace Dynamicweb.Examples.UI.Commands;

/// <summary>
/// Example of a <see cref="Command{T}"/> - the shape to use when an edit screen posts a model. The model is the
/// payload, so the command declares no properties of its own.
/// </summary>
/// <remarks>
/// Behaviour lives in <see cref="OrderChangeExampleCommandHandler"/> and validation in
/// <see cref="OrderChangeExampleCommandValidator"/>. Note that <see cref="Command{T}.ValidateModel"/> is sealed:
/// validation cannot be overridden here, it has to be a validator.
/// </remarks>
public sealed class OrderChangeExampleCommand : Command<OrderChangeExampleDataModel>;
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,
        };
    }
}
using System.Collections.Generic;
using Dynamicweb.CoreUI.Data;
using Dynamicweb.CoreUI.Data.Validation;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Examples.UI.Models;

namespace Dynamicweb.Examples.UI.Commands;

/// <summary>
/// Example of an <see cref="ICommandValidator{TCommand, TModel}"/> - where validation lives now that
/// <see cref="Command{T}.ValidateModel"/> is sealed.
/// </summary>
/// <remarks>
/// Shows both kinds of rule: one that only needs the model, and one that needs a service. The service arrives through
/// the constructor, which an override on the command could never have done. A command may have any number of
/// validators; every one of them runs and their results are combined.
/// </remarks>
/// <param name="orderService">Used to check whether the order may still be changed.</param>
internal sealed class OrderChangeExampleCommandValidator(OrderService orderService) : ICommandValidator<OrderChangeExampleCommand, OrderChangeExampleDataModel>
{
    /// <inheritdoc />
    public IEnumerable<ValidationResult> Validate(OrderChangeExampleCommand command, OrderChangeExampleDataModel model)
    {
        if (string.IsNullOrWhiteSpace(model.CustomerName))
            yield return new(nameof(model.CustomerName), "A customer name is required");

        if (orderService.IsComplete(model.OrderId))
            yield return new(nameof(model.Comment), "A completed order cannot be changed");
    }
}

Methods

Handle()

Dispatches this command and its model to the registered handler.

public override sealed CommandResult Handle()

Returns

CommandResult

ValidateModel(T)

Runs the ICommandValidator<TCommand, TModel> implementations registered for this command.

public override sealed IEnumerable<ValidationResult> ValidateModel(T model)

Parameters

model T

Returns

IEnumerable<ValidationResult>

Remarks

Sealed for the same reason as Handle(): the command holds data only. Validation belongs in an ICommandValidator<TCommand, TModel>, which can take its dependencies through the constructor - and an override here would silently stop every registered validator from running.

To top