Reference

AddEffects

Package Trax.EffectNamespaces Trax.Effect.Extensions, Trax.Effect.Configuration.TraxEffectBuilder, Trax.Effect.Configuration.TraxEffectConfigurationSource ServiceExtensions.cs, TraxEffectBuilder.cs, TraxEffectBuilderWithData.cs, ITraxEffectConfiguration.cs, TraxEffectConfiguration.cs

The first step on the AddTrax builder. Configures the effect system: the data provider, the effects that run around each train and junction, lifecycle hooks, broadcasting and deciders. The callback receives a TraxEffectBuilder and returns the builder from its last chained call.

Signatures

public static TraxBuilderWithEffects AddEffects(
    this TraxBuilder builder,
    Func<TraxEffectBuilder, TraxEffectBuilder> configure
)
 
public static TraxBuilderWithEffects AddEffects(
    this TraxBuilder builder
)

Parameters

ParameterTypeDescription
configureFunc<TraxEffectBuilder, TraxEffectBuilder>Chains the effect methods and returns the builder from the last call. The parameterless overload is AddEffects(effects => effects).

Returns

TraxBuilderWithEffects, on which AddMediator is the next step.

Behavior

configure runs once. The builder it returns is built into the ITraxEffectConfiguration that AddTrax registers. With no data provider configured, the methods that need one fail when the configuration is built: AddJunctionProgress() throws InvalidOperationException from AddEffects, and AddScheduler() throws InvalidOperationException naming the three providers.

TraxEffectBuilder

PropertyTypeDefaultDescription
ServiceCollectionIServiceCollectionThe host's service collection, for registrations of your own
EffectRegistryIEffectRegistry?The registry effects are registered in
HasDataProviderboolfalseTrue once a data provider method ran. Set only by those methods.
HasDatabaseProviderboolfalseTrue once UsePostgres or UseSqlite ran. Set only by those methods.
MigrationsDisabledboolfalseSet by SkipMigrations
JunctionProgressEnabledboolfalseSet by AddJunctionProgress; building the configuration then requires a data provider
SerializeJunctionDataboolfalseSet by AddJunctionLogger(serializeJunctionData:)
LogLevelLogLevelLogLevel.DebugSet by SetEffectLogLevel
TrainParameterJsonSerializerOptionsJsonSerializerOptionsTraxJsonSerializationOptions.DefaultReplaced by SaveTrainParameters when it is given options

All but HasDataProvider and HasDatabaseProvider are hidden from completion: the builder methods set them.

TraxEffectBuilderWithData

UsePostgres, UseSqlite and UseInMemory return TraxEffectBuilderWithData, a subclass of TraxEffectBuilder that carries the state set before it. The methods that need a data provider extend only this type. Called on a plain TraxEffectBuilder, each binds to an overload marked obsolete as an error, so it fails to compile with the instruction, for example Call UsePostgres(...), UseSqlite(...) or UseInMemory(...) before AddDataContextLogging(...).

The generic methods (TBuilder : TraxEffectBuilder) return the builder type they were called on, so a chain that starts with a data provider keeps TraxEffectBuilderWithData through them.

MethodExtendsPackage
UsePostgres, SkipMigrationsTraxEffectBuilderTrax.Effect.Data.Postgres
UseSqliteTraxEffectBuilderTrax.Effect.Data.Sqlite
UseInMemoryTraxEffectBuilderTrax.Effect.Data.InMemory
AddDataContextLogging, AddDecisionRecording, AddJunctionEventsTraxEffectBuilderWithDataTrax.Effect.Data
AddEffect, AddJunctionEffect, AddLifecycleHookTraxEffectBuilderTrax.Effect
SetEffectLogLevel, UseBroadcasterTBuilderTrax.Effect
AddJsonTBuilderTrax.Effect.Provider.Json
SaveTrainParametersTBuilderTrax.Effect.Provider.Parameter
AddJunctionLoggerTBuilderTrax.Effect.JunctionProvider.Logging
AddJunctionProgressTBuilderTrax.Effect.JunctionProvider.Progress
AddNimbleDecider, AddSystemOneDeciderTBuilderTrax.Effect.Decisions.SystemOne

AddEffect, AddJunctionEffect and AddLifecycleHook return TraxEffectBuilder, so a chain that needs TraxEffectBuilderWithData afterwards calls them before the data provider.

ITraxEffectConfiguration

The built configuration, registered as a singleton.

MemberDescription
LogLevelThe builder's LogLevel
SerializeJunctionDataThe builder's SerializeJunctionData
SystemJsonSerializerOptionsThe builder's TrainParameterJsonSerializerOptions
TraxEffectConfiguration.StaticSystemJsonSerializerOptionsStatic. The same options, set process-wide when the configuration is built

Examples

An expression lambda returns its last call:

services.AddTrax(trax => trax
    .AddEffects(effects => effects
        .UsePostgres(connectionString)
        .AddDataContextLogging()
        .AddJson()
        .SaveTrainParameters()
        .AddJunctionLogger()
        .AddJunctionProgress()
    )
    .AddMediator(typeof(IShipOrderTrain).Assembly)
);

A block lambda returns the builder itself:

services.AddTrax(trax => trax
    .AddEffects(effects =>
    {
        effects.ServiceCollection.AddSingleton(TimeProvider.System);
        return effects.UsePostgres(connectionString).AddJson();
    })
    .AddMediator(typeof(IShipOrderTrain).Assembly)
);

See also