Table of Contents

Class ListItemsCommand

Namespace
Dynamicweb.CoreUI.Lists
Assembly
Dynamicweb.CoreUI.dll

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

public abstract class ListItemsCommand : ListItemsCommandBase, IProvidesCommandLogContext
Inheritance
ListItemsCommand
Implements
Inherited Members

Remarks

Derives from ListItemsCommandBase rather than Command so that the framework keeps recognising it as a list command - hydrating Ids from transient storage and rejecting an empty selection. Implementations hold data only; nothing is resolved from the dependency container until Handle() is called.

using Dynamicweb.CoreUI.Lists;

namespace Dynamicweb.Examples.UI.Commands;

/// <summary>
/// Example of a <see cref="ListItemsCommand"/> - the shape to use for an action raised from a list, acting on the
/// rows the user selected. The selection arrives in <see cref="ListItemsCommandBase.Ids"/>; any extra input the
/// action needs is declared here.
/// </summary>
/// <remarks>
/// Behaviour lives in <see cref="OrderChangeCommentBulkExampleCommandHandler"/>. The framework fills
/// <see cref="ListItemsCommandBase.Ids"/> from transient storage and rejects an empty selection before dispatch, so
/// the handler can assume there is at least one id.
/// </remarks>
public sealed class OrderChangeCommentBulkExampleCommand : ListItemsCommand
{
    /// <summary>
    /// Gets or sets the comment to store on every selected order.
    /// </summary>
    public string? Comment { get; set; }
}
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()

Dispatches this command to the registered handler.

public override sealed CommandResult Handle()

Returns

CommandResult
To top