Singularity
Features
- Extreme 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.
- Clean fluent API.
- Source Link enabled
- Generic wrappers:
Func<T>
Lazy<T>
Expression<Func<T>>
- And any other generic wrapper you may have defined yourself.
- Collection support:
IEnumerable<T>
IReadOnlyCollection<T>
IReadOnlyList
- Supports open generics.
- Supports resolving unregistered concrete types.
- Supports decorators.
- Supports method and property injection without forcing you to litter attributes all over your code base. All configuration is kept inside the container.
- Auto dispose, this is off by default but can be turned on with
With(DisposeBehavior)
or adding the lifetimes you want to auto dispose toSingularitySettings.AutoDisposeLifetimes
. - Custom finalizers with the
WithFinalizer(Action<TInstance>)
method. - Supports Transient, Singleton and Scope lifetimes.
- Supports child containers.
- Clear error messages and fail fast to point you in the right direction as fast as possible.
Getting started
Installation
Singularity
can be installed through nuget:
A simple example
Its easy to setup a container and request a instance:
var container = new Container(builder =>
{
builder.Register<ITestService10, TestService10>();
});
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
The code used in the benchmark can be found here Licensed under AGPL however this might not be fit for all commercial works as it requires you to open source your work. If you require a different license please contact me.var instanceExpressions = container.GetInstance<IReadOnlyList<Expression<Func<IPlugin>>>>(); //Returns all expressions for IPlugin registrations
Benchmarks
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 |
Donations
Licensing