Pure.DI is not a framework or library, but a source code generator for creating object graphs. To make them accurate, the developer uses a set of intuitive hints from the Pure.DI API. During the compilation phase, Pure.DI determines the optimal graph structure, checks its correctness, and generates partial class code to create object graphs in the Pure DI paradigm using only basic language constructs. The resulting generated code is robust, works everywhere, throws no exceptions, does not depend on .NET library calls or .NET reflections, is efficient in terms of performance and memory consumption, and is subject to all optimizations. This code can be easily integrated into an application because it does not use unnecessary delegates, additional calls to any methods, type conversions, boxing/unboxing, etc.
- DI without any IoC/DI containers, frameworks, dependencies and hence no performance impact or side effects.
Pure.DI is actually a .NET code generator. It uses basic language constructs to create simple code as well as if you were doing it yourself: de facto it's just a bunch of nested constructor calls. This code can be viewed, analyzed at any time, and debugged.
- A predictable and verified dependency graph is built and validated on the fly while writing code.
All logic for analyzing the graph of objects, constructors and methods takes place at compile time. Pure.DI notifies the developer at compile time about missing or ring dependencies, cases when some dependencies are not suitable for injection, etc. The developer has no chance to get a program that will crash at runtime because of some exception related to incorrect object graph construction. All this magic happens at the same time as the code is written, so you have instant feedback between the fact that you have made changes to your code and the fact that your code is already tested and ready to use.
- Does not add any dependencies to other assemblies.
When using pure DI, no dependencies are added to assemblies because only basic language constructs and nothing more are used.
- Highest performance, including compiler and JIT optimization and minimal memory consumption.
All generated code runs as fast as your own, in pure DI style, including compile-time and run-time optimization. As mentioned above, graph analysis is done at compile time, and at runtime there are only a bunch of nested constructors, and that's it. Memory is spent only on the object graph being created.
- It works everywhere.
Since the pure DI approach does not use any dependencies or .NET reflection at runtime, it does not prevent the code from running as expected on any platform: Full .NET Framework 2.0+, .NET Core, .NET, UWP/XBOX, .NET IoT, Xamarin, Native AOT, etc.
- Ease of Use.
The Pure.DI API is very similar to the API of most IoC/DI libraries. And this was a conscious decision: the main reason is that programmers don't need to learn a new API.
- Superfine customization of generic types.
In Pure.DI it is proposed to use special marker types instead of using open generic types. This allows you to build the object graph more accurately and take full advantage of generic types.
- Supports the major .NET BCL types out of the box.
Pure.DI already supports many of BCL types like
Array
,IEnumerable<T>
,IList<T>
,IReadOnlyCollection<T>
,IReadOnlyList<T>
,ISet<T>
,IProducerConsumerCollection<T>
,ConcurrentBag<T>
,Func<T>
,ThreadLocal
,ValueTask<T>
,Task<T>
,MemoryPool<T>
,ArrayPool<T>
,ReadOnlyMemory<T>
,Memory<T>
,ReadOnlySpan<T>
,Span<T>
,IComparer<T>
,IEqualityComparer<T>
and etc. without any extra effort. - Good for building libraries or frameworks where resource consumption is particularly critical.
Its high performance, zero memory consumption/preparation overhead, and lack of dependencies make it ideal for building libraries and frameworks.
interface IBox<out T>
{
T Content { get; }
}
interface ICat
{
State State { get; }
}
enum State
{
Alive,
Dead
}
class CardboardBox<T>(T content) : IBox<T>
{
public T Content { get; } = content;
public override string ToString() => $"[{Content}]";
}
class ShroedingersCat(Lazy<State> superposition) : ICat
{
// The decoherence of the superposition
// at the time of observation via an irreversible process
public State State => superposition.Value;
public override string ToString() => $"{State} cat";
}
It is important to note that our abstraction and implementation knows nothing about the magic of DI or any frameworks.
Add the Pure.DI package to your project:
Let's bind the abstractions to their implementations and set up the creation of the object graph:
DI.Setup(nameof(Composition))
// Models a random subatomic event that may or may not occur
.Bind().As(Singleton).To<Random>()
// Represents a quantum superposition of 2 states: Alive or Dead
.Bind().To(ctx =>
{
ctx.Inject<Random>(out var random);
return (State)random.Next(2);
})
.Bind().To<ShroedingersCat>()
// Represents a cardboard box with any contents
.Bind().To<CardboardBox<TT>>()
// Composition Root
.Root<Program>("Root");
The above code specifies the generation of a partial class named Composition, this name is defined in the DI.Setup(nameof(Composition))
call. This class contains a Root property that returns a graph of objects with an object of type Program as the root. The type and name of the property is defined by calling Root<Program>("Root")
. The code of the generated class looks as follows:
partial class Composition
{
private object _lock = new object();
private Random _random;
public Program Root
{
get
{
Func<State> stateFunc = new Func<State>(() =>
{
if (_random == null)
{
lock (_lock)
{
if (_random == null)
{
_random = new Random();
}
}
}
return (State)_random.Next(2);
});
return new Program(
new CardboardBox<ICat>(
new ShroedingersCat(
new Lazy<Sample.State>(
stateFunc))));
}
}
public T Resolve<T>() { ... }
public object Resolve(Type type) { ... }
}
The public Program Root { get; }
property here is a Composition Root, the only place in the application where the composition of the object graph for the application takes place. Each instance is created by only basic language constructs, which compiles with all optimizations with minimal impact on performance and memory consumption. In general, applications may have multiple composition roots and thus such properties. Each composition root must have its own unique name, which is defined when the Root<T>(string name)
method is called, as shown in the above code.
class Program(IBox<ICat> box)
{
// Composition Root, a single place in an application
// where the composition of the object graphs for an application take place
static void Main() => new Composition().Root.Run();
private void Run() => Console.WriteLine(box);
}
The full analog of this application with top-level statements can be found here.
Pure.DI creates efficient code in a pure DI paradigm, using only basic language constructs as if you were writing code by hand. This allows you to take full advantage of Dependency Injection everywhere and always, without any compromise!
Just try!
Download a sample project
git clone https://github.com/DevTeam/Pure.DI.Example.git
And run it from solution root folder
cd ./Pure.DI.Example
dotnet run
- Auto-bindings
- Injections of abstractions
- Composition roots
- Composition roots simplified
- Resolve methods
- Simplified binding
- Factory
- Injection
- Arguments
- Root arguments
- Tags
- Multi-contract bindings
- Field injection
- Method injection
- Property injection
- Default values
- Required properties or fields
- Tracking disposable instances in delegates
- Tracking disposable instances per a composition root
- RootBind
- Func
- Enumerable
- Enumerable generics
- Array
- Lazy
- Task
- ValueTask
- Manually started tasks
- Span and ReadOnlySpan
- Tuple
- Weak Reference
- Async Enumerable
- Service collection
- Func with arguments
- Func with tag
- Keyed service provider
- Service provider
- Service provider with scope
- Overriding the BCL binding
- Constructor ordinal attribute
- Member ordinal attribute
- Tag attribute
- Type attribute
- Custom attributes
- Resolve hint
- ThreadSafe hint
- OnDependencyInjection hint
- OnCannotResolve hint
- OnNewInstance hint
- ToString hint
- Check for a root
- Composition root kinds
- Instance Initialization
- Tag Type
- Tag Unique
- A few partial classes
- Partial class
- Dependent compositions
- Accumulators
- Global compositions
- Console
- UI
- Web
- Git repo with examples
Each generated class, hereafter called a composition, must be customized. Setup starts with a call to the Setup(string compositionTypeName)
method:
DI.Setup("Composition")
.Bind<IDependency>().To<Dependency>()
.Bind<IService>().To<Service>()
.Root<IService>("Root");
The following class will be generated
partial class Composition
{
// Default constructor
public Composition() { }
// Scope constructor
internal Composition(Composition baseComposition) { }
// Composition root
public IService Root
{
get
{
return new Service(new Dependency());
}
}
public T Resolve<T>() { ... }
public T Resolve<T>(object? tag) { ... }
public object Resolve(Type type) { ... }
public object Resolve(Type type, object? tag) { ... }
}
Setup arguments
The first parameter is used to specify the name of the composition class. All sets with the same name will be combined to create one composition class. Alternatively, this name may contain a namespace, e.g. a composition class is generated for Sample.Composition
:
namespace Sample
{
partial class Composition
{
...
}
}
The second optional parameter may have multiple values to determine the kind of composition.
This value is used by default. If this value is specified, a normal composition class will be created.
If you specify this value, the class will not be generated, but this setup can be used by others as a base setup. For example:
DI.Setup("BaseComposition", CompositionKind.Internal)
.Bind<IDependency>().To<Dependency>();
DI.Setup("Composition").DependsOn("BaseComposition")
.Bind<IService>().To<Service>();
If the CompositionKind.Public flag is set in the composition setup, it can also be the base for other compositions, as in the example above.
No composition class will be created when this value is specified, but this setup is the base setup for all setups in the current project, and DependsOn(...)
is not required.
Constructors
It's quite trivial, this constructor simply initializes the internal state.
It replaces the default constructor and is only created if at least one argument is specified. For example:
DI.Setup("Composition")
.Arg<string>("name")
.Arg<int>("id")
...
In this case, the constructor with arguments is as follows:
public Composition(string name, int id) { ... }
and there is no default constructor. It is important to remember that only those arguments that are used in the object graph will appear in the constructor. Arguments that are not involved cannot be defined, as they are omitted from the constructor parameters to save resources.
This constructor creates a composition instance for the new scope. This allows Lifetime.Scoped
to be applied. See this example for details.
Properties
To create an object graph quickly and conveniently, a set of properties (or a methods) is formed. These properties are here called roots of compositions. The type of a property/method is the type of the root object created by the composition. Accordingly, each invocation of a property/method leads to the creation of a composition with a root element of this type.
DI.Setup("Composition")
.Bind<IService>().To<Service>()
.Root<IService>("MyService");
In this case, the property for the IService type will be named MyService and will be available for direct use. The result of its use will be the creation of a composition of objects with the root of IService type:
public IService MyService
{
get
{
...
return new Service(...);
}
}
This is recommended way to create a composition root. A composition class can contain any number of roots.
If the root name is empty, a private composition root with a random name is created:
private IService RootM07D16di_0001
{
get { ... }
}
This root is available in Resolve methods in the same way as public roots. For example:
DI.Setup("Composition")
.Bind<IService>().To<Service>()
.Root<IService>();
These properties have an arbitrary name and access modifier private and cannot be used directly from the code. Do not attempt to use them, as their names are arbitrarily changed. Private composition roots can be resolved by Resolve methods.
Methods
By default, a set of four Resolve methods is generated:
public T Resolve<T>() { ... }
public T Resolve<T>(object? tag) { ... }
public object Resolve(Type type) { ... }
public object Resolve(Type type, object? tag) { ... }
These methods can resolve both public and private composition roots that do not depend on any arguments of the composition roots. They are useful when using the Service Locator approach, where the code resolves composition roots in place:
var composition = new Composition();
composition.Resolve<IService>();
This is a not recommended way to create composition roots. To control the generation of these methods, see the Resolve hint.
Provides a mechanism to release unmanaged resources. This method is generated only if the composition contains at least one singleton instance that implements the IDisposable interface. To dispose of all created singleton objects, the Dispose()
method of the composition should be called:
using var composition = new Composition();
Setup hints
Hints are used to fine-tune code generation. Setup hints can be used as shown in the following example:
DI.Setup("Composition")
.Hint(Hint.Resolve, "Off")
.Hint(Hint.ThreadSafe, "Off")
.Hint(Hint.ToString, "On")
...
In addition, setup hints can be commented out before the Setup method as hint = value
. For example:
// Resolve = Off
// ThreadSafe = Off
DI.Setup("Composition")
.Hint(Hint.ToString, "On")
...
Both approaches can be used in combination with each other.
The list of hints will be gradually expanded to meet the needs and desires for fine-tuning code generation. Please feel free to add your ideas.
Determines whether to generate Resolve methods. By default, a set of four Resolve methods are generated. Set this hint to Off to disable the generation of resolve methods. This will reduce the generation time of the class composition, and in this case no private composition roots will be generated. The class composition will be smaller and will only have public roots. When the Resolve hint is disabled, only the public roots properties are available, so be sure to explicitly define them using the Root<T>(string name)
method with an explicit composition root name.
Determines whether to use the OnNewInstance partial method. By default, this partial method is not generated. This can be useful, for example, for logging purposes:
internal partial class Composition
{
partial void OnNewInstance<T>(ref T value, object? tag, object lifetime)
{
Console.WriteLine($"'{typeof(T)}'('{tag}') created.");
}
}
You can also replace the created instance with a T
type, where T
is the actual type of the created instance. To minimize performance loss when calling OnNewInstance, use the three hints below.
Determines whether to generate the OnNewInstance partial method. By default, this partial method is generated when the OnNewInstance hint is On
.
This is a regular expression for filtering by instance type name. This hint is useful when OnNewInstance is in On state and it is necessary to limit the set of types for which the OnNewInstance method will be called.
This is a regular expression for filtering by tag. This hint is also useful when OnNewInstance is in On state and it is necessary to limit the set of tags for which the OnNewInstance method will be called.
This is a regular expression for filtering by lifetime. This hint is also useful when OnNewInstance is in On state and it is necessary to restrict the set of life times for which the OnNewInstance method will be called.
Determines whether to use the OnDependencyInjection partial method when the OnDependencyInjection hint is On
to control dependency injection. By default it is On
.
// OnDependencyInjection = On
// OnDependencyInjectionPartial = Off
// OnDependencyInjectionContractTypeNameRegularExpression = ICalculator[\d]{1}
// OnDependencyInjectionTagRegularExpression = Abc
DI.Setup("Composition")
...
Determines whether to generate the OnDependencyInjection partial method to control dependency injection. By default, this partial method is not generated. It cannot have an empty body because of the return value. It must be overridden when it is generated. This may be useful, for example, for Interception Scenario.
// OnDependencyInjection = On
// OnDependencyInjectionContractTypeNameRegularExpression = ICalculator[\d]{1}
// OnDependencyInjectionTagRegularExpression = Abc
DI.Setup("Composition")
...
To minimize performance loss when calling OnDependencyInjection, use the three tips below.
This is a regular expression for filtering by instance type name. This hint is useful when OnDependencyInjection is in On state and it is necessary to restrict the set of types for which the OnDependencyInjection method will be called.
This is a regular expression for filtering by the name of the resolving type. This hint is also useful when OnDependencyInjection is in On state and it is necessary to limit the set of permissive types for which the OnDependencyInjection method will be called.
This is a regular expression for filtering by tag. This hint is also useful when OnDependencyInjection is in the On state and you want to limit the set of tags for which the OnDependencyInjection method will be called.
This is a regular expression for filtering by lifetime. This hint is also useful when OnDependencyInjection is in On state and it is necessary to restrict the set of lifetime for which the OnDependencyInjection method will be called.
Determines whether to use the OnCannotResolve<T>(...)
partial method to handle a scenario in which an instance cannot be resolved. By default, this partial method is not generated. Because of the return value, it cannot have an empty body and must be overridden at creation.
// OnCannotResolve = On
// OnCannotResolveContractTypeNameRegularExpression = string|DateTime
// OnDependencyInjectionTagRegularExpression = null
DI.Setup("Composition")
...
To avoid missing failed bindings by mistake, use the two relevant hints below.
Determines whether to generate the OnCannotResolve<T>(...)
partial method when the OnCannotResolve hint is On to handle a scenario in which an instance cannot be resolved. By default it is On
.
// OnCannotResolve = On
// OnCannotResolvePartial = Off
// OnCannotResolveContractTypeNameRegularExpression = string|DateTime
// OnDependencyInjectionTagRegularExpression = null
DI.Setup("Composition")
...
To avoid missing failed bindings by mistake, use the two relevant hints below.
Determines whether to use a static partial method OnNewRoot<TContract, T>(...)
to handle the new composition root registration event.
// OnNewRoot = On
DI.Setup("Composition")
...
Be careful, this hint disables checks for the ability to resolve dependencies!
Determines whether to generate a static partial method OnNewRoot<TContract, T>(...)
when the OnNewRoot hint is On
to handle the new composition root registration event.
// OnNewRootPartial = Off
DI.Setup("Composition")
...
This is a regular expression for filtering by the name of the resolving type. This hint is also useful when OnCannotResolve is in On state and it is necessary to limit the set of resolving types for which the OnCannotResolve method will be called.
This is a regular expression for filtering by tag. This hint is also useful when OnCannotResolve is in On state and it is necessary to limit the set of tags for which the OnCannotResolve method will be called.
This is a regular expression for filtering by lifetime. This hint is also useful when OnCannotResolve is in the On state and it is necessary to restrict the set of lives for which the OnCannotResolve method will be called.
Determines whether to generate the ToString() method. This method provides a class diagram in mermaid format. To see this diagram, just call the ToString method and copy the text to this site.
// ToString = On
DI.Setup("Composition")
.Bind<IService>().To<Service>()
.Root<IService>("MyService");
var composition = new Composition();
string classDiagram = composition.ToString();
This hint determines whether the composition of objects will be created in a thread-safe way. The default value of this hint is On. It is a good practice not to use threads when creating an object graph, in this case the hint can be disabled, which will result in a small performance gain. For example:
// ThreadSafe = Off
DI.Setup("Composition")
.Bind<IService>().To<Service>()
.Root<IService>("MyService");
Overrides the modifiers of the public T Resolve<T>()
method.
Overrides the method name for public T Resolve<T>()
.
Overrides the modifiers of the public T Resolve<T>(object? tag)
method.
Overrides the method name for public T Resolve<T>(object? tag)
.
Overrides the modifiers of the public object Resolve(Type type)
method.
Overrides the method name for public object Resolve(Type type)
.
Overrides the modifiers of the public object Resolve(Type type, object? tag)
method.
Overrides the method name for public object Resolve(Type type, object? tag)
.
Overrides the modifiers of the public void Dispose()
method.
Specifies whether the generated code should be formatted. This option consumes a lot of CPU resources. This hint may be useful when studying the generated code or, for example, when making presentations.
Indicates the severity level of the situation when, in the binding, an implementation does not implement a contract. Possible values:
- "Error", it is default value.
- "Warning" - something suspicious but allowed.
- "Info" - information that does not indicate a problem.
- "Hidden" - what's not a problem.
Specifies whether the generated code should be commented.
// Represents the composition class
DI.Setup(nameof(Composition))
.Bind<IService>().To<Service>()
// Provides a composition root of my service
.Root<IService>("MyService");
Appropriate comments will be added to the generated Composition
class and the documentation for the class, depending on the IDE used, will look something like this:
Then documentation for the composition root:
Pure.DI | DI Source code generator | |
Pure.DI.Templates | Template Package you can call from the shell/command line. | |
Pure.DI.MS | Tools for working with Microsoft DI |
Install the DI template Pure.DI.Templates
dotnet new -i Pure.DI.Templates
Create a "Sample" console application from the template di
dotnet new di -o ./Sample
And run it
dotnet run --project Sample
For more information about the template, please see this page.
Version update
When updating the version, it is possible that the previous version of the code generator remains active and is used by compilation services. In this case, the old and new versions of the generator may conflict. For a project where the code generator is used, it is recommended to do the following:
- After updating the version, close the IDE if it is open
- Delete the obj and bin directories
- Execute the following commands one by one
dotnet build-server shutdown
dotnet restore
dotnet build
Disabling API generation
Pure.DI automatically generates its API. If an assembly already has the Pure.DI API, for example, from another assembly, it is sometimes necessary to disable its automatic generation to avoid ambiguity. To do this, you need to add a DefineConstants element to the project files of these modules. For example:
<PropertyGroup>
<DefineConstants>$(DefineConstants);PUREDI_API_SUPPRESSION</DefineConstants>
</PropertyGroup>
Display generated files
You can set project properties to save generated files and control their storage location. In the project file, add the <EmitCompilerGeneratedFiles>
element to the <PropertyGroup>
group and set its value to true
. Build the project again. The generated files are now created in the obj/Debug/netX.X/generated/Pure.DI/Pure.DI/Pure.DI.SourceGenerator directory. The path components correspond to the build configuration, the target framework, the source generator project name, and the full name of the generator type. You can choose a more convenient output folder by adding the <CompilerGeneratedFilesOutputPath>
element to the application project file. For example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
Thank you for your interest in contributing to the Pure.DI project! First of all, if you are going to make a big change or feature, please open a problem first. That way, we can coordinate and understand if the change you're going to work on fits with current priorities and if we can commit to reviewing and merging it within a reasonable timeframe. We don't want you to waste a lot of your valuable time on something that may not align with what we want for Pure.DI.
This project uses the "build as code" approach using csharp-interactive. The entire build logic is a regular console .NET application. You can use the build.cmd and build.sh files with the appropriate command in the parameters to perform all basic actions on the project, e.g:
Command | Description |
---|---|
g, generator | Builds and tests generator |
l, libs | Builds and tests libraries |
c, check | Compatibility checks |
p, pack | Creates NuGet packages |
r, readme | Generates README.md |
benchmarks, bm | Runs benchmarks |
deploy, dp | Deploys packages |
t, template | Creates and deploys templates |
u, update | Updates internal DI version |
For example:
./build.sh pack
./build.cmd benchmarks
If you are using the Rider IDE, it already has a set of configurations to run these commands.
Installed .NET SDK 8.0
Array
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|
'Hand Coded' | 181.4 ns | 3.65 ns | 6.09 ns | 1.00 | 0.00 | 0.0365 | - | 632 B | 1.00 |
'Pure.DI composition root' | 182.0 ns | 3.57 ns | 7.29 ns | 1.01 | 0.04 | 0.0365 | - | 632 B | 1.00 |
'Pure.DI Resolve<T>()' | 186.1 ns | 3.52 ns | 4.58 ns | 1.03 | 0.04 | 0.0365 | - | 632 B | 1.00 |
'Pure.DI Resolve(Type)' | 189.8 ns | 3.90 ns | 11.05 ns | 1.08 | 0.07 | 0.0365 | - | 632 B | 1.00 |
LightInject | 198.9 ns | 4.06 ns | 8.01 ns | 1.10 | 0.05 | 0.0365 | - | 632 B | 1.00 |
DryIoc | 213.3 ns | 4.35 ns | 10.82 ns | 1.20 | 0.07 | 0.0365 | - | 632 B | 1.00 |
Unity | 10,172.3 ns | 193.16 ns | 180.68 ns | 55.87 | 1.97 | 0.8392 | - | 14520 B | 22.97 |
Autofac | 27,444.9 ns | 318.63 ns | 248.76 ns | 149.08 | 3.87 | 1.6479 | 0.0610 | 28816 B | 45.59 |
Enum
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|
'Pure.DI Resolve<T>()' | 136.3 ns | 2.77 ns | 4.48 ns | 0.88 | 0.03 | 0.0198 | - | 344 B | 1.00 |
'Pure.DI Resolve(Type)' | 137.9 ns | 2.84 ns | 4.66 ns | 0.88 | 0.03 | 0.0198 | - | 344 B | 1.00 |
'Pure.DI composition root' | 141.5 ns | 2.69 ns | 5.50 ns | 0.92 | 0.05 | 0.0198 | - | 344 B | 1.00 |
'Hand Coded' | 157.7 ns | 3.24 ns | 3.32 ns | 1.00 | 0.00 | 0.0198 | - | 344 B | 1.00 |
'Microsoft DI' | 189.1 ns | 3.11 ns | 2.60 ns | 1.20 | 0.02 | 0.0272 | - | 472 B | 1.37 |
LightInject | 290.2 ns | 5.79 ns | 5.42 ns | 1.84 | 0.05 | 0.0496 | - | 856 B | 2.49 |
DryIoc | 308.8 ns | 6.25 ns | 14.12 ns | 1.92 | 0.08 | 0.0496 | - | 856 B | 2.49 |
Unity | 7,874.1 ns | 137.31 ns | 128.44 ns | 50.05 | 1.18 | 0.7935 | - | 13752 B | 39.98 |
Autofac | 26,601.4 ns | 213.84 ns | 189.56 ns | 168.63 | 4.20 | 1.6479 | 0.0610 | 28944 B | 84.14 |
Func
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|
'Hand Coded' | 9.394 ns | 0.2654 ns | 0.3056 ns | 1.00 | 0.00 | 0.0014 | 24 B | 1.00 |
'Pure.DI composition root' | 9.462 ns | 0.2354 ns | 0.2087 ns | 1.00 | 0.04 | 0.0014 | 24 B | 1.00 |
'Pure.DI Resolve<T>()' | 12.645 ns | 0.2946 ns | 0.2756 ns | 1.34 | 0.05 | 0.0014 | 24 B | 1.00 |
'Pure.DI Resolve(Type)' | 13.081 ns | 0.1891 ns | 0.1676 ns | 1.38 | 0.04 | 0.0014 | 24 B | 1.00 |
DryIoc | 70.358 ns | 1.4444 ns | 1.2804 ns | 7.41 | 0.27 | 0.0069 | 120 B | 5.00 |
LightInject | 328.676 ns | 5.6607 ns | 4.7269 ns | 34.52 | 0.99 | 0.0291 | 504 B | 21.00 |
Unity | 4,550.994 ns | 30.3968 ns | 26.9460 ns | 479.38 | 13.31 | 0.1450 | 2552 B | 106.33 |
Autofac | 11,121.527 ns | 148.2118 ns | 138.6374 ns | 1,174.57 | 28.55 | 0.8087 | 14008 B | 583.67 |
Singleton
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|
'Hand Coded' | 8.469 ns | 0.2493 ns | 0.4165 ns | 1.00 | 0.00 | 0.0014 | - | 24 B | 1.00 |
'Pure.DI composition root' | 9.506 ns | 0.2733 ns | 0.3648 ns | 1.11 | 0.06 | 0.0014 | - | 24 B | 1.00 |
'Pure.DI Resolve<T>()' | 12.508 ns | 0.2986 ns | 0.2793 ns | 1.43 | 0.08 | 0.0014 | - | 24 B | 1.00 |
'Pure.DI Resolve(Type)' | 12.572 ns | 0.2674 ns | 0.2501 ns | 1.43 | 0.07 | 0.0014 | - | 24 B | 1.00 |
DryIoc | 28.755 ns | 0.2372 ns | 0.2219 ns | 3.28 | 0.15 | 0.0014 | - | 24 B | 1.00 |
'Simple Injector' | 35.304 ns | 0.2639 ns | 0.2060 ns | 3.99 | 0.19 | 0.0014 | - | 24 B | 1.00 |
'Microsoft DI' | 39.436 ns | 0.4113 ns | 0.3435 ns | 4.48 | 0.24 | 0.0014 | - | 24 B | 1.00 |
LightInject | 910.508 ns | 5.9297 ns | 5.2565 ns | 103.56 | 4.97 | 0.0010 | - | 24 B | 1.00 |
Unity | 7,927.580 ns | 29.0347 ns | 24.2453 ns | 900.81 | 44.06 | 0.1831 | - | 3184 B | 132.67 |
Autofac | 19,562.149 ns | 236.3915 ns | 209.5549 ns | 2,224.36 | 94.37 | 1.3733 | 0.0305 | 24208 B | 1,008.67 |
'Castle Windsor' | 31,009.123 ns | 327.3126 ns | 306.1684 ns | 3,531.38 | 141.29 | 1.3428 | - | 23912 B | 996.33 |
Ninject | 120,850.129 ns | 2,359.7906 ns | 3,532.0234 ns | 14,228.15 | 605.72 | 4.2725 | 1.0986 | 74096 B | 3,087.33 |
Transient
Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|
'Hand Coded' | 8.300 ns | 0.2275 ns | 0.2017 ns | 1.00 | 0.00 | 0.0014 | - | 24 B | 1.00 |
'Pure.DI composition root' | 8.417 ns | 0.2472 ns | 0.2313 ns | 1.02 | 0.04 | 0.0014 | - | 24 B | 1.00 |
'Pure.DI Resolve<T>()' | 10.007 ns | 0.2170 ns | 0.1924 ns | 1.21 | 0.03 | 0.0014 | - | 24 B | 1.00 |
'Pure.DI Resolve(Type)' | 10.739 ns | 0.2942 ns | 0.4915 ns | 1.29 | 0.06 | 0.0014 | - | 24 B | 1.00 |
LightInject | 18.968 ns | 0.4292 ns | 0.3805 ns | 2.29 | 0.07 | 0.0014 | - | 24 B | 1.00 |
'Microsoft DI' | 24.761 ns | 0.2356 ns | 0.2089 ns | 2.98 | 0.07 | 0.0014 | - | 24 B | 1.00 |
DryIoc | 26.034 ns | 0.3170 ns | 0.2810 ns | 3.14 | 0.06 | 0.0014 | - | 24 B | 1.00 |
'Simple Injector' | 33.862 ns | 0.4485 ns | 0.3976 ns | 4.08 | 0.11 | 0.0014 | - | 24 B | 1.00 |
Unity | 11,055.904 ns | 85.9931 ns | 76.2306 ns | 1,332.65 | 31.50 | 0.2899 | - | 5176 B | 215.67 |
Autofac | 28,686.734 ns | 523.7684 ns | 464.3070 ns | 3,458.02 | 103.75 | 1.9226 | 0.0916 | 33224 B | 1,384.33 |
'Castle Windsor' | 57,113.574 ns | 417.0749 ns | 369.7260 ns | 6,883.84 | 138.26 | 3.1128 | - | 54360 B | 2,265.00 |
Ninject | 259,310.814 ns | 5,082.3164 ns | 5,438.0217 ns | 31,238.77 | 1,101.53 | 7.3242 | 1.9531 | 131008 B | 5,458.67 |
Benchmarks environment
BenchmarkDotNet v0.13.12, Ubuntu 20.04.6 LTS (Focal Fossa)
Intel Xeon Platinum 8175M CPU 2.50GHz, 1 CPU, 2 logical cores and 1 physical core
.NET SDK 8.0.201
[Host] : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX-512F+CD+BW+DQ+VL
DefaultJob : .NET 8.0.2 (8.0.224.6711), X64 RyuJIT AVX-512F+CD+BW+DQ+VL