Reference

IRoute

Package Trax.CoreNamespace Trax.Core.RouteSource IRoute.cs

The contract every train implements: take an input, produce an output or fail. Train<TInput, TReturn> implements it and IServiceTrain<TIn, TOut> extends it, so code that only runs something can depend on IRoute without knowing it is a train.

Signature

namespace Trax.Core.Route;
 
public interface IRoute<in TIn, TOut>
{
    Task<TOut> Run(TIn input, CancellationToken cancellationToken = default);
}

Parameters

ParameterTypeDescription
inputTInThe input to run with
cancellationTokenCancellationTokenCancels the run. Defaults to CancellationToken.None.

Returns

Task<TOut>, the output. A failure is thrown, as from Run.

Behavior

TypeRelationship to IRoute
Train<TInput, TReturn>Implements IRoute<TInput, TReturn>
IServiceTrain<TIn, TOut>Extends IRoute<TIn, TOut> and IDisposable, and adds Metadata
A service train's own interfaceExtends IServiceTrain<TIn, TOut>, and so IRoute

The registration helpers (AddScopedTraxRoute and its siblings) are named after it because they register any route, train or junction. None of them registers IRoute<,> itself, so the container cannot resolve IRoute<TIn, TOut> unless you forward it.

Example

using Trax.Core.Route;
 
public class OrderEndpoint(IRoute<CreateOrderInput, OrderResult> createOrder)
{
    public Task<OrderResult> Handle(CreateOrderInput input, CancellationToken ct) =>
        createOrder.Run(input, ct);
}

Forwarding the train's interface so IRoute resolves:

using Trax.Core.Route;
using Trax.Effect.Extensions;
 
services.AddTransientTraxRoute<ICreateOrderTrain, CreateOrderTrain>();
services.AddTransient<IRoute<CreateOrderInput, OrderResult>>(sp =>
    sp.GetRequiredService<ICreateOrderTrain>()
);

See also