EffectJunction
Package Trax.EffectNamespace Trax.Effect.Services.EffectJunctionSource EffectJunction.cs
A Junction that records a JunctionMetadata for each run and lets junction effects (the junction logger, junction progress, or an IJunctionEffectProvider of your own) run before and after it. It is written exactly as a Junction is, and runs only inside a ServiceTrain.
Signature
namespace Trax.Effect.Services.EffectJunction;
public abstract class EffectJunction<TIn, TOut> : Junction<TIn, TOut>
{
protected EffectJunction();
public JunctionMetadata? Metadata { get; }
public abstract override Task<TOut> Run(TIn input);
public sealed override Task<Either<Exception, TOut>> RailwayJunction<TTrainIn, TTrainOut>(
Either<Exception, TIn> previousOutput,
Train<TTrainIn, TTrainOut> train
);
public Task<Either<Exception, TOut>> RailwayJunction<TTrainIn, TTrainOut>(
Either<Exception, TIn> previousOutput,
ServiceTrain<TTrainIn, TTrainOut> serviceTrain
);
}Members
| Member | Description |
|---|---|
Run(TIn input) | The subclass's work, as on Junction. |
Metadata | The in-memory record of the current or most recent run of this junction: name, input and output types, start and end time, railway state, whether Run was called (HasRan), and OutputJson when the junction logger serializes output. null until the junction first runs, replaced at the start of each run. Not written to the database itself; the junction effects read it, and the junction logger logs it. |
RailwayJunction | Called by the train. The Train overload hands over to the ServiceTrain one. |
CancellationToken | Inherited from Junction. |
Behavior
Around each run:
- A fresh
Metadatais created, named after the junction class, with the train's metadata as its parent. - Each enabled junction effect's
BeforeJunctionExecutionruns, in registration order. - The start time is stamped, and the junction runs on the railway as a
Junctiondoes. A failed predecessor skipsRunand leavesHasRanfalse. - When junction events are on, a junction that is not skipped is reported as started and as ended.
- The end time and railway state are stamped.
- Each junction effect's
AfterJunctionExecutionruns, whether the junction succeeded, failed or was skipped.
An exception from a junction effect stops the remaining effects and fails the train, except when the railway already carries a failure: BeforeJunctionExecution throwing before a skipped junction, or AfterJunctionExecution throwing after the junction failed or was skipped, is logged, and the failure already on the railway is what the train reports.
| Condition | Exception |
|---|---|
Chained into a plain Train | TrainException: Cannot run an EffectJunction (<junction>) against a non-ServiceTrain (<train>) |
The ServiceTrain has no Metadata | TrainException |
Example
using Trax.Effect.Services.EffectJunction;
public class PersistOrder(IOrderStore store) : EffectJunction<PaymentReceipt, OrderResult>
{
public override async Task<OrderResult> Run(PaymentReceipt receipt)
{
var order = await store.Save(receipt, CancellationToken);
return new OrderResult(order.Id);
}
}See also
- EffectJunction vs Junction, which to derive from
- Junction
- IJunctionEffectProvider, a junction effect of your own