Skip to content

bogdan-mandryk/Singularity

 
 

Repository files navigation

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

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 Beerpay
paypal Beerpay

Licensing

Licensed under LGPL.

About

A extremely fast ioc container for high performance applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.8%
  • Other 1.2%