SimpleInjection 0.9.6.7

Requires NuGet 4.1.0 or higher.

dotnet add package SimpleInjection --version 0.9.6.7
                    
NuGet\Install-Package SimpleInjection -Version 0.9.6.7
                    
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="SimpleInjection" Version="0.9.6.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleInjection" Version="0.9.6.7" />
                    
Directory.Packages.props
<PackageReference Include="SimpleInjection" />
                    
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 SimpleInjection --version 0.9.6.7
                    
#r "nuget: SimpleInjection, 0.9.6.7"
                    
#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 SimpleInjection@0.9.6.7
                    
#: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=SimpleInjection&version=0.9.6.7
                    
Install as a Cake Addin
#tool nuget:?package=SimpleInjection&version=0.9.6.7
                    
Install as a Cake Tool

<img src="https://github.com/DerekGooding/SimpleInjection/blob/main/Icon.png" width=50 height=50/> SimpleInjection

A lightweight dependency injection library for C# that combines simple DI container functionality with powerful source generation for content management.

Features

🚀 Simple Dependency Injection

  • Attribute-based registration - Mark classes with [Singleton], [Scoped], or [Transient]
  • Automatic service discovery - No manual registration required
  • Constructor injection - Automatic dependency resolution
  • Scope management - Built-in scoped service lifetime management

⚡ Content Source Generation

  • Automatic enum generation - Creates enums from your content collections
  • Type-safe access - Generated helper methods for accessing content by enum or index
  • Performance optimized - Uses NamedComparer<T> for fast dictionary lookups
  • Roslyn analyzers - Enforces best practices and catches common mistakes

Quick Start

1. Install the Package

dotnet add package SimpleInjection

2. Dependency Injection Usage

Mark your classes with lifetime attributes:

[Singleton]
public class DatabaseService
{
    public void Connect() => Console.WriteLine("Connected to database");
}

[Scoped]
public class UserService
{
    private readonly DatabaseService _database;
    
    public UserService(DatabaseService database)
    {
        _database = database;
    }
    
    public void GetUser() => _database.Connect();
}

Initialize and use the host:

var host = Host.Initialize();

// Get singleton services directly
var dbService = host.Get<DatabaseService>();

// Create scopes for scoped services
using var scope = host.CreateScope();
var userService = scope.Get<UserService>();

3. Content Generation Usage

Define your content classes:

// Your content item must implement INamed
public record Material(string Name, string Color, int Durability) : INamed;

// Your content collection must implement IContent<T>
[Singleton]
public partial class Materials : IContent<Material>
{
    public Material[] All { get; } = 
    [
        new("Steel", "Gray", 100),
        new("Wood", "Brown", 50),
        new("Gold", "Yellow", 25)
    ];
}

The source generator automatically creates:

// Generated enum
public enum MaterialsType
{
    Steel,
    Wood,
    Gold
}

// Generated helper methods
public partial class Materials
{
    public Material Get(MaterialsType type) => All[(int)type];
    public Material this[MaterialsType type] => All[(int)type];
    public Material GetById(int id) => All[id];
    public Material Steel => All[0];
    public Material Wood => All[1];
    public Material Gold => All[2];
}

Use the generated code:

var materials = host.Get<Materials>();

// Type-safe access using enums
var steel = materials[MaterialsType.Steel];
var wood = materials.Get(MaterialsType.Wood);

// Direct property access
var gold = materials.Gold;

// Index-based access
var firstMaterial = materials.GetById(0);

Advanced Features

Performance Optimizations

The library includes NamedComparer<T> for efficient dictionary operations with INamed keys:

// Use ToNamedDictionary extension method
var materialDict = materials.All.ToNamedDictionary(m => m.Durability);

// Or explicitly specify the comparer
var dict = new Dictionary<Material, int>(new NamedComparer<Material>());

SubContent Collections

For hierarchical content organization:

public class WeaponStats : ISubContent<Material, int>
{
    public Dictionary<Material, int> ByKey { get; }
    public int this[Material material] => ByKey[material];
}

Roslyn Analyzers

The package includes analyzers that help you:

  • NC001: Ensures Dictionary<TKey, TValue> uses NamedComparer<T> for INamed keys
  • TND001: Suggests using ToNamedDictionary() instead of ToDictionary() for INamed keys

Requirements

  • .NET 8.0 or .NET 9.0
  • C# with nullable reference types enabled (recommended)

How It Works

  1. Service Discovery: The host scans all loaded assemblies for classes marked with lifetime attributes
  2. Dependency Resolution: Constructor parameters are automatically resolved from registered services
  3. Source Generation: The generator scans for classes implementing IContent<T> and generates enums and helper methods
  4. Code Analysis: Roslyn analyzers ensure best practices for dictionary usage with named keys

Best Practices

  • Use [Singleton] for stateless services and shared resources
  • Use [Scoped] for services that should be unique per operation/request
  • Use [Transient] for lightweight, stateless services that need fresh instances
  • Always implement INamed for content objects to enable source generation
  • Use the generated enums for type-safe content access
  • Leverage ToNamedDictionary() for performance when working with INamed collections

License

MIT License - see the license file for details.

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 is compatible.  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.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.9.6.7 116 7/10/2025
0.9.6.6 114 7/10/2025
0.9.6.5 116 7/10/2025
0.9.6.3 81 6/27/2025
0.9.6.2 76 6/27/2025
0.9.6.1 79 6/27/2025
0.9.6 272 6/11/2025
0.9.5 261 6/11/2025
0.9.3 262 6/10/2025
0.9.2 265 6/10/2025
0.9.1 261 6/10/2025
0.9.0 263 6/10/2025