Singularity is a ioc container that focuses on the following things
- Performance, Singularity is one of the fastest if not the fastest dependency injection container out there. Don't believe me? Check out my benchmarks or if you want a second opinion check out the benchmarks that Daniel Palme made here.
- Not invasive. For instance
Dispose
wont be automatically called by default, instead you can configure Singularity to do so through theWith(DisposeBehavior)
method or tell singularity to automatically callDispose
by simply changing the settings. Is calling Dispose not enough and you want to invoke your own custom logic? TheWithFinalizer(Action<TInstance>)
method might be of help here. - Clear error messages and fail fast to point you in the right direction as fast as possible.
Singularity
can be installed through nuget. The packages that are available can be found in the nuget section
Its easy to setup a container and request a instance:
var config = new BindingConfig();
config.Register<ITestService10>, TestService10>();
var container = new Container(config);
var instance = container.GetInstance<ITestService10>();
However Singularity
can do much more than this simple example. You can request the instance with different wrapper types such as Lazy<T>
:
var lazyInstance = container.GetInstance<Lazy<ITestService10>>();
Or you can request the factory to create the instance:
var factory = container.GetInstance<Func<ITestService10>>();
You can even request the expression itself:
var instanceExpression = container.GetInstance<Expression<Func<ITestService10>>>();
Ofcourse its possible to combine these with for instance a collection type such as IEnumerable or IReadOnlyList:
var instanceExpressions = container.GetInstance<IReadOnlyList<Expression<Func<IPlugin>>>>(); //Returns all expressions for IPlugin registrations
Advanced scenarios such as open generics are also supported.
More info about Singularity
can be found on the documentation website which can be found here.
The code used in the benchmark can be found here
BenchmarkDotNet=v0.11.4, OS=Windows 10.0.17763.437 (1809/October2018Update/Redstone5)
Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100-preview3-010431
[Host] : .NET Core 2.1.9 (CoreCLR 4.6.27414.06, CoreFX 4.6.27415.01), 64bit RyuJIT
LegacyJitX64 : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3324.0
RyuJitX64 : .NET Core 2.1.9 (CoreCLR 4.6.27414.06, CoreFX 4.6.27415.01), 64bit RyuJIT
Platform=X64 IterationTime=500.0000 ms
| Method | Job | Jit | Runtime | Mean | Error | StdDev | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
|---------- |------------- |---------- |-------- |----------:|----------:|----------:|------------:|------------:|------------:|--------------------:|
| Singleton | LegacyJitX64 | LegacyJit | Clr | 7.700 ns | 0.0722 ns | 0.0675 ns | - | - | - | - |
| Transient | LegacyJitX64 | LegacyJit | Clr | 10.086 ns | 0.0220 ns | 0.0195 ns | 0.0057 | - | - | 24 B |
| Combined | LegacyJitX64 | LegacyJit | Clr | 15.067 ns | 0.0568 ns | 0.0532 ns | 0.0133 | - | - | 56 B |
| Complex | LegacyJitX64 | LegacyJit | Clr | 26.822 ns | 0.1198 ns | 0.1121 ns | 0.0229 | - | - | 96 B |
| Singleton | RyuJitX64 | RyuJit | Core | 7.744 ns | 0.0743 ns | 0.0695 ns | - | - | - | - |
| Transient | RyuJitX64 | RyuJit | Core | 10.031 ns | 0.0172 ns | 0.0161 ns | 0.0057 | - | - | 24 B |
| Combined | RyuJitX64 | RyuJit | Core | 16.454 ns | 0.0564 ns | 0.0500 ns | 0.0133 | - | - | 56 B |
| Complex | RyuJitX64 | RyuJit | Core | 23.667 ns | 0.0410 ns | 0.0342 ns | 0.0229 | - | - | 96 B |
Library | Version |
---|---|
Singularity | |
Singularity.Duality.core | |
Singularity.Microsoft.DependencyInjection |