Table of Contents

Class ResourceLoaderById<TMessage, TResource, TId>

Namespace
Trellis.Authorization
Assembly
Trellis.Authorization.dll

Convenience base class for resource loaders that extract a typed ID from the message and load via a repository's GetByIdAsync method.

public abstract class ResourceLoaderById<TMessage, TResource, TId> : IResourceLoader<TMessage, TResource>

Type Parameters

TMessage

The command or query type.

TResource

The aggregate or entity type to load.

TId

The identifier type (e.g., OrderId, Guid).

Inheritance
ResourceLoaderById<TMessage, TResource, TId>
Implements
IResourceLoader<TMessage, TResource>
Inherited Members
Extension Methods

Examples

public sealed class CancelOrderResourceLoader
    : ResourceLoaderById<CancelOrderCommand, Order, OrderId>
{
    private readonly IOrderRepository _repo;
    public CancelOrderResourceLoader(IOrderRepository repo) => _repo = repo;
    protected override OrderId GetId(CancelOrderCommand message) => message.OrderId;
    protected override Task<Result<Order>> GetByIdAsync(OrderId id, CancellationToken ct)
        => _repo.GetByIdAsync(id, ct);
}

Remarks

Covers the most common resource-loading pattern: extract an ID from the message, call a repository method. Implement IResourceLoader<TMessage, TResource> directly for complex cases (composite keys, projections, etc.).

Methods

GetByIdAsync(TId, CancellationToken)

Loads the resource by ID. Return Result.Failure with a NotFoundError if the resource does not exist.

protected abstract Task<Result<TResource>> GetByIdAsync(TId id, CancellationToken cancellationToken)

Parameters

id TId

The resource identifier extracted by GetId(TMessage).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Result<TResource>>

A success result containing the loaded resource, or a failure result if not found.

GetId(TMessage)

Extracts the resource ID from the message.

protected abstract TId GetId(TMessage message)

Parameters

message TMessage

The command or query containing the resource identifier.

Returns

TId

The typed resource identifier.

LoadAsync(TMessage, CancellationToken)

Loads the resource identified by the message.

public Task<Result<TResource>> LoadAsync(TMessage message, CancellationToken cancellationToken)

Parameters

message TMessage

The command or query containing the resource identifier.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Result<TResource>>

A success result containing the loaded resource, or a failure result (typically NotFound(string, string?)) if the resource does not exist.