Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

simpleinjector/SimpleInjector

Repository files navigation

NuGet

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

The goal ofSimple Injector is to provide .NET application developers with an easy, flexible, and fastDependency 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, .NET 5 and up.
    • Xamarin.

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

Getting started

The easiest way to get started is by installingthe available NuGet packages. Take a look at theUsing section in the documentation on learning how to configure and use Simple Injector. Go to theIntegration page to find out how to integrate Simple Injector in your favorate application framework. Look at theMore 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 thedependency injection pattern while adhering to theDependency Inversion Principle. Take for instance the followingUserController class in the context of an ASP.NET MVC application:

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

publicclassUserController:Controller{privatereadonlyIUserRepositoryrepository;privatereadonlyILoggerlogger;// Use constructor injection for the dependenciespublicUserController(IUserRepositoryrepository,ILoggerlogger){this.repository=repository;this.logger=logger;}// implement UserController methods here:publicActionResultIndex(){this.logger.Log("Index called");returnView(this.repository.GetAll());}}publicclassSqlUserRepository:IUserRepository{privatereadonlyILoggerlogger;// Use constructor injection for the dependenciespublicSqlUserRepository(ILoggerlogger){this.logger=logger;}publicUserGetById(Guidid){this.logger.Log("Getting User "+id);// retrieve from db.}}

TheUserController class depends on theIUserRepository andILogger interfaces. By not depending on concrete implementations, you can testUserController 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 theUserController andSqlUserRepository classes shown above, might look something like this:

protectedvoidApplication_Start(objectsender,EventArgse){// 1. Create a new Simple Injector containervarcontainer=newContainer();// 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(newSimpleInjectorDependencyResolver(container));}

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

The given configuration registers implementations for theIUserRepository andILogger interfaces. The code snippet shows a few interesting things. First of all, you can map concrete instances (such asSqlUserRepository) to an interface or base type. In the given example, every time you ask the container for anIUserRepository, it will always create a newSqlUserRepository on your behalf (in DI terminology: an object with aTransient lifestyle).

The seconds registration maps theILogger interface to aMailLogger implementation. ThisMailLogger is registered with theSingleton lifestyle—only one instance ofMailLogger will ever be created by theContainer.

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

newUserController(newSqlUserRepository(logger),logger);

Note that object graphs can become very deep. What you can see is that not onlyUserController contains dependencies, so doesSqlUserRepository. In this caseSqlUserRepository itself contains anILogger dependency. Simple Injector will not only resolve the dependencies ofUserController 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 theintegration guide for examples of many common application frameworks.

Please go to theusing section in thedocumentation to see more examples.

More information

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

Happy injecting!

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp