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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ApacheTech.Common.DependencyInjection.Abstractions" Version="2.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApacheTech.Common.DependencyInjection.Abstractions" Version="2.3.0" />
                    
Directory.Packages.props
<PackageReference Include="ApacheTech.Common.DependencyInjection.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ApacheTech.Common.DependencyInjection.Abstractions --version 2.3.0
                    
#r "nuget: ApacheTech.Common.DependencyInjection.Abstractions, 2.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ApacheTech.Common.DependencyInjection.Abstractions@2.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ApacheTech.Common.DependencyInjection.Abstractions&version=2.3.0
                    
Install as a Cake Addin
#tool nuget:?package=ApacheTech.Common.DependencyInjection.Abstractions&version=2.3.0
                    
Install as a Cake Tool

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 of IServiceCollection.
  • ServiceDescriptor - describes a service registration (service type, implementation, factory and lifetime).
  • ActivatorUtilities - helpers to instantiate types using constructor injection from an IServiceProvider.
  • 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 or TransientAttribute.

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

  1. 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))));
  1. Resolve Services via an IServiceProvider (the project provides IServiceProvider extensions to help):
IServiceProvider provider = /* obtain provider from your composition root */;
var svc = provider.GetRequiredService<MyService>();
  1. 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 ServiceDescriptors based on the attributes and add them to the collection.

API Highlights

  • ServiceDescriptor - factory methods like Transient, Singleton and Describe to create registrations.
  • IServiceCollection / ServiceCollection - storage for ServiceDescriptors.
  • ServiceCollectionDescriptorExtensions - extension helpers such as Add, TryAdd, TryAddSingleton, TryAddTransient, Replace, and RemoveAll.
  • ActivatorUtilities - utilities for creating instances and compatible factories (CreateFactory, CreateInstance, GetServiceOrCreateInstance).
  • Annotations in Annotation namespace - SingletonAttribute, TransientAttribute and ActivatorUtilitiesConstructor.

Dependencies

  • ApacheTech.Common.Extensions (used for some reflection helpers)
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
2.3.0 115 9/14/2025
2.2.0 229 12/5/2024
2.1.2 376 2/15/2024
2.1.1 1,693 1/6/2023
2.1.0 2,459 7/29/2022

ApacheTech.Common.DependencyInjection.Abstractions v2.3.0

Added: Per Assembly disposal of services.
Build: Updated to .NET 8.0 SDK.