ApacheTech.Common.DependencyInjection.Abstractions
2.3.0
dotnet add package ApacheTech.Common.DependencyInjection.Abstractions --version 2.3.0
NuGet\Install-Package ApacheTech.Common.DependencyInjection.Abstractions -Version 2.3.0
<PackageReference Include="ApacheTech.Common.DependencyInjection.Abstractions" Version="2.3.0" />
<PackageVersion Include="ApacheTech.Common.DependencyInjection.Abstractions" Version="2.3.0" />
<PackageReference Include="ApacheTech.Common.DependencyInjection.Abstractions" />
paket add ApacheTech.Common.DependencyInjection.Abstractions --version 2.3.0
#r "nuget: ApacheTech.Common.DependencyInjection.Abstractions, 2.3.0"
#:package ApacheTech.Common.DependencyInjection.Abstractions@2.3.0
#addin nuget:?package=ApacheTech.Common.DependencyInjection.Abstractions&version=2.3.0
#tool nuget:?package=ApacheTech.Common.DependencyInjection.Abstractions&version=2.3.0
Minimal Dependency Injection
This is a minimal implmentation of the Microsoft.Extensions.DependencyInjection
package, including the ActivatorUtilities
class.
Allows for Singleton and Transient services to be registered with an IOC container. Scoped services have not been implemented.
This is designed as a very lightweight, and minimalistic, but this may come at a cost. This package doesn't come with a lot of the safety measures, and fallbacks that the full package contains. It was written from the ground up, other than the ActivatorUtilities
class. Therefore, it might not play well with thrid-party ICO wrappers like AutoFac, or CastleWindsor. It should not be seen as a replacement for Microsoft.Extensions.DependencyInjection.
The original purpose of this package was to act as an IOC container for game mods, where a full Enterprise level package simply comes with too much bloat. This is ideal for those kinds of scenarios, when you just need a quick and simple IOC container, without all the bloat.
Acknowledgement:
Inspired by Nick Chapsas' video tutorial on bespoke dependency injection solutions.
https://www.youtube.com/watch?v=NSVZa4JuTl8
Overview
This project provides a compact, focused implementation of dependency injection primitives. It includes:
IServiceCollection
- a lightweight service collection interface.ServiceCollection
- a simple list-backed implementation ofIServiceCollection
.ServiceDescriptor
- describes a service registration (service type, implementation, factory and lifetime).ActivatorUtilities
- helpers to instantiate types using constructor injection from anIServiceProvider
.- Attribute-based registration helpers:
SingletonAttribute
,TransientAttribute
, and scanning helpers to register annotated types from assemblies.
The goal is to offer a tiny, dependency-free alternative suitable for small projects, tools, or game modding where the full Microsoft.Extensions.DependencyInjection
is too heavy.
Features
- Register transient and singleton services via descriptors or convenience extension methods.
- Resolve services from an
IServiceProvider
instance. - Create instances that mix explicit constructor arguments with services from the provider via
ActivatorUtilities
. - Scan assemblies and register types annotated with
SingletonAttribute
orTransientAttribute
.
Important Notes / Limitations
- This implementation intentionally omits scoped services.
- It is minimal and does not implement all safeguards, diagnostic features or integrations that the official Microsoft DI provides.
- Behaviour may differ from the official DI in edge cases - test before replacing the official DI in critical systems.
Quick Start
- Add services to a
ServiceCollection
:
var services = new ApacheTech.Common.DependencyInjection.ServiceCollection();
// Register concrete type as singleton
services.TryAddSingleton<MyService>();
// Register service with implementation
services.TryAddTransient(typeof(IMyService), typeof(MyService));
// Register using factory
services.TryAddSingleton(typeof(IMyService), sp => new MyService(sp.GetService(typeof(IOther))));
- Resolve Services via an
IServiceProvider
(the project providesIServiceProvider
extensions to help):
IServiceProvider provider = /* obtain provider from your composition root */;
var svc = provider.GetRequiredService<MyService>();
- Create Instances with
ActivatorUtilities
:
// Create instance of a type and let the container satisfy constructor dependencies
var instance = ActivatorUtilities.CreateInstance(provider, typeof(MyType), someExplicitArg);
// Generic helper
var typed = ActivatorUtilities.CreateInstance<MyType>(provider, someExplicitArg);
// Get service or create instance if not registered
var maybe = ActivatorUtilities.GetServiceOrCreateInstance(provider, typeof(MyOtherType));
Attribute Based Registration
Annotate classes with SingletonAttribute
or TransientAttribute
to make them discoverable by the scanning helpers.
Example:
[Singleton(typeof(IMyService))]
public class MyService : IMyService { }
[Transient]
public class OtherService { }
Then register annotated types from an assembly:
var services = new ApacheTech.Common.DependencyInjection.ServiceCollection();
services.AddAnnotatedServicesFromAssembly(typeof(MyService));
This will create ServiceDescriptor
s based on the attributes and add them to the collection.
API Highlights
ServiceDescriptor
- factory methods likeTransient
,Singleton
andDescribe
to create registrations.IServiceCollection
/ServiceCollection
- storage forServiceDescriptor
s.ServiceCollectionDescriptorExtensions
- extension helpers such asAdd
,TryAdd
,TryAddSingleton
,TryAddTransient
,Replace
, andRemoveAll
.ActivatorUtilities
- utilities for creating instances and compatible factories (CreateFactory
,CreateInstance
,GetServiceOrCreateInstance
).- Annotations in
Annotation
namespace -SingletonAttribute
,TransientAttribute
andActivatorUtilitiesConstructor
.
Dependencies
ApacheTech.Common.Extensions
(used for some reflection helpers)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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 was computed. 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. |
-
net8.0
- ApacheTech.Common.Extensions (>= 2.1.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on ApacheTech.Common.DependencyInjection.Abstractions:
Package | Downloads |
---|---|
ApacheTech.Common.DependencyInjection
A minimal implmentation of the `Microsoft.Extensions.DependencyInjection` package, including the `ActivatorUtilities` class. |
|
VintageStory.Gantry
Gantry MDK is a Mod Developent Kit, used to create third-party plugins for the game Vintage Story, by Anego Studios. |
GitHub repositories
This package is not used by any popular GitHub repositories.
ApacheTech.Common.DependencyInjection.Abstractions v2.3.0
Added: Per Assembly disposal of services.
Build: Updated to .NET 8.0 SDK.