Enhanced.DependencyInjection
1.3.0
dotnet add package Enhanced.DependencyInjection --version 1.3.0
NuGet\Install-Package Enhanced.DependencyInjection -Version 1.3.0
<PackageReference Include="Enhanced.DependencyInjection" Version="1.3.0" />
paket add Enhanced.DependencyInjection --version 1.3.0
#r "nuget: Enhanced.DependencyInjection, 1.3.0"
// Install Enhanced.DependencyInjection as a Cake Addin #addin nuget:?package=Enhanced.DependencyInjection&version=1.3.0 // Install Enhanced.DependencyInjection as a Cake Tool #tool nuget:?package=Enhanced.DependencyInjection&version=1.3.0
Enhanced.DependencyInjection
Enhanced.DependencyInjection
is a NuGet package for simplified dependency registration in .NET DI container. It uses C# attributes and a source generator for explicit registration, making the process more understandable and faster. It makes applications more resilient and easily extensible, and welcomes community contributions.
REFLECTION-FREE !!!
Motivation
Enhanced.DependencyInjection
was created with the goal of simplifying dependency registration in .NET applications. By using C# attributes and a source generator, the package provides an intuitive and explicit way to register dependencies, making the process faster and easier to understand.
However, the main motivation behind Enhanced.DependencyInjection
is to provide developers with full control over the lifecycle of dependencies and to see that lifecycle in the context of implementation code. This helps avoid separating knowledge about dependencies between different parts of the application and improves code understanding, which speeds up development and reduces the likelihood of errors.
Features
Enhanced.DependencyInjection
provides the following key features:
- Integration with .NET dependency injection.
- Support for all service lifetimes: transient, scoped, and singleton.
- Support for option registration and linking it with a configuration section.
- Implicit registration of dependencies from satellite modules.
Quick start
Install package
dotnet add package Enhanced.DependencyInjection
Mark your classes with attributes
using Enhanced.DependencyInjection;
namespace MyProject;
[Singleton<IAwesomeService>]
internal sealed class AwesomeService : IAwesomeService {
// your awesome code
}
[Options("Features:AwesomeService")]
internal sealed class AwesomeServiceOptions {
// your awesome options
}
[Transient]
internal class AwesomeCalculator {
// your awesome calculator
}
Add generated modules in container
using MyProject.Enhanced.DependencyInjection;
// ...
services.AddEnhancedModules(configuration);
// ...
How it works?
The package uses a source generator, powered by Roslyn, to automatically generate registration code at design- or compile-time.
Enhanced.DependencyInjection
generates a separate module for each assembly that uses the attributes.
// <auto-generated />
#nullable enable
using global::Enhanced.DependencyInjection.Extensions;
using IContainerModule = global::Enhanced.DependencyInjection.Modules.IContainerModule;
using IConfiguration = global::Microsoft.Extensions.Configuration.IConfiguration;
using IServiceCollection = global::Microsoft.Extensions.DependencyInjection.IServiceCollection;
using ServiceLifetime = global::Microsoft.Extensions.DependencyInjection.ServiceLifetime;
namespace MyProject
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Enhanced.DependencyInjection.CodeGeneration", "1.3.0")]
public sealed class ContainerModule : global::Enhanced.DependencyInjection.Modules.IContainerModule
{
public void AddEntries(IServiceCollection serviceCollection, IConfiguration? configuration)
{
serviceCollection.Entry<global::MyProject.AwesomeService>(global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton, typeof(global::MyProject.IAwesomeService));
serviceCollection.Entry<global::MyProject.AwesomeCalculator>(global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient);
serviceCollection.Options<global::MyProject.AwesomeServiceOptions>(configuration.GetSection("Features:AwesomeService"));
}
}
}
In addition, an extension method is generated for each module that can be used to register the module with the DI container.
// <auto-generated />
#nullable enable
using global::Enhanced.DependencyInjection.Extensions;
using IConfiguration = global::Microsoft.Extensions.Configuration.IConfiguration;
using IServiceCollection = global::Microsoft.Extensions.DependencyInjection.IServiceCollection;
namespace Microsoft.Extensions.DependencyInjection
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Enhanced.DependencyInjection.CodeGeneration", "1.3.0")]
internal static class EnhancedDependencyInjectionExtensions
{
public static IServiceCollection AddEnhancedModules(this IServiceCollection serviceCollection, IConfiguration? configuration)
{
serviceCollection.Module<global::MyProject.ContainerModule>(configuration);
return serviceCollection;
}
}
}
It's important to note that using the generated extension method is typically only necessary in the entry assembly, since satellite modules will be automatically included.
Conclusion
By using Enhanced.DependencyInjection, developers can simplify the process of dependency registration, keep their registration code clean and maintainable, and ensure that their dependencies are registered correctly with the DI container. The use of Roslyn Source Generators also ensures that the registration code is generated efficiently and without unnecessary overhead.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
-
.NETStandard 2.1
-
net6.0
-
net7.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.3.0 | 544 | 9/6/2023 | |
1.2.2 | 545 | 9/4/2023 | |
1.2.1 | 538 | 9/4/2023 | |
1.2.0 | 519 | 9/3/2023 | |
1.1.0 | 634 | 5/19/2023 | |
1.1.0-preview1 | 598 | 5/15/2023 | |
1.1.0-preview.2 | 105 | 5/15/2023 | |
1.0.1 | 863 | 11/23/2022 | |
1.0.0 | 776 | 11/6/2022 |