Train Registration
Extension methods for registering Trax.Core trains with the .NET DI container. These methods wrap standard AddScoped/AddTransient/AddSingleton and add [Inject] property injection support.
Signatures
Generic Overloads
public static IServiceCollection AddScopedTraxRoute<TService, TImplementation>(
this IServiceCollection services
) where TService : class where TImplementation : class, TService
public static IServiceCollection AddTransientTraxRoute<TService, TImplementation>(
this IServiceCollection services
) where TService : class where TImplementation : class, TService
public static IServiceCollection AddSingletonTraxRoute<TService, TImplementation>(
this IServiceCollection services
) where TService : class where TImplementation : class, TServiceNon-Generic Overloads
public static IServiceCollection AddScopedTraxRoute(
this IServiceCollection services,
Type serviceInterface,
Type serviceImplementation
)
public static IServiceCollection AddTransientTraxRoute(
this IServiceCollection services,
Type serviceInterface,
Type serviceImplementation
)
public static IServiceCollection AddSingletonTraxRoute(
this IServiceCollection services,
Type serviceInterface,
Type serviceImplementation
)Type Parameters
| Type Parameter | Constraint | Description |
|---|---|---|
TService | class | The service interface type (e.g., IMyTrain) |
TImplementation | class, TService | The implementation type (e.g., MyTrain) |
Returns
IServiceCollection — for continued chaining.
Example
services.AddTransientTraxRoute<ICreateOrderTrain, CreateOrderTrain>();
services.AddScopedTraxRoute<IProcessPaymentTrain, ProcessPaymentTrain>();What It Does
- Registers
TImplementationwith the DI container at the specified lifetime. - Registers
TServicewith a factory that:- Resolves
TImplementationfrom the container - Calls
InjectProperties()to set all[Inject]-attributed properties on the instance - Sets
CanonicalNametoTService'sFullName(the interface name) — this is used as the train's canonical identifier in metadata, work queue entries, and subscription matching. See Canonical Train Naming. - Returns the fully-initialized instance
- Resolves
When to Use
Use these methods when your train class (or its base class) uses [Inject] properties. For example, ServiceTrain<TIn, TOut> has:
[Inject] public IEffectRunner? EffectRunner { get; set; }
[Inject] public ILogger<ServiceTrain<TIn, TOut>>? Logger { get; set; }
[Inject] public IJunctionEffectRunner? JunctionEffectRunner { get; set; }Without AddTraxRoute, these properties would remain null after DI resolution.
Remarks
- If your trains are discovered via AddMediator, you don't need to register them manually — the bus handles registration automatically.
- These methods are primarily useful for trains registered outside of assembly scanning, or when you need explicit control over the DI lifetime.
- The non-generic overloads accept
Typeparameters for dynamic/reflection-based registration scenarios.