DependencyInjection.SourceGenerator.Microsoft 1.2.1-alpha2

This is a prerelease version of DependencyInjection.SourceGenerator.Microsoft.
There is a newer version of this package available.
See the version list below for details.
dotnet add package DependencyInjection.SourceGenerator.Microsoft --version 1.2.1-alpha2
                    
NuGet\Install-Package DependencyInjection.SourceGenerator.Microsoft -Version 1.2.1-alpha2
                    
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.SourceGenerator.Microsoft" Version="1.2.1-alpha2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DependencyInjection.SourceGenerator.Microsoft" Version="1.2.1-alpha2" />
                    
Directory.Packages.props
<PackageReference Include="DependencyInjection.SourceGenerator.Microsoft" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DependencyInjection.SourceGenerator.Microsoft --version 1.2.1-alpha2
                    
#r "nuget: DependencyInjection.SourceGenerator.Microsoft, 1.2.1-alpha2"
                    
#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.
#:package DependencyInjection.SourceGenerator.Microsoft@1.2.1-alpha2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DependencyInjection.SourceGenerator.Microsoft&version=1.2.1-alpha2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DependencyInjection.SourceGenerator.Microsoft&version=1.2.1-alpha2&prerelease
                    
Install as a Cake Tool

DependencyInjection.SourceGenerator.Microsoft

Register services using attributes instead of registering in code.

Usage

Add the "Register" attribute to the class you want to register. The attribute takes a type and a lifetime. The type is the type you want to register and the lifetime is the lifetime of the service. The lifetime is optional and defaults to Transient.

Microsoft.Extensions.DependencyInjection


namespace RootNamespace.Services;

public interface IExampleService
{
	string GetExample();
}

public interface IAnotherService
{
	string GetAnother();
}

[Register(ServiceName = "ServiceName", Lifetime = Lifetime.Singleton)]
public class ExampleService : IExampleService
{
	public string GetExample()
	{
		return "Example";
	}
}

[Decorate]
public class KeyedService : IExampleService
{
	public string GetExample()
	{
		return "Keyed";
	}
}

[Decorator]
public class ServiceDecorator : IExampleService
{
	private readonly IExampleService _exampleService;

	public ServiceDecorator(IExampleService exampleService)
	{
		_exampleService = exampleService;
	}

	public string GetExample()
	{
		return _exampleService.GetExample();
	}
}

[Register<IAnotherService>]
public class MultipleInterfacesService : IExampleService, IAnotherService
{
	public string GetExample()
	{
		return "MultipleInterfaces";
	}

	public string GetAnother()
	{
		return "Another";
	}
}

Generates a class ServiceCollectionExtensions Assuming the project is named MyProject, the generated method will be named AddMyProject.

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace RootNamespace;
using global::Microsoft.Extensions.DependencyInjection;

[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static class ServiceCollectionExtensions
{
    public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddMyProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
        services.AddKeyedSingleton<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ExampleService>("ServiceName");
        services.Decorate<global::RootNamespace.Services.IExampleService, global::RootNamespace.Services.ServiceDecorator>();
        services.AddTransient<global::RootNamespace.Services.IAnotherService, global::RootNamespace.Services.MultipleInterfacesService>();
        return services;
    }
}

You can also create a method that will be called by the AddMyProject method. This is useful if you want to register services in a different way.

using global::DependencyInjection.SourceGenerator.Contracts.Attributes;

namespace DependencyInjection.SourceGenerator.Microsoft.Demo;

public class Registrator
{
    [RegistrationExtension]
    internal static void Register(global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
		// Register your additional services here
    }
}

This will then produce the following code:

public static class ServiceCollectionExtensions
{
    public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddTestProject(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
        global::DependencyInjection.SourceGenerator.Microsoft.Demo.Registrator.Register(services);
        return services;
    }
}

Lifetime

The lifetime is an enum with the following values:

  • Transient
  • Scoped
  • Singleton
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DependencyInjection.SourceGenerator.Microsoft:

Package Downloads
KolibSoftware.Api.Infra

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.0 696 10/14/2025
3.0.0 367 8/13/2025
3.0.0-alpha9 193 1/23/2025
3.0.0-alpha8 254 1/21/2025
3.0.0-alpha7 153 1/21/2025
3.0.0-alpha6 154 1/21/2025
3.0.0-alpha5 162 1/20/2025
3.0.0-alpha4 153 1/20/2025
3.0.0-alpha3 169 1/20/2025
3.0.0-alpha2 152 1/20/2025
3.0.0-alpha11 205 1/23/2025
3.0.0-alpha1 321 1/19/2025
2.0.0 284 8/30/2024
1.6.0 288 2/14/2024
1.5.1 287 1/15/2024
1.5.0 216 1/15/2024
1.4.3 304 1/10/2024
1.4.2 252 1/10/2024
1.4.1 288 1/9/2024
1.2.1-alpha2 410 1/5/2024
Loading failed