DependencyInjection.SourceGenerator.Contracts 1.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package DependencyInjection.SourceGenerator.Contracts --version 1.1.1
NuGet\Install-Package DependencyInjection.SourceGenerator.Contracts -Version 1.1.1
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.Contracts" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DependencyInjection.SourceGenerator.Contracts --version 1.1.1
#r "nuget: DependencyInjection.SourceGenerator.Contracts, 1.1.1"
#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.Contracts as a Cake Addin
#addin nuget:?package=DependencyInjection.SourceGenerator.Contracts&version=1.1.1

// Install DependencyInjection.SourceGenerator.Contracts as a Cake Tool
#tool nuget:?package=DependencyInjection.SourceGenerator.Contracts&version=1.1.1

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.

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.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on DependencyInjection.SourceGenerator.Contracts:

Package Downloads
DependencyInjection.SourceGenerator.LightInject.Contracts

Contains types used to set up dependency injection registrations for LightInject

DependencyInjection.SourceGenerator.Microsoft.Contracts

Contains types used to set up dependency injection registrations for Microsoft.Extensions.DependencyInjection

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.0 371 2/14/2024
1.5.1 174 1/15/2024
1.5.0 121 1/15/2024
1.4.3 151 1/10/2024
1.4.2 132 1/10/2024
1.4.1 136 1/9/2024
1.4.0 124 1/9/2024
1.3.3 146 1/8/2024
1.3.2 139 1/8/2024
1.3.1 145 1/8/2024
1.3.0 140 1/8/2024
1.2.2 130 1/8/2024
1.2.2-alpha3 89 1/8/2024
1.2.2-alpha2 109 1/8/2024
1.2.1 178 1/5/2024
1.2.1-alpha6 223 1/5/2024
1.2.1-alpha5 172 1/5/2024
1.2.1-alpha2 191 1/5/2024
1.2.1-alpha1 214 1/5/2024
1.2.0 166 1/5/2024
1.1.2 207 1/5/2024
1.1.1 139 1/5/2024
1.1.0 185 1/5/2024
1.0.2 195 1/5/2024
1.0.1 215 1/4/2024
1.0.0 214 1/4/2024
0.1.1 344 12/14/2022
0.1.0 276 12/14/2022
0.0.3 280 12/14/2022
0.0.3-alpha4 132 12/14/2022
0.0.3-alpha3 112 12/14/2022
0.0.3-alpha2 127 12/14/2022
0.0.2 284 12/14/2022