InjectionExpert 0.2.1
dotnet add package InjectionExpert --version 0.2.1
NuGet\Install-Package InjectionExpert -Version 0.2.1
<PackageReference Include="InjectionExpert" Version="0.2.1" />
<PackageVersion Include="InjectionExpert" Version="0.2.1" />
<PackageReference Include="InjectionExpert" />
paket add InjectionExpert --version 0.2.1
#r "nuget: InjectionExpert, 0.2.1"
#:package InjectionExpert@0.2.1
#addin nuget:?package=InjectionExpert&version=0.2.1
#tool nuget:?package=InjectionExpert&version=0.2.1
Injection Expert
Injection Expert is a dependency injection framework for .NET, featuring dynamic code generation for high performance and flexibility. It supports constructor and member injection, generic type resolution, and advanced configuration options.
Compared to Microsoft.Extensions.DependencyInjection
,
this library offers more flexibility by allowing dynamically adding injections.
This library has good compatibility with Microsoft.Extensions.DependencyInjection
,
IInjectionProvider
of this library can be easily adapted to IServiceProvider
, and vice versa.
Key Concepts
- InjectionContainer: The registry for dependencies. Supports singleton and transient lifetimes, generic type mapping, and resolution of injections.
- ConstructorInjector: Dynamically generates code to inject dependencies via constructors.
- MemberInjector: Dynamically generates code to inject dependencies into fields and properties marked with
[Injection]
attribute. - Attributes:
[Injection]
requires injector to inject fields/properties withoutrequired
keyword, or ignore required members. This attribute can also indicate whether the member needs keyed injection.
Rules
Rules for Selecting Members to Inject
Fields or properties must fulfill all requirements at the same time:
Is marked with
required
keyword or[Injection]
attribute whenenabled
parameter is true.Is not a literal or init-only field.
Is not a read-only property.
Rules for Selecting Constructors to Inject
- Constructors with the lowest number of parameters are prioritized.
- The first construct that all parameters can be resolved is selected.
Usage
Registering and Resolving Dependencies
var container = new InjectionContainer()
.AddSingleton(1)
.AddSingleton(0.5)
.AddSingleton("Sample");
// Constructor injection
var succeeded = ConstructorInjector
.For(typeof(MyClass))
.TryInject(out var instance, container, out _);
var target = (MyClass?)instance;
if (succeeded && target != null)
{
Console.WriteLine(target.Text); // "Sample"
Console.WriteLine(target.Number); // 1
Console.WriteLine(target.Value); // 0.5
}
Member Injection
public class MyTarget
{
// This field will be injected because it is marked with [Injection]
[Injection] public int NumberField = 0;
// This field will be injected because it is marked as required
public required string StringField = "";
// This field will NOT be injected even it is a required member, because it is marked with [Injection(enabled: false)
[Injection(false)] public required int IgnoredMember = "";
}
var container = new InjectionContainer()
.AddSingleton(1)
.AddSingleton("Sample");
var sample = new MyTarget();
MemberInjector
.For(typeof(MyTarget))
.TryInject(sample, container, out _);
// sample.NumberField == 1
// sample.StringField == "Sample"
Generic Type Registration
container.AddTransient(typeof(IMyGenericInterface<,>), typeof(MyGenericType<,>));
var instance = container.GetInjection<IMyGenericInterface<int, long>>();
// instance is resolved as MyGenericType<long, int>
Features
- High-performance dynamic code generation for injection.
- Supports both constructor and member injection.
- Generic type mapping and resolution.
- Attribute-based configuration for fine control.
- Extensible and suitable for advanced scenarios.
For advanced usage and configuration, please refer to the source code and test cases.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- EmitToolbox (>= 0.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 0.2.1
Breaking Changes:
- Add a struct 'InjectionItem' to replace the previous '(object, InjectionLifespan)'.
- Separate the functor support from 'IInjectionContainer' to 'FunctorInjectionProvider';
now functor wrapper can cache singleton instances;
users can extend an injection provider by combining functor wrapper and chained wrapper.
Improvements:
- Unify the word usage in documentation.