NCode.Registration
0.1.1
Prefix Reserved
dotnet add package NCode.Registration --version 0.1.1
NuGet\Install-Package NCode.Registration -Version 0.1.1
<PackageReference Include="NCode.Registration" Version="0.1.1" />
<PackageVersion Include="NCode.Registration" Version="0.1.1" />
<PackageReference Include="NCode.Registration" />
paket add NCode.Registration --version 0.1.1
#r "nuget: NCode.Registration, 0.1.1"
#:package NCode.Registration@0.1.1
#addin nuget:?package=NCode.Registration&version=0.1.1
#tool nuget:?package=NCode.Registration&version=0.1.1
NCode.Registration
Dependency injection registration for .NET that proves each library's prerequisites at compile time: every Add…Library() returns a marker builder, IServiceBuilder<TMarker>, that the libraries it needs extend, so a registration whose prerequisite is missing doesn't compile. BindStrictly binds a configuration section so that a key no property takes stops the host instead of being ignored.
Installation
dotnet add package NCode.Registration
The package depends on Microsoft.Extensions.DependencyInjection.Abstractions and Microsoft.Extensions.Configuration.Binder.
Example
Each library declares a marker, an empty readonly struct, and its Add…Library() returns the builder for that marker. A library with no prerequisite extends IServiceCollection; a library that needs another extends that library's builder:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NCode.Registration;
public readonly struct ClockLibrary : IRegistrationMarker<ClockLibrary>;
public readonly struct GreetingLibrary : IRegistrationMarker<GreetingLibrary>;
public sealed class GreetingOptions
{
public string Greeting { get; set; } = "Hello";
}
public static class ClockLibraryRegistration
{
extension(IServiceCollection services)
{
public IServiceBuilder<ClockLibrary> AddClockLibrary()
{
services.TryAddSingleton(TimeProvider.System);
return services.NewBuilder<ClockLibrary>();
}
}
}
public static class GreetingLibraryRegistration
{
extension(IServiceBuilder<ClockLibrary> builder)
{
public IServiceBuilder<GreetingLibrary> AddGreetingLibrary(IConfigurationSection section)
{
builder
.ServiceCollection.AddOptions<GreetingOptions>()
.Configure(options => section.BindStrictly(options))
.ValidateOnStart();
return builder.NewBuilder<GreetingLibrary>();
}
}
}
A host registers the libraries in order, and services.AddGreetingLibrary(…) without AddClockLibrary() before it doesn't compile:
services.AddClockLibrary().AddGreetingLibrary(configuration.GetSection("Greeting"));
Registering a library's marker twice throws InvalidOperationException, and a Greeting section with a key such as Greting that GreetingOptions has no property for stops the host as it starts.
The repository's readme shows how to require a second library, share one marker among several registrations, register several instances of one service and bind configuration strictly.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Configuration.Binder (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
See the changelog: https://github.com/NCodeGroup/Registration/blob/HEAD/CHANGELOG.md