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
using AlmightyShogun.Utils;
using Microsoft.Extensions.DependencyInjection;
builder.Services
.RegisterOnInherit<ICommandHandler>(ServiceLifetime.Transient);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 sameIServiceCollection instance with matching discovered types registered.Type signature
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;