View on GitHub

SARAFF SOFTWARE

All products

Saraff.IoC

Saraff.IoC is a lightweight, thread-safe dependency injection (DI) container for .NET. It helps you split an application into loosely-coupled, highly-cohesive pieces and glue them back together in a flexible way — with constructor, property and method injection, scopes, lifetimes, interception and asynchronous initialization.

   
Package Saraff.IoC
Version 2.0
Target frameworks net40, netstandard2.0
Namespace Saraff.IoC
License LGPL-3.0-or-later

Migrating from Saraff.IoCX? The package is now Saraff.IoC 2.x. The namespace and the ServiceContainer type are unchanged. See Migration.

The container in one picture

flowchart LR
    subgraph Design [1. Design]
        I["Interface<br/>IGreeting"]
        C["Implementation<br/>Greeting"]
        I -.->|implements| C
    end

    R["2. Register<br/>Register IGreeting -> Greeting"]
    S["3. Resolve<br/>Resolve IGreeting"]
    O["Ready-to-use object<br/>with dependencies injected"]
    D["4. Release<br/>Dispose / DisposeAsync"]

    C --> R --> S --> O --> D

The container has four responsibilities:

  1. Register — learn which implementation to create for a service type.
  2. Resolve — create the object and inject its dependencies.
  3. Control lifetime — decide whether an instance is reused, scoped or created every time.
  4. Release — dispose created services and nested scopes at the right moment.

Quick start

using System;
using Saraff.IoC;

public interface IGreeting {
    string Greet();
}

public sealed class Greeting : IGreeting {
    public string Greet() => "hello";
}

using (var container = new ServiceContainer()) {
    container.Register<IGreeting, Greeting>();

    var greeting = container.Resolve<IGreeting>();
    Console.WriteLine(greeting.Greet()); // hello
}

Install the package:

dotnet add package Saraff.IoC

What is inside

Area What you get
Registration Type, factory and instance registrations; named registrations; multiple registrations; open generics; IEnumerable<T> resolution
Lifetimes Singleton, Scoped, Transient; custom lifetimes through IServiceLifetime
Scopes CreateScope() for request/unit-of-work isolation; deterministic disposal
Injection Constructor, property, method and lazy injection; context-aware bindings; IServiceProvider support
Interception [ProxyRequired] services, IListener, short-circuiting, result replacement, exception mapping
Async IAsyncInitializable, ResolveAsync, InitializeAsync, DisposeAsync, AsyncLazy<T> (netstandard2.0)
Extensibility Configuration groups, custom lifetimes, registration descriptors, async adapters, neutral IContainerCore API
Legacy The original Bind/Load/[BindService] API is preserved and works side by side with the new API

Documentation map

Start here

Core concepts

Using the container

Advanced topics

Legacy, samples and migration

Design principles