Skip to content

RegisterOnInherit ​

Scans assemblies for concrete types assignable to T and registers each one, for command handlers, recurring jobs, validation rules, and anything else better discovered than listed by hand. Interfaces and abstract classes are never registered, and neither is a type carrying SkipAutoRegistration.

Registrations are added rather than replaced, so scanning the same assembly twice registers everything twice.

Usage ​

csharp
using AlmightyShogun.Utils;
using Microsoft.Extensions.DependencyInjection;

builder.Services
    .RegisterOnInherit<ICommandHandler>(ServiceLifetime.Transient);
csharp
using AlmightyShogun.Utils;
using Microsoft.Extensions.DependencyInjection;

builder.Services.RegisterOnInherit<ICommandHandler>(
    [typeof(ImportCommandHandler).Assembly],
    ServiceLifetime.Transient,
    registerAsBaseType: false,
    filter: type => type.Name.EndsWith("CommandHandler")
);

TIP

Assemblies always go in as an array; no overload takes a single Assembly or params. registerAsBaseType and filter live only there, so reaching for either means passing the assemblies explicitly.

Parameters

assemblies: Assembly[]
The assemblies to scan, in the order they should be searched. An empty array registers nothing, and the overload taking none scans the calling assembly.

serviceLifetime: ServiceLifetime
Lifetime used for each discovered service registration.
Default: ServiceLifetime.Singleton

registerAsBaseType: bool
Registers each discovered implementation under service type T when true, which is what a consumer resolving IEnumerable<T> needs. Registers it under its own concrete type when false. Only on the overload taking assemblies.
Default: true

filter: Func<Type, bool>?
Predicate applied to each discovered type. Only types it accepts are registered. Only on the overload taking assemblies.
Default: null

Returns

The same IServiceCollection instance with matching discovered types registered.

Type signature ​

csharp
public IServiceCollection RegisterOnInherit<T>(
    ServiceLifetime serviceLifetime = ServiceLifetime.Singleton
) where T : class;

public IServiceCollection RegisterOnInherit<T>(
    Assembly[] assemblies,
    ServiceLifetime serviceLifetime = ServiceLifetime.Singleton,
    bool registerAsBaseType = true,
    Func<Type, bool>? filter = null
) where T : class;

All packages are released under the MIT License.