Snowberry.DependencyInjection.Abstractions
6.0.0
dotnet add package Snowberry.DependencyInjection.Abstractions --version 6.0.0
NuGet\Install-Package Snowberry.DependencyInjection.Abstractions -Version 6.0.0
<PackageReference Include="Snowberry.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageVersion Include="Snowberry.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Snowberry.DependencyInjection.Abstractions" />
paket add Snowberry.DependencyInjection.Abstractions --version 6.0.0
#r "nuget: Snowberry.DependencyInjection.Abstractions, 6.0.0"
#:package Snowberry.DependencyInjection.Abstractions@6.0.0
#addin nuget:?package=Snowberry.DependencyInjection.Abstractions&version=6.0.0
#tool nuget:?package=Snowberry.DependencyInjection.Abstractions&version=6.0.0
Snowberry.DependencyInjection.Abstractions
The contracts of Snowberry.DependencyInjection: the container, registry, scope and factory interfaces, the injection attributes, ServiceDescriptor and ServiceLifetime, the exceptions the container throws, and the extension methods used to register and resolve services. It targets .NET 11, .NET 10 and .NET Standard 2.0, is annotated for trimming and Native AOT, and has no runtime dependencies.
Reference it from a library that registers or resolves services without taking a dependency on the container implementation; the application that composes everything references Snowberry.DependencyInjection, which brings this package along.
Table of Contents
- Installation
- Quick Start
- Top Features at a Glance
- What Is Inside
- Extension Methods
- Pitfalls
- Related Packages
Installation
dotnet add package Snowberry.DependencyInjection.Abstractions
Quick Start
using Snowberry.DependencyInjection;
using Snowberry.DependencyInjection.Abstractions.Extensions;
using Snowberry.DependencyInjection.Abstractions.Interfaces;
// 3. The application, which references Snowberry.DependencyInjection, composes and resolves.
using var container = new ServiceContainer();
container.AddGreeting();
Console.WriteLine(container.GetRequiredService<IGreeter>().Greet("world"));
// 1. A library that references only the abstractions declares its services...
public interface IGreeter { string Greet(string name); }
internal sealed class Greeter : IGreeter { public string Greet(string name) => $"Hello, {name}!"; }
// 2. ...and exposes a registration method over the IServiceRegistry contract.
public static class GreetingRegistration
{
public static IServiceRegistry AddGreeting(this IServiceRegistry registry)
{
registry.RegisterSingleton<IGreeter, Greeter>();
return registry;
}
}
Top Features at a Glance
- Container-agnostic registration. Write
AddXyz(this IServiceRegistry registry)methods against the interfaces; anyIServiceRegistryimplementation can consume them. - Typed registration helpers.
RegisterSingleton,RegisterScopedandRegisterTransientwith implementation types, instances orServiceInstanceFactory<T>factories, each optionally keyed, plusTryRegister*variants. - Typed resolution helpers.
GetService<T>,GetRequiredService<T>,GetKeyedService<T>,GetRequiredKeyedService<T>,CreateScopeandCreateInstance<T>on anyIServiceProvider. - Injection attributes.
[Inject],[FromKeyedServices]and[PreferredConstructor]mark types for the container without referencing it. - Explicit failures. Dedicated exceptions (
ServiceTypeNotRegistered,CircularDependencyException,ServiceValidationException, ...) and a structuredServiceValidationErrorlist from validation. - Trimming-safe signatures. Type parameters and
Typearguments that are activated carry[DynamicallyAccessedMembers], so the trimmer keeps the constructors and properties the container needs.
What Is Inside
| Export | Kind | Purpose |
|---|---|---|
IServiceContainer |
Interface | The full container: registry, descriptor lookup, keyed service provider and disposal. |
IServiceRegistry |
Interface | Register, UnregisterService and IsServiceRegistered. |
IServiceDescriptorReceiver |
Interface | Looks up registered IServiceDescriptors; enumerable over all of them. |
IKeyedServiceProvider |
Interface | IServiceProvider plus GetKeyedService(Type, object?). |
IServiceScopeFactory / IScope |
Interfaces | Create scopes; a scope exposes its provider, scoped-instance cache and disposables. |
IServiceFactory |
Interface | Constructs instances of implementation types; the container's activation strategy. |
IDisposableContainer |
Interface | Tracks the disposables owned by a scope or container. |
IServiceDescriptor / ServiceDescriptor |
Interface / class | A registration: service type, implementation type, lifetime, instance or factory. ServiceDescriptor.Singleton/Scoped/Transient create one. |
IServiceIdentifier / IScopeServiceCacheEntry |
Interfaces | A service type plus optional key; a cached scoped instance. |
ServiceInstanceFactory / ServiceInstanceFactory<T> |
Delegates | Factories that receive the resolving provider and the service key. |
ServiceLifetime |
Enum | Singleton, Scoped, Transient. |
InjectAttribute, FromKeyedServicesAttribute, PreferredConstructorAttribute |
Attributes | Property injection, keyed dependency selection, constructor selection. |
ServiceTypeNotRegistered, CircularDependencyException, ServiceRegistryReadOnlyException, ServiceScopeRequiredException, InvalidServiceImplementationType, ServiceValidationException |
Exceptions | The failures the container reports. |
ServiceValidationError / ServiceValidationErrorKind |
Class / enum | One validation problem: MissingDependency, CircularDependency or NoPublicConstructor. |
ServiceRegistryExtensions, ServiceProviderExtensions |
Static classes | The registration and resolution helpers below. |
ThrowHelper |
Static class | Throw helpers for IServiceFactory implementations. |
Extension Methods
Registration, on IServiceRegistry (namespace Snowberry.DependencyInjection.Abstractions.Extensions):
| Method | Registers |
|---|---|
RegisterSingleton<T>(), RegisterSingleton<T, TImpl>() |
A singleton constructed by the container. |
RegisterSingleton<T>(T instance), RegisterSingleton<T, TImpl>(TImpl instance) |
A pre-built singleton instance (not disposed by the container). |
RegisterSingleton<T>(ServiceInstanceFactory<T>), RegisterSingleton<T, TImpl>(ServiceInstanceFactory<TImpl>) |
A singleton created by a factory. |
RegisterScoped<...>(...), RegisterTransient<...>(...) |
The same type and factory forms for the other lifetimes. |
TryRegister(descriptor), TryRegisterSingleton/Scoped/Transient<...>(...) |
Like the above, but returns false instead of registering when the service is already registered. |
Every registration method takes an optional serviceKey.
Resolution, on IServiceProvider / IKeyedServiceProvider:
| Method | Returns |
|---|---|
GetService<T>(), GetKeyedService<T>(key) |
The service, or default when it is not registered. |
GetRequiredService<T>(), GetRequiredService(Type) |
The service; throws ServiceTypeNotRegistered when it is not registered. |
GetRequiredKeyedService<T>(key), GetRequiredKeyedService(Type, key) |
The keyed service; throws ServiceTypeNotRegistered when it is not registered. |
CreateScope() |
A new IScope from the provider's IServiceScopeFactory. |
CreateInstance<T>(), CreateInstance(Type) |
A new instance of an unregistered type, with its dependencies injected. |
Pitfalls
- The abstractions do not contain a container.
ServiceContainerandServiceContainerOptionslive inSnowberry.DependencyInjection; reference that package in the application that composes the services. - A wrong implementation type fails at resolve time.
GetService<T>and the other typed helpers throwInvalidCastExceptionwhen the registered instance is not aT. CreateInstanceis not trimming or AOT safe. It is annotated[RequiresUnreferencedCode]and[RequiresDynamicCode]because it can close generic types at runtime; prefer registering the type.- Async disposal is .NET only. On .NET Standard 2.0 the interfaces do not include
IAsyncDisposableandRegisterDisposable(IAsyncDisposable).
Related Packages
| Package | Purpose |
|---|---|
| Snowberry.DependencyInjection | The container that implements these contracts. |
| Snowberry.Mediator.DependencyInjection | Registers Snowberry.Mediator handlers in the Snowberry container. |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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. net11.0 is compatible. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net10.0
- No dependencies.
-
net11.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Snowberry.DependencyInjection.Abstractions:
| Package | Downloads |
|---|---|
|
Snowberry.DependencyInjection
A lightweight easy to use IoC container for .NET. |
GitHub repositories
This package is not used by any popular GitHub repositories.