Dependency Management

October 3, 2022 ยท View on GitHub

Version 1.4.3 C# 9+ Dot NET 6, 7 Nullable Generator

NuGet

Dependency Management

The Dependency Management is a very fast dependency injection and components container, with many interesting features, and without reflection.

Its allows you to make your project more extensible and modular.

Features

The project contains many extensions (eg - modularity) and nuances that are not described here, read the documentation.

Example

This example is very primitive and does not allow you to take into all the features of the project.

Dependency.cs

public sealed class Dependency { }

Service.cs

public sealed class Service
{
    private readonly Dependency _dependency;

    public Service(Dependency dependency)
    {
        _dependency = dependency;
    }
}

With Code Generator

Program.cs

await using var container = new Container()
    .WithStrategies()
    .WithProviders();

container.SetTarget<Dependency>().ToSingleton();

container.SetTarget<Service>().ToTransient();

var readOnlyContainer = container.ToReadOnly();

var service = readOnlyContainer.LastInstance<Service>();

Without Code Generator

Program.cs

await using var container = new Container().WithStrategies();

container.SetTarget<Dependency>()
    .With(_ => new())
    .ToSingleton();

container.SetTarget<Service>()
    .With(c => new(c.LastInstance<Dependency>()))
    .ToTransient();

var readOnlyContainer = container.ToReadOnly();

var service = readOnlyContainer.LastInstance<Service>();

Installation

<PropertyGroup>
    <Nullable>enable</Nullable>
  
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Essy.DependencyManagement.Injection" Version="1.4.3"/>
    <PackageReference Include="Essy.DependencyManagement.Injection.Generator" Version="1.4.3" 
                      OutputItemType="Analyzer" 
                      ReferenceOutputAssembly="false"/>
    
    <PackageReference Include="Essy.DependencyManagement.Modularity" Version="1.4.3"/>
    <PackageReference Include="Essy.DependencyManagement.Modularity.Generator" Version="1.4.3" 
                      OutputItemType="Analyzer" 
                      ReferenceOutputAssembly="false"/>
</ItemGroup>

Benchmarks

The Dependency Management contains benchmarks to conduct measured performance comparison between this and the following dependency injection frameworks:

  • Microsof Dependency Injection;
  • Autofac;
  • Ninject.
BenchmarkDotNet=v0.13.2, OS=ubuntu 20.04
Intel Xeon Platinum 8272CL CPU 2.60GHz, 1 CPU, 2 logical and 2 physical cores
.NET SDK=7.0.100-rc.1.22431.12

Initialize

MethodRuntimeMeanErrorStdDevMedianRatioRatioSDAllocatedAlloc Ratio
'Dependency Management - Initialize'.NET 7.037.63 us18.63 us54.92 us30.90 us1.000.005.76 KB1.00
'Microsoft Dependency Injection - Initialize'.NET 7.027.91 us18.62 us54.89 us22.10 us0.710.073.9 KB0.68
'Autofac - Initialize'.NET 7.0126.47 us46.97 us138.50 us107.60 us3.530.3519.07 KB3.31
'Dependency Management - Initialize'NativeAOT 7.090.14 us172.88 us509.74 us32.60 us1.000.005.76 KB1.00
'Microsoft Dependency Injection - Initialize'NativeAOT 7.029.11 us19.79 us58.35 us22.50 us0.690.114.27 KB0.74
'Autofac - Initialize'NativeAOT 7.0128.21 us45.56 us134.35 us107.80 us3.370.4419.07 KB3.31

Transient

MethodRuntimeMeanErrorStdDevMedianRatioRatioSD
'Dependency Management - Transient'.NET 7.01.099 us0.4995 us0.3304 us0.9550 us1.000.00
'Microsoft Dependency Injection - Transient'.NET 7.02.143 us6.8938 us4.5598 us0.7050 us1.482.45
'Autofac - Transient'.NET 7.02.630 us0.9855 us0.6518 us2.4101 us2.460.36
'Dependency Management - Transient'NativeAOT 7.01.168 us0.5282 us0.3494 us1.0050 us1.000.00
'Microsoft Dependency Injection - Transient'NativeAOT 7.02.165 us7.1158 us4.7067 us0.6850 us1.392.37
'Autofac - Transient'NativeAOT 7.02.698 us1.0792 us0.7138 us2.4651 us2.360.35

Singleton

MethodRuntimeMeanErrorStdDevRatioRatioSD
'Dependency Management - Singleton'.NET 7.01.863 us0.0601 us0.0953 us1.000.00
'Microsoft Dependency Injection - Singleton'.NET 7.01.270 us0.0516 us0.0818 us0.680.03
'Autofac - Singleton'.NET 7.04.921 us0.3616 us0.5735 us2.640.20
'Dependency Management - Singleton'NativeAOT 7.01.974 us0.0782 us0.1241 us1.000.00
'Microsoft Dependency Injection - Singleton'NativeAOT 7.01.384 us0.0539 us0.0854 us0.700.05
'Autofac - Singleton'NativeAOT 7.04.963 us0.3303 us0.5238 us2.510.16

Dispose

MethodRuntimeMeanErrorStdDevMedianRatioRatioSDAllocatedAlloc Ratio
'Dependency Management - Dispose'.NET 7.015.30 us48.65 us143.45 us0.7000 us1.000.00816 B1.00
'Microsoft Dependency Injection - Dispose'.NET 7.014.60 us44.34 us130.74 us1.5000 us2.220.40432 B0.53
'Autofac - Dispose'.NET 7.035.35 us116.97 us344.88 us0.7000 us1.010.19816 B1.00
'Dependency Management - Dispose'NativeAOT 7.016.16 us51.71 us152.48 us0.6000 us1.000.00816 B1.00
'Microsoft Dependency Injection - Dispose'NativeAOT 7.013.41 us40.17 us118.44 us1.6000 us2.420.41816 B1.00
'Autofac - Dispose'NativeAOT 7.036.74 us121.84 us359.24 us0.6000 us1.010.20816 B1.00

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

Dependency Management is licensed under MIT license.