DependencyInjection.SourceGenerator.Microsoft 1.2.1-alpha1

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-alpha1                
NuGet\Install-Package DependencyInjection.SourceGenerator.Microsoft -Version 1.2.1-alpha1                
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-alpha1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DependencyInjection.SourceGenerator.Microsoft --version 1.2.1-alpha1                
#r "nuget: DependencyInjection.SourceGenerator.Microsoft, 1.2.1-alpha1"                
#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.SourceGenerator.Microsoft as a Cake Addin
#addin nuget:?package=DependencyInjection.SourceGenerator.Microsoft&version=1.2.1-alpha1&prerelease

// Install DependencyInjection.SourceGenerator.Microsoft as a Cake Tool
#tool nuget:?package=DependencyInjection.SourceGenerator.Microsoft&version=1.2.1-alpha1&prerelease                

DependencyInjection.SourceGenerator

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;
    }
}
Usage
var services = new ServiceCollection();
services.AddMyProject();

### LightInject

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

public interface IExampleService
{
	string GetExample();
}

Generates a class CompositionRoot

public class CompositionRoot : ICompositionRoot
{
	public static void Compose(IServiceRegistry serviceRegistry)
	{
		serviceRegistry.Register<IExampleService, ExampleService>("ServiceName", new PerContainerLifetime());
	}
}

If you already have a class CompositionRoot defined, the generated class will be made partial. Remeber to make your CompositionRoot partial as well. It will also call a method RegisterServices on the existing CompositionRoot class (this must be defined).

public partial class CompositionRoot : ICompositionRoot
{
	public static void Compose(IServiceRegistry serviceRegistry)
	{
		RegisterServices(serviceRegistry);
		serviceRegistry.Register<IExampleService, ExampleService>("ServiceName", new PerContainerLifetime());
	}
}

The final existing CompositionRoot class must look like this:

public partial class CompositionRoot
{
	public void RegisterServices(IServiceRegistry serviceRegistry)
	{
		// Register services here
	}
}

Lifetime

The lifetime is an enum with the following values:

  • Transient
  • Scoped
  • Singleton

Misc

Current only works with LightInject.

There are no supported framework assets in this 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
2.0.0 110 8/30/2024
1.6.0 205 2/14/2024
1.5.1 215 1/15/2024
1.5.0 147 1/15/2024
1.4.3 206 1/10/2024
1.4.2 181 1/10/2024
1.4.1 198 1/9/2024
1.4.0 171 1/9/2024
1.3.3 188 1/8/2024
1.3.2 183 1/8/2024
1.3.1 174 1/8/2024
1.3.0 165 1/8/2024
1.2.2 157 1/8/2024
1.2.2-alpha3 170 1/8/2024
1.2.2-alpha2 147 1/8/2024
1.2.1 192 1/5/2024
1.2.1-alpha6 192 1/5/2024
1.2.1-alpha5 187 1/5/2024
1.2.1-alpha2 182 1/5/2024
1.2.1-alpha1 178 1/5/2024
1.2.0 176 1/5/2024
1.1.2 186 1/5/2024
1.1.1 154 1/5/2024
1.1.0 149 1/5/2024
1.0.2 179 1/5/2024
1.0.1 193 1/4/2024
1.0.0 193 1/4/2024