Constructor DependencyInjectionTypeRegistrar
- Namespace
- Ploch.CommandLine.Spectre.DependencyInjection
- Assembly
- Ploch.CommandLine.Spectre.dll
DependencyInjectionTypeRegistrar(IHostBuilder)
Provides an implementation of Spectre.Console's Spectre.Console.Cli.ITypeRegistrar that integrates with Microsoft.Extensions.DependencyInjection for dependency injection.
public DependencyInjectionTypeRegistrar(IHostBuilder builder)
Parameters
builderIHostBuilderThe host builder used to configure services.
Remarks
Lifetimes follow one rule. Everything registered through Register(Type, Type) — which is how Spectre registers every command, settings type, type converter and pair deconstructor — is transient: a new instance is created for every resolution, so a command holds its transient dependencies for its own lifetime rather than the host's. Dependency lifetimes are unchanged, so singleton services stay shared. RegisterInstance(Type, object) and RegisterLazy(Type, Func<object>?) are singletons, because that is what Spectre's contract asks for: the first must hand back the very instance it was given, and the second describes a lazily created instance: the first instance the factory creates successfully is shared by every later resolution, while a factory that throws is run again on the next resolution, because the container caches only a created instance.
The infrastructure the library registers itself — the console, the IOutput, the message formatter
processor and the application's CancellationTokenSource — stays singleton by design: those
are shared or stateless, and a command is meant to receive the same instance the application uses.
Spectre registers its types when the application runs, after every ConfigureServices delegate, and
the last registration of a service type wins. Registering a command type as a singleton from
ConfigureServices therefore has no effect; state that must outlive one resolution belongs in a
singleton service the command depends on.