Simple.DependencyInjection
1.0.0
dotnet add package Simple.DependencyInjection --version 1.0.0
NuGet\Install-Package Simple.DependencyInjection -Version 1.0.0
<PackageReference Include="Simple.DependencyInjection" Version="1.0.0" />
<PackageVersion Include="Simple.DependencyInjection" Version="1.0.0" />
<PackageReference Include="Simple.DependencyInjection" />
paket add Simple.DependencyInjection --version 1.0.0
#r "nuget: Simple.DependencyInjection, 1.0.0"
#:package Simple.DependencyInjection@1.0.0
#addin nuget:?package=Simple.DependencyInjection&version=1.0.0
#tool nuget:?package=Simple.DependencyInjection&version=1.0.0
Simple.DependencyInjection
<b>Simple.DependencyInjection</b> is a tool powered by the .NET Source Generator. It is designed to automate the registration of services into the Inversion of Control (IoC) container in .NET applications. This tool significantly simplifies dependency injection setup, reducing boilerplate code and enhancing maintainability and readability.
The useful part of this tool is that you can easily understand how each service in your application is registered by just looking at the attribute located right inside the given service.
The registration code is not hidden, so you don't need to worry about how it is happening. It is just auto-generated and placed in an appropriate file which you can access at any time.
Features
<b>Automatic Service Registration</b>: Automatically scans for service classes and interfaces, registering them inside the IoC container.
<b>Customizable Scoping</b>: Allows configuration of service lifetimes (transient, scoped, singleton) through attributes.
<b>Performance Optimization</b>: Leverages source generation for compile-time registration, ensuring minimal runtime overhead.
Usage
Installation
Add the Simple.DependencyInjection package to your project using the .NET CLI:
dotnet add package Simple.DependencyInjection
Or via the NuGet Package Manager in Visual StuDependencyInjectiono.
Code example
For the code example we are going to use a simple Worker Service.
To use the automatic service registration, you should invoke the
.RegisterResolvableServices()
extension method to your <b>IServiceCollection</b>.Program.cs
using Simple.DependencyInjection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Demo; public class Program { public static void Main(string[] args) { var builder = Host.CreateApplicationBuilder(args); builder.Services.AddHostedService<Worker>(); builder.Services.RegisterResolvableServices(); // <---------- var host = builder.Build(); host.Run(); } }
Applying the attributes to your services
Service that does not implement any interface
namespace Demo { [Service(Lifetime.Scoped)] public class ScopedAttributeTarget { } }
Service that implements an interface
using Contracts; namespace Demo; [Service(Lifetime.Transient, nameof(ITransientTarget))] public class TransientAttributeTarget : ITransientTarget { public bool ReturnTrue() { return true; } }
Open Generic registration
[OpenGenericService(Lifetime.Singleton, nameof(IRepository<T>))] public class Repository<T> : IRepository<T> where T : class, new() { public T Get() => new T(); }
As a result the following file is created:
// <auto-generated /> // IMPORTANT: This class is auto-generated! using Microsoft.Extensions.DependencyInjection; namespace Simple.DependencyInjection { public static class ServiceRegistrator { public static IServiceCollection RegisterResolvableServices(this IServiceCollection services) { services.Add(new ServiceDescriptor(typeof(Demo.ScopedAttributeTarget), typeof(Demo.ScopedAttributeTarget), ServiceLifetime.Scoped)); services.Add(new ServiceDescriptor(typeof(Contracts.ITransientTarget), typeof(Demo.TransientAttributeTarget), ServiceLifetime.Transient)); services.Add(new ServiceDescriptor(typeof(Contracts.IRepository<>), typeof(Demo.Repository<>), ServiceLifetime.Singleton)); return services; } } }
Lifetime object
Follows the <b>ServiceLifetime</b> enum that comes from the Microsoft DependencyInjection package
- Lifetime.Transient
- Lifetime.Scoped
- Lifetime.Singleton
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Afterword
By automating service registration, <b>Simple.DependencyInjection</b> streamlines the development process, ensuring clean, and readable code. Embrace the power of source generators and simplify your dependency injection setup.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. 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. |
-
net8.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.0.0 | 130 | 6/14/2024 |