Junctions
Package Trax.CoreNamespace Trax.Core.TrainSource Train.cs
The override that declares a train's chain: the junctions it passes through, in order, and the steps between them. Every train overrides it, and it is the only way to declare a chain.
Signature
// Train<TInput, TReturn>, inherited unchanged by ServiceTrain<TIn, TOut>
protected virtual Task<Either<Exception, TReturn>> Junctions();Returns
Task<Either<Exception, TReturn>>, the train's railway result. The chain ends in Resolve(), which takes TReturn out of Memory, or carries the exception that stopped the chain as Left.
Behavior
- The train seeds Memory with its input (under its declared type, its runtime type and that type's interfaces) and with
Unit, then callsJunctions(). - The chain methods are protected methods on the train.
Chain,IChain,ShortCircuitand the routing steps return a MonadTask;ExtractandAddServicescalled on the train return aMonad. Resolve()awaits the chain and returnsEither<Exception, TReturn>.- A junction that throws skips the rest of the chain and its exception comes back as
Left. An exceptionJunctions()itself throws is returned asLefttoo. - Run unwraps the result and rethrows a
Left; RunEither returns it as is. - The base implementation throws
NotImplementedException.
The same Junctions() is read without running anything by DeclaredChain, which the host's startup chain check calls for every registered train. The host refuses to start when Junctions():
- reads
TrainInputorTrainOutput, which throwChainDeclarationExceptionwhile the chain is read - awaits something before returning
- returns a result directly (
Task.FromResult(value)) instead of ending inResolve() - ends in
Resolve(value) - uses
IChain<T>orAddServices<T>with a class instead of an interface - passes
AddServicesa null service, such as a field assigned later inOnStarted - short-circuits with a junction whose output cannot be the train's return type
A train with no junctions ends in Task.FromResult(Resolve()). The full list of faults, and the Memory rules the check replays, are in The host checks every chain before it serves traffic. A train that used to override RunInternal or call Activate is upgraded with Removal of RunInternal and Activate.
Examples
using Trax.Core.Functional;
using Trax.Effect.Services.ServiceTrain;
public class PlaceOrderTrain : ServiceTrain<OrderInput, OrderResult>
{
protected override Task<Either<Exception, OrderResult>> Junctions() =>
Chain<ValidateOrder>().Chain<ProcessPayment>().Resolve();
}With ShortCircuit, Extract and AddServices:
using Trax.Core.Functional;
using Trax.Effect.Services.ServiceTrain;
public class FulfilOrderTrain(INotificationService notifications)
: ServiceTrain<OrderInput, OrderResult>
{
protected override Task<Either<Exception, OrderResult>> Junctions() =>
AddServices<INotificationService>(notifications)
.ShortCircuit<CheckCache>()
.Extract<OrderInput, OrderDetails>()
.Chain<ProcessOrder>()
.Chain<SendConfirmation>()
.Resolve();
}See also
- Move work around a chain into junctions
- Trains and Junctions, why the declaration is the only way
- Train Methods, every step a chain can take