Home

Awesome

DependencyInjection.SourceGenerators

MIT License GitHub issues GitHub stars GitHub forks GitHub pull requests GitHub contributors Github nuget package GitHub code size in bytes

Overview

DependencyInjection.SourceGenerators is an advanced C# source generator specifically crafted for the Microsoft.Extensions.DependencyInjection library. It automates the process of service registration, eliminating the need for manual or reflection-based registration in .NET applications.

Key Features

Installation

DependencyInjection.SourceGenerators is available as a NuGet package. You can install it using the following command:

dotnet add package DependencyInjection.SourceGenerators

Or, add the package reference manually in your project file:

<PackageReference Include="DependencyInjection.SourceGenerators" Version="1.0.1" />

Usage

Basic Registration

Decorate your classes with one of the following attributes to register them in the DI container:

Example:

[Singleton]
public class MyService;

This will generate:

services.AddSingleton<MyService>();

Registering Interfaces

You can also register services through their interfaces:

[Singleton]
public class MyService : IMyService;

This will generate:

services.AddSingleton<IMyService, MyService>();

Keyed Service Registration

To use Keyed Services, you can annotate your service implementations with a special attribute indicating the key:

[Singleton("myKey")]
public class MyService : IMyService;

This will generate:

services.AddKeyedSingleton<IMyService, MyService>("myKey");

Multi-Assembly Configuration

DependencyInjection.SourceGenerators supports multi-assembly scenarios. For example, with assemblies MyProject.Main, MyProject.Services, and MyProject.Data, configure your ServiceCollection as follows:

var serviceCollection = new ServiceCollection();
serviceCollection.AddServicesFromMainAssembly();
serviceCollection.AddServicesFromServicesAssembly();
serviceCollection.AddServicesFromDataAssembly();
serviceCollection.BuildServiceProvider();

Enhanced Service Registration with Assembly-Level Attributes

DependencyInjection.SourceGenerators introduces an innovative and streamlined approach to service registration in .NET applications by leveraging assembly-level attributes. This feature allows developers to define their dependency injection mappings at a single, centralized location, enhancing readability and maintainability.

Key Advantages

Usage

You can register your services directly at the assembly level using attributes like Singleton, Transient, and Scoped. This method is particularly useful for large projects with numerous services, as it centralizes the DI configuration.

Example:

[assembly: Singleton<One>, Transient<ITwo, Two>, Scoped<Three>("myKey")]

In this example:

Implementation

To utilize this feature, simply place the assembly attribute in any file within your project, typically at the top of the file for visibility. The DependencyInjection.SourceGenerators tool will scan these attributes during compilation and generate the necessary DI registration code.

This approach, as described, offers a more organized and efficient way to handle dependency injection in .NET applications, especially beneficial in complex projects with a large number of services.

License

DependencyInjection.SourceGenerators is released under the MIT License, offering freedom and flexibility for both personal and commercial use under the terms of MIT.