Awesome Open Source
Awesome Open Source

Singularity

Discord NuGet Badge Build Status Azure DevOps tests (branch) Maintainability Rating Reliability Rating Security Rating coverage Beerpay

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:
    1. Func<T>
    2. Lazy<T>
    3. Expression<Func<T>>
    4. And any other generic wrapper you may have defined yourself.
  • Collection support:
    1. IEnumerable<T>
    2. IReadOnlyCollection<T>
    3. IReadOnlyList<T>
    4. T[]
    5. List<T>
    6. ICollection<T>
    7. IList<T>
    8. HashSet<T>
    9. ISet<T>
  • 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.
  • Supports dynamically picking the most suitable constructor based on the available types that can be resolved.
  • Auto dispose, this is off by default but can be turned on with With(DisposeBehavior)or adding the lifetimes you want to auto dispose to SingularitySettings.AutoDisposeLifetimes.
  • Custom finalizers with the WithFinalizer(Action<TInstance>) method.
  • Supports Transient, Singleton and Scope lifetimes.
  • Supports child containers.
  • Supports best fit constructor selection
  • 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. The packages that are available can be found in the nuget section

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 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.

Documentation

More info about Singularity can be found on the documentation website which can be found here.

Other

Benchmarks

The code used in the benchmark can be found here

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores
.NET Core SDK=3.1.200
  [Host]       : .NET Core 3.1.2 (CoreCLR 4.700.20.6602, CoreFX 4.700.20.6702), X64 RyuJIT
  LegacyJitX64 : .NET Framework 4.8 (4.8.4150.0), X64 RyuJIT
  RyuJitX64    : .NET Core 2.1.16 (CoreCLR 4.6.28516.03, CoreFX 4.6.28516.10), X64 RyuJIT

Platform=X64  IterationTime=1.0000 s

|    Method |          Job |       Jit |       Runtime |          Mean |       Error |      StdDev |  Gen 0 |  Gen 1 | Gen 2 | Allocated |
|---------- |------------- |---------- |-------------- |--------------:|------------:|------------:|-------:|-------:|------:|----------:|
| Singleton | LegacyJitX64 | LegacyJit |    .NET 4.7.2 |     10.539 ns |   0.1156 ns |   0.1082 ns |      - |      - |     - |         - |
| Transient | LegacyJitX64 | LegacyJit |    .NET 4.7.2 |     15.095 ns |   0.0562 ns |   0.0525 ns | 0.0145 |      - |     - |      24 B |
|  Combined | LegacyJitX64 | LegacyJit |    .NET 4.7.2 |     20.722 ns |   0.0707 ns |   0.0552 ns | 0.0338 |      - |     - |      56 B |
|   Complex | LegacyJitX64 | LegacyJit |    .NET 4.7.2 |     27.218 ns |   0.2894 ns |   0.2566 ns | 0.0580 |      - |     - |      96 B |
| Singleton |    RyuJitX64 |    RyuJit | .NET Core 2.1 |      9.569 ns |   0.0634 ns |   0.0593 ns |      - |      - |     - |         - |
| Transient |    RyuJitX64 |    RyuJit | .NET Core 2.1 |     14.755 ns |   0.1025 ns |   0.0959 ns | 0.0145 |      - |     - |      24 B |
|  Combined |    RyuJitX64 |    RyuJit | .NET Core 2.1 |     22.506 ns |   0.3656 ns |   0.3420 ns | 0.0337 |      - |     - |      56 B |
|   Complex |    RyuJitX64 |    RyuJit | .NET Core 2.1 |     26.756 ns |   0.3521 ns |   0.3294 ns | 0.0578 |      - |     - |      96 B |
| Singleton |    RyuJitX64 |    RyuJit | .NET Core 3.1 |     12.167 ns |   0.1213 ns |   0.1135 ns |      - |      - |     - |         - |
| Transient |    RyuJitX64 |    RyuJit | .NET Core 3.1 |     14.461 ns |   0.0698 ns |   0.0653 ns | 0.0029 |      - |     - |      24 B |
|  Combined |    RyuJitX64 |    RyuJit | .NET Core 3.1 |     20.933 ns |   0.2406 ns |   0.2251 ns | 0.0067 |      - |     - |      56 B |
|   Complex |    RyuJitX64 |    RyuJit | .NET Core 3.1 |     24.196 ns |   0.1059 ns |   0.0991 ns | 0.0115 |      - |     - |      96 B |

Nuget

Library Version
Singularity NuGet Badge
Singularity.Duality.core NuGet Badge
Singularity.Microsoft.DependencyInjection NuGet Badge
Singularity.AspNetCore.Hosting NuGet Badge
Singularity.AspNetCore.MVC NuGet Badge

Random info

GitHub repo size GitHub code size in bytes Lines of Code Duplicated Lines (%)

Build History

Donations

Paypal
paypal

Licensing

Licensed under LGPL.


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
c-sharp (12,316
csharp (1,071
dotnet (887
dependency-injection (219
netcore (156
netstandard (101
ioc (95
net (67
ioc-container (34
inversion-of-control (21
di (20
singularity (17