Simple Injector

Simple Injector

Simple Injector is a dependency injection (DI) container for .NET that focuses on being fast, simple, and easy to reason about compared to heavier frameworks. Simple Injector is easily integrated with frameworks such as Web API, MVC, WCF and many others. It’s easy to implement the dependency injection pattern with loosely coupled components using Simple Injector.

Simple Injector has a carefully selected set of features in its core library to support many advanced scenarios. Simple Injector has been designed to run in partial/medium trust. Simple Injector supports code based configuration and comes with built-in diagnostics services for identifying many common configuration problems.

Simple Injector is open source and published under the permissive MIT license. Simple injector is, and always will be, free. Free to use. Free to copy. Free to change.

All contributions to Simple Injector are covered by a comprehensive contributors license agreement to help ensure that all of the code contributed to the Simple Injector project cannot later be claimed as belonging to any individual or group.

Why use Simple Injector?

Without DI:

var service = new OrderService(new OrderRepository());

With Simple Injector:

• You register dependencies once
• The container builds the object graph for you
• You avoid manual wiring everywhere

Basic Simlple Injector Example

1. Define services

public interface IRepository
{
    string GetData();
}

public class Repository : IRepository
{
    public string GetData() => "Hello from DB";
}

public class Service
{
    private readonly IRepository _repo;

    public Service(IRepository repo)
    {
        _repo = repo;
    }

    public void Run()
    {
        Console.WriteLine(_repo.GetData());
    }
}

2. Configure Simple Injector

using SimpleInjector;

var container = new Container();

container.Register<IRepository, Repository>();
container.Register<Service>();

container.Verify();

3. Resolve and use

var service = container.GetInstance<Service>();
service.Run();

Key features of SI

• Very fast performance
• Strict lifestyle management (Singleton, Scoped, Transient)
• Compile-time validation (via Verify) to catch configuration errors early
• Minimal magic (no heavy reflection tricks)
• Works with ASP.NET Core, console apps, desktop apps
• Encourages clean architecture

Key concepts of Simple Injector

• Container → central registry of dependencies
• Registration → mapping interfaces to implementations
• Lifestyle → object lifetime management:
• Transient (new each time)
• Singleton (one instance)
• Scoped (per request / scope)
• Verification → ensures dependency graph is valid

Advantages

• Very fast and lightweight
• Predictable behavior (less “magic” than other containers)
• Strong diagnostics and error detection
• Good for clean architecture enforcement
• Actively maintained and widely used in .NET ecosystem

Disadvantages

• Not as feature-rich as some enterprise containers
• Less flexible for some advanced dynamic scenarios
• Requires manual registration (less auto-discovery)
• Can feel strict compared to “auto-wiring” containers

When to use SI?

Use Simple Injector when:

• You want high-performance DI
• You prefer explicit configuration over magic
• You are building clean architecture / layered systems
• You want strict validation of dependencies

Avoid it when:

• You want heavy auto-discovery / convention-based DI
• You prefer fully integrated framework DI (like ASP.NET Core built-in DI)
• You need advanced interception or dynamic runtime composition

Alternatives

• Built-in .NET DI (Microsoft.Extensions.DependencyInjection)
• Autofac (very flexible, feature-rich)
• Ninject (older, dynamic style)
• StructureMap (legacy but influential)
• DryIoc (very fast, modern alternative)

Contents related to 'Simple Injector'

Lazy Initialization
Lazy Initialization
Extension Methods
Extension Methods
Weak References
Weak References
Scaldi
Scaldi