DependencyInjection.SourceGenerators 1.0.4

dotnet add package DependencyInjection.SourceGenerators --version 1.0.4                
NuGet\Install-Package DependencyInjection.SourceGenerators -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DependencyInjection.SourceGenerators" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DependencyInjection.SourceGenerators --version 1.0.4                
#r "nuget: DependencyInjection.SourceGenerators, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DependencyInjection.SourceGenerators as a Cake Addin
#addin nuget:?package=DependencyInjection.SourceGenerators&version=1.0.4

// Install DependencyInjection.SourceGenerators as a Cake Tool
#tool nuget:?package=DependencyInjection.SourceGenerators&version=1.0.4                

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

  • Automatic Registration: ARI generates code to automatically register services in the DI container using custom attributes.
  • Compile-Time Efficiency: Operates at compile time, offering a more efficient alternative to reflection or manual updates in ServiceCollection.
  • Simplified Dependency Management: Greatly reduces the complexity of managing dependencies, especially in larger projects.
  • Attribute-Based Configuration: Services are registered through easy-to-use attributes, streamlining the process.
  • Interface Registration Support: Allows for clean and concise registration of services through their interfaces.
  • Multi-Assembly Support: Facilitates service registration across multiple assemblies, enhancing modularity.
  • Keyed Service Support: Enables registration of multiple implementations of the same interface with unique keys, allowing for more nuanced dependency resolution.

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:

  • [Singleton]
  • [Scoped]
  • [Transient]

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

  • Centralized Configuration: Define all your DI mappings in one place, making it easier to manage and review.
  • Declarative Syntax: A clear and concise way to express service registrations, improving code clarity.
  • Reduced Boilerplate: Minimizes repetitive code across different parts of the application.

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:

  • One is registered as a singleton. This means a single instance of One will be created and shared across the application.
  • Two is registered as a transient implementation of the ITwo interface. This means a new instance of Two will be created each time ITwo is requested.
  • Three is registered as a scoped service with a key "myKey". This means an instance of Three will be created for each scope (like a web request in ASP.NET Core) and it can be uniquely identified or resolved using the key "myKey". This configuration is declared at the assembly level, applying to the entire assembly where it's defined. It provides a centralized and streamlined way to manage dependency injection across your application.

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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.4 1,837 6/12/2024
1.0.3 314 12/28/2023
1.0.2 110 12/28/2023
1.0.1 148 12/28/2023
1.0.0 126 12/28/2023