All Projects → simpleinjector → Simpleinjector

simpleinjector / Simpleinjector

Licence: mit
An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.

Projects that are alternatives of or similar to Simpleinjector

ThunderboltIoc
One of the very first IoC frameworks for .Net that has no reflection. An IoC that casts its services before thunder casts its bolts.
Stars: ✭ 40 (-95.86%)
Mutual labels:  dependency-injection, ioc-container
brisk-ioc
fast light brisk ioc/di container on nodejs; Node下快速 轻量的IoC/DI容器,依赖注入,配合装饰器使用
Stars: ✭ 12 (-98.76%)
Mutual labels:  dependency-injection, ioc-container
alpha-dic
Powerful dependency injection container for node.js
Stars: ✭ 27 (-97.2%)
Mutual labels:  dependency-injection, ioc-container
di
🛠 A full-featured dependency injection container for go programming language.
Stars: ✭ 156 (-83.85%)
Mutual labels:  dependency-injection, ioc-container
Swinject
Dependency injection framework for Swift with iOS/macOS/Linux
Stars: ✭ 4,958 (+413.25%)
Mutual labels:  dependency-injection, ioc-container
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-96.89%)
Mutual labels:  dependency-injection, ioc-container
pythondi
Python lightweight dependency injection library
Stars: ✭ 26 (-97.31%)
Mutual labels:  dependency-injection, ioc-container
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-97.72%)
Mutual labels:  dependency-injection, ioc-container
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (-55.8%)
Mutual labels:  dependency-injection, ioc-container
Autofac
An addictive .NET IoC container
Stars: ✭ 3,713 (+284.37%)
Mutual labels:  dependency-injection, ioc-container
inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (-94.93%)
Mutual labels:  dependency-injection, ioc-container
Di
Dependency Injection and IoC framework for PHP
Stars: ✭ 5 (-99.48%)
Mutual labels:  dependency-injection, ioc-container
SwiftInjection
Dependency Injection framework for Swift
Stars: ✭ 21 (-97.83%)
Mutual labels:  dependency-injection, ioc-container
AzureFunctions.Extensions
This provides some useful extensions for Azure Functions.
Stars: ✭ 81 (-91.61%)
Mutual labels:  dependency-injection, ioc-container
Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (-72.77%)
Mutual labels:  dependency-injection, ioc-container
Oragon.Spring
Spring.NET (spring.core + spring.aop) on .NET Standard 2.0
Stars: ✭ 19 (-98.03%)
Mutual labels:  dependency-injection, ioc-container
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (-96.27%)
Mutual labels:  dependency-injection, ioc-container
ReaLocate
ASP.NET MVC 5 Real Estate Application
Stars: ✭ 18 (-98.14%)
Mutual labels:  dependency-injection, ioc-container
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (-69.25%)
Mutual labels:  dependency-injection, ioc-container
Weaver
Dependency Injection framework for Swift (iOS/macOS/Linux)
Stars: ✭ 546 (-43.48%)
Mutual labels:  dependency-injection, ioc-container

Simple Injector

Gitter NuGet

To get a high level overview of Simple Injector, please visit our website or dive directly into our documentation. And did you know there's a Simple Injector blog?

The goal of Simple Injector is to provide .NET application developers with an easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.

Many of the existing DI libraries have a big complicated legacy API or are new, immature, and lack features often required by large scale development projects. Simple Injector fills this gap by supplying a simple implementation with a carefully selected, but complete set of features that allow you to write highly maintainable applications. Features like decorator registration and container-verification set it apart from the other containers. In the end, you will realize that there only two types of DI Containers—Simple Injector... and the rest.

The following platforms are supported:

  • .NET 4.5 and up.
  • .NET Standard including:
    • Universal Windows Programs.
    • Mono.
    • .NET Core.
    • Xamarin.

Simple Injector is carefully designed to run in partial / medium trust, and it is fast; blazingly fast.

Getting started

The easiest way to get started is by installing the available NuGet packages. Take a look at the Using section in the documentation on learning how to configure and use Simple Injector. Go to the Integration page to find out how to integrate Simple Injector in your favorate application framework. Look at the More Information section to learn more or if you have any questions.

A Quick Example

Dependency Injection

The general idea behind Simple Injector (or any DI library for that matter) is that you design your application around loosely coupled components using the dependency injection pattern while adhering to the Dependency Inversion Principle. Take for instance the following UserController class in the context of an ASP.NET MVC application:

Note: Simple Injector works for many different technologies and not just MVC. Please see the integration for help using Simple Injector with your technology of choice.

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, you can test UserController in isolation. But ease of testing is only one of a number of things that Dependency Injection gives you. It also enables you, for example, to design highly flexible systems that can be completely composed in one specific location (often the startup path) of the application.

Introducing Simple Injector

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);   
    container.Register<UserController>();

    // 3. Optionally verify the container's configuration.
    container.Verify();

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

Tip: If you start with a MVC application, take a look at the ASP.NET MVC integration guide.

The given configuration registers implementations for the IUserRepository and ILogger interfaces. The code snippet shows a few interesting things. First of all, you can map concrete instances (such as SqlUserRepository) to an interface or base type. 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 seconds 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.

Using this configuration, when a UserController is requested, the following object graph is constructed:

new UserController(
    new SqlUserRepository(
        logger),
    logger);

Note that object graphs can become very deep. What you can see is that not only UserController contains dependencies, so does SqlUserRepository. In this case SqlUserRepository itself contains an ILogger dependency. Simple Injector will not only resolve the dependencies of UserController but will instead build a whole tree structure of any level deep for you.

And this is all it takes to start using Simple Injector. Design your classes around the SOLID principles and the Dependency Injection pattern (which is actually the hard part) and configure them during application initialization. Some frameworks (such as ASP.NET MVC) will do the rest for you, other frameworks (like ASP.NET Web Forms) will need a little bit more work. See the integration guide for examples of many common application frameworks.

Please go to the using section in the documentation to see more examples.

More information

For more information about Simple Injector please visit the following links:

Happy injecting!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].