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 nowSaraff.IoC2.x. The namespace and theServiceContainertype 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:
- Register — learn which implementation to create for a service type.
- Resolve — create the object and inject its dependencies.
- Control lifetime — decide whether an instance is reused, scoped or created every time.
- 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
- Getting Started — install, first container, first service
- Introduction — IoC, DIP, DI and containers in plain language
- Features — complete feature matrix
Core concepts
Using the container
- Registration —
Register, factories, instances, names, open generics - Lifetimes and Scopes — singleton, scoped, transient, disposal rules
- Injection Patterns — constructor, property, method, lazy, context bindings
- Nested Containers — container trees and isolation
- Factories and
CreateInstance— create objects that are not registered
Advanced topics
- Async Initialization — async startup, async disposal,
AsyncLazy<T> - Interception and Listeners — proxies, logging, security, exception handling
- Configuration — tuning the container through configuration groups
- Customization —
IContainerCore, custom lifetimes, custom API facade - API Reference — public types at a glance
- Troubleshooting — common errors and how to fix them
Legacy, samples and migration
Design principles
- Explicit over implicit. Mark the constructor, property or parameter that needs a dependency
with
[ServiceRequired]; the container never guesses a constructor. - Predictable lifetime.
Singletonlives with the registration owner,Scopedlives with the scope,Transientis created on every resolution. - Deterministic cleanup. Disposing a scope releases only its scoped services; disposing the root releases nested scopes and root-owned services.
- No hidden magic. Registration and resolution are ordinary method calls; configuration is an
immutable snapshot that you can refresh with
UseConfiguration.