Dependency Injection using SimpleInjector

What is Dependency Injection

Dependency injection is a technique that helps to inject dependant objects into your class thus making your architecture more loosely coupled.

Take for instance the following UserController class in the context of an ASP.NET MVC application:

public class UserController : Controller {
    private readonly IUserRepository repository;
    private readonly ILogger logger;

    // Use constructor injection for the dependencies
    public UserController(IUserRepository repository, ILogger logger) {
        this.repository = repository;
        this.logger = logger;

    // implement UserController methods here:
    public ActionResult Index() {
        this.logger.Log("Index called");
        return View(this.repository.GetAll());

public class SqlUserRepository : IUserRepository {
    private readonly ILogger logger;

    // Use constructor injection for the dependencies
    public SqlUserRepository(ILogger logger) {
        this.logger = logger;

    public User GetById(Guid id) {
        this.logger.Log("Getting User " + id);
        // retrieve from db.

The UserController class depends on the IUserRepository and ILogger interfaces. By not depending on concrete implementations, we can test UserController in isolation.

Introducing Simple Injector

Simple Injector is an easy-to-use Dependency Injection (DI) library for .NET 4+ frameworks. It is easily integrated with frameworks such as Web API, MVC, WCF, ASP.NET Core and many others.

The usage of Simple Injector consists of four to six steps:

  1. Create a new container
  2. Configure the container (Register)
  3. Verify the container
  4. Store the container for use by the application
  5. Retrieve instances from the container (Resolve)
  6. Dispose the container instance when the application ends.

The first four steps are performed only once at application startup. The fifth step is usually performed multiple times (usually once per request) for the majority of applications.

Using Simple Injector, the configuration of the application using the UserController and SqlUserRepository classes shown above, might look something like this:

protected void Application_Start(object sender, EventArgs e) {
    // 1. Create a new Simple Injector container
    var container = new Container();

    // 2. Configure the container (register)
    container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Transient);
    container.Register<ILogger, MailLogger>(Lifestyle.Singleton);

    // 3. Optionally verify the container's configuration.

    // 4. Register the container as MVC IDependencyResolver.
    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

The given configuration registers implementations for the IUserRepository and ILogger interfaces. In the given example, every time you ask the container for an IUserRepository, it will always create a new SqlUserRepository on your behalf (in DI terminology: an object with a Transient lifestyle).

The second registration maps the ILogger interface to a MailLogger implementation. This MailLogger is registered with the Singleton lifestyle; only one instance of MailLogger will ever be created by the Container.

Simple Injector will not only resolve the dependencies of UserController, but will instead build a whole tree structure of any level deep for you.

Register multiple implementations with single interface

You can register multiple implementation of the same interface with using the RegisterAll method

So you need to write:

container.RegisterAll<IInterface1>(typeof(Myclass1), typeof(Myclass2));

And now Simple Injector can inject a collection of Interface1 implementation into your constructor, for example:

public class Foo
    public Foo(IEnumerable<IInterface1> interfaces)

Or you can explicitly resolve your IInterface1 implementations with GetAllInstances:

var myClasses = container.GetAllInstances<IInterface1>();







Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s