Simplify.DI
4.2.9
See the version list below for details.
dotnet add package Simplify.DI --version 4.2.9
NuGet\Install-Package Simplify.DI -Version 4.2.9
<PackageReference Include="Simplify.DI" Version="4.2.9" />
paket add Simplify.DI --version 4.2.9
#r "nuget: Simplify.DI, 4.2.9"
// Install Simplify.DI as a Cake Addin #addin nuget:?package=Simplify.DI&version=4.2.9 // Install Simplify.DI as a Cake Tool #tool nuget:?package=Simplify.DI&version=4.2.9
Simplify.DI Documentation
Decouples users and frameworks (that are based on Simplify.DI) from dependency on IOC containers. Instead of that, they will only depend on Simplify.DI interface.
Disciplines and unifies dependencies registration, verification and objects creation.
Provides DIContainer.Current
ambient context as centralized IOC container.
Provides unified Interface for creation IOC container providers (wrappers) for IOC container frameworks.
Using DIContainer.Current
and respective container provider you can switch between any IOC container without rewriting your code.
Also as a centralized IOC container you can use it for building, for example, some framework which needs to use constructor injection and at the same time use it by that framework user (example using it in framework: registration, resolving with lifetime scope).
Available at NuGet as binary package
By default DIContainer.Current
initialized with DryIocDIProvider
container provider (DryIOC).
Another container providers available:
- Simplify.DI.Provider.SimpleInjector (SimpleInjector), Available at NuGet as binary package
- Simplify.DI.Provider.CastleWindsor (CastleWindsor), Available at NuGet as binary package
- Simplify.DI.Provider.Microsoft.Extensions.DependencyInjection (Dependency injection in ASP.NET Core), Available at NuGet as binary package
- Simplify.DI.Provider.Autofac (Autofac), Available at NuGet as binary package
Please create an issue, if you want another container provider.
Main IOC container interface is IDIContainerProvider
Examples
Entities
public interface IFoo
{
}
public class Foo : IFoo
{
public Foo(IBar bar)
{
}
}
public class Foo2 : IFoo
{
public Foo2(IBar bar, string someParameter)
{
}
}
public interface IBar
{
}
public class Bar : IBar
{
}
public class Bar2 : IBar
{
public Bar2(string someParameter)
{
}
}
Registration
// Simple registration
DIContainer.Current.Register<IBar, Bar>()
.Register<IFoo, Foo>();
// Registration with delegate
DIContainer.Current.Register<IBar>(p => new Bar2("test"));
// Registration with delegate and type resolve
DIContainer.Current.Register<IFoo>(p => new Foo2(p.Resolve<IBar>(), "test"));
Resolve
var myObj = DIContainer.Current.Resolve<IBar>();
Verification
Container verification is a process of building registered objects graph and checking what all required types are present in a container and there is no registrations lifetime mismatches
DIContainer.Current.Verify();
In case of any container mismatches an exception will be thrown, exact type of the exception and messages will depend on current container provider
Scopes
3 type of scopes available:
- PerLifetimeScope (default scope) - only one instance of the same type will be created inside opened scope
- Singleton - only one instance will be created per container
- Transient - new instance will be created on every Resolve request
Using PerLifetimeScope scope
Lifetime scope registration
// Simple registration
DIContainer.Current.Register<IBar, Bar>(LifetimeType.PerLifetimeScope);
// Any dependency while registration with delegate should be resolved with delegate parameter `p`, it is current scope resolve provider.
DIContainer.Current.Register<IFoo>(p => new Foo2(p.Resolve<IBar>(), "Test"), LifetimeType.PerLifetimeScope);
Lifetime scope usage
// Note, what scope.Container actually it is the same container as DIContainer.Current, for example, if using SimpleInjector, but with DryIoc it will be child container.
using (var scope = DIContainer.Current.BeginLifetimeScope())
{
var myObject = scope.Resolver.Resolve<IFoo>();
}
Registration specific to IOC container
You can use any IOC container to directly register types or perform specific actions, for example, with Simple Injector:
With direct container registration
var provider = new SimpleInjectorDIProvider();
DIContainer.Current = provider;
provider.Container.RegisterSingleton<IFoo>();
...
Standard registrations with SimpleInjector container verification example
var provider = new SimpleInjectorDIProvider();
DIContainer.Current = provider;
...
DIContainer.Current.Register<IBar, Bar>();
...
provider.Container.Verify();
Fluent interfaces example
public static class Program
{
public static void Main(string[] args)
{
var provider = new DryIocDIProvider();
provider.RegisterAll()
.Verify();
using var scope = provider.BeginLifetimeScope();
scope.Resolver.Resolve<IMyService1>().DoWork();
}
private static IDIContainerProvider RegisterAll(this IDIContainerProvider provider)
{
provider.RegisterServicesGroup1()
.RegisterServicesGroup2();
return provider;
}
private static IDIRegistrator RegisterServicesGroup1(this IDIRegistrator registrator) =>
registrator.Register<IMyService1, MyService1>()
.Register<IMyService2>(r => new MyService2("string parameter example"))
.Register<IMyService3>(r => new MyService3(r.Resolve<IMyService2>()));
private static IDIRegistrator RegisterServicesGroup2(this IDIRegistrator registrator) =>
registrator.Register<IMySettings, MySettings>(LifetimeType.Singleton)
.Register<IUsersRepository, UsersRepository>()
.Register<IGroupsRepository, GroupsRepository>();
}
Special integrations
How to replace 'Simplify.DI.Provider.Microsoft.Extensions.DependencyInjection' internal container with container from AspNetCore application (This allows us to perform registrations via Simplify.DI keeping original container in AspNetCore application)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net452 is compatible. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.5.2
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (13)
Showing the top 5 NuGet packages that depend on Simplify.DI:
Package | Downloads |
---|---|
Simplify.Web
Lightweight and fast .NET web-framework based on MVC and OWIN |
|
AcspNet
ACSP.NET is a lightweight and fast .NET web-framework based on MVC and OWIN. |
|
Simplify.WindowsServices
Windows services framework with DI |
|
Simplify.DI.Provider.SimpleInjector
Simplify.DI SimpleInjector provider |
|
Simplify.Repository.FluentNHibernate
Simplify.Repository FluentNHibernate implementation |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.2.11 | 653 | 6/9/2024 |
4.2.10 | 4,060 | 7/31/2023 |
4.2.9 | 568 | 7/3/2023 |
4.2.8 | 1,919 | 11/30/2022 |
4.2.7 | 447 | 11/11/2022 |
4.2.6 | 703 | 8/29/2022 |
4.2.5 | 516 | 8/21/2022 |
4.2.4 | 527 | 8/6/2022 |
4.2.3 | 2,634 | 7/15/2022 |
4.2.2 | 1,449 | 5/23/2022 |
4.2.1 | 4,425 | 5/11/2022 |
4.2.0 | 2,031 | 4/23/2022 |
4.1.7 | 2,705 | 3/27/2022 |
4.1.6 | 554 | 3/6/2022 |
4.1.5 | 1,899 | 1/10/2022 |
4.1.4 | 386 | 1/3/2022 |
4.1.3 | 4,001 | 11/3/2021 |
4.1.2 | 1,049 | 10/25/2021 |
4.1.1 | 842 | 10/15/2021 |
4.1.0 | 2,164 | 7/15/2021 |
4.0.20 | 881 | 7/5/2021 |
4.0.19 | 593 | 6/10/2021 |
4.0.18 | 565 | 5/29/2021 |
4.0.17 | 1,198 | 5/13/2021 |
4.0.16 | 1,851 | 4/22/2021 |
4.0.15 | 700 | 4/8/2021 |
4.0.15-pre01 | 297 | 4/8/2021 |
4.0.14 | 3,458 | 2/26/2021 |
4.0.13 | 815 | 2/12/2021 |
4.0.12 | 1,374 | 1/18/2021 |
4.0.11 | 480 | 1/13/2021 |
4.0.10 | 1,523 | 12/11/2020 |
4.0.9 | 808 | 12/10/2020 |
4.0.8 | 989 | 10/29/2020 |
4.0.7 | 2,216 | 8/26/2020 |
4.0.6 | 850 | 7/21/2020 |
4.0.5 | 740 | 7/7/2020 |
4.0.4 | 1,710 | 6/27/2020 |
4.0.3 | 2,222 | 4/28/2020 |
4.0.2 | 1,160 | 3/31/2020 |
4.0.1 | 1,798 | 1/26/2020 |
4.0.0 | 6,354 | 9/24/2019 |
3.0.0 | 2,072 | 8/30/2019 |
2.1.0 | 2,370 | 6/22/2019 |
2.0.1 | 2,684 | 11/14/2018 |
1.2.2 | 1,915 | 3/10/2018 |
1.2.1 | 3,134 | 8/14/2017 |
1.2.0 | 1,902 | 1/23/2017 |
1.1.2 | 5,777 | 8/18/2016 |
1.1.1 | 2,392 | 1/10/2016 |
1.1.0 | 1,792 | 11/23/2015 |
1.0.4 | 3,390 | 11/12/2014 |
1.0.3 | 7,563 | 10/23/2014 |
1.0.2 | 3,211 | 9/14/2014 |
1.0.1 | 2,819 | 8/19/2014 |
1.0.0 | 1,963 | 8/14/2014 |