DBaker.DepRegAttributes 10.0.0

dotnet add package DBaker.DepRegAttributes --version 10.0.0
                    
NuGet\Install-Package DBaker.DepRegAttributes -Version 10.0.0
                    
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="DBaker.DepRegAttributes" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DBaker.DepRegAttributes" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DBaker.DepRegAttributes" />
                    
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 DBaker.DepRegAttributes --version 10.0.0
                    
#r "nuget: DBaker.DepRegAttributes, 10.0.0"
                    
#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 DBaker.DepRegAttributes@10.0.0
                    
#: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=DBaker.DepRegAttributes&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DBaker.DepRegAttributes&version=10.0.0
                    
Install as a Cake Tool

DepRegAttributes

Register services with attributes. Skip manual registration in Program.cs!


Quick Start

Attributes for registration:

[RegisterTransient]
[RegisterScoped]
[RegisterSingleton]

Register all attributed services:

serviceCollection.AddByAttribute();
serviceCollection.AddByAttribute(assembly); // Only from specific assembly

Usage Examples

1. Basic Registration

[RegisterTransient]
public class ExampleService { }
// Equivalent: serviceCollection.AddTransient<ExampleService>();

If your class implements an interface with a matching name, it will be registered as that interface:

[RegisterTransient]
public class ExampleService : IExampleService { }
// Equivalent: serviceCollection.AddTransient<IExampleService, ExampleService>();

Note: Explicit service types in the attribute override this behavior.

2. Explicit Service Types

[RegisterTransient<IAnotherExampleService>]
public class ExampleService : ExampleServiceBase, IExampleService, IAnotherExampleService { }
// Equivalent: serviceCollection.AddTransient<IAnotherExampleService, ExampleService>();

Register with multiple service types:

[RegisterTransient<ExampleServiceBase, IExampleService, IAnotherExampleService>]
public class ExampleService : ExampleServiceBase, IExampleService, IAnotherExampleService { }
// Equivalent:
// serviceCollection.AddTransient<ExampleServiceBase, ExampleService>();
// serviceCollection.AddTransient<IExampleService, ExampleService>();
// serviceCollection.AddTransient<IAnotherExampleService, ExampleService>();

Or use multiple attributes:

[RegisterTransient<ExampleServiceBase>]
[RegisterTransient<IExampleService>]
[RegisterTransient<IAnotherExampleService>]
public class ExampleService : ExampleServiceBase, IExampleService, IAnotherExampleService { }
// Equivalent: One registration per attribute

C# < 11: Use type arguments instead of generics:

[RegisterTransient(typeof(IExampleService), typeof(IAnotherExampleService))]
public class ExampleService : ExampleServiceBase, IExampleService, IAnotherExampleService { }

3. Singleton & Scoped Registration

For each attribute, only one object is constructed for singleton or scoped services:

[RegisterSingleton<IExampleService, IAnotherExampleService>]
public class ExampleService : IExampleService, IAnotherExampleService { }
// Equivalent:
// serviceCollection.AddSingleton<IExampleService, ExampleService>();
// serviceCollection.AddSingleton(sp => (IAnotherExampleService)sp.GetRequiredService<IExampleService>());

Both interfaces resolve to the same instance.

Multiple attributes = different instances:

[RegisterSingleton<IExampleService>]
[RegisterSingleton<IAnotherExampleService>]
public class ExampleService : IExampleService, IAnotherExampleService { }
// Equivalent: Separate singleton for each service type

Registering Hosted Services

To register a hosted service, use:

[RegisterSingleton<IHostedService>]
public class MyWorker : IHostedService { }
// Equivalent: services.AddHostedService<MyWorker>()

Advanced Features

Registration Tags

Tags allow conditional registration. Tags must be constants (string, enum, number):

serviceCollection.AddByAttribute("Example");
serviceCollection.AddByAttribute(14);
serviceCollection.AddByAttribute(ExampleEnum.ExampleValue);

Set tags via the attribute:

[RegisterTransient(Tag = "Example")]
public class ExampleService { }
// Only registered if tag matches

Call AddByAttribute for each tag:

serviceCollection.AddByAttribute(); // Untagged
serviceCollection.AddByAttribute(ExampleEnum.ExampleValue);
serviceCollection.AddByAttribute(14);

Keyed Services (Not available in v3)

Read more: .NET 8 Release Notes

Register a keyed service:

[RegisterTransient(Key = "Example")]
public class ExampleService { }
// Equivalent: serviceCollection.AddKeyedTransient<ExampleService>("Example");

Unbound Generics

Register a generic class as an unbound generic:

[RegisterTransient]
public class ExampleService<T> { }
// Equivalent: serviceCollection.AddTransient(typeof(ExampleService<>));

Explicit interface registration for unbound generics:

[RegisterTransient(typeof(IExampleService<>))]
public class ExampleService<T> : IExampleService<T> { }
// Equivalent: serviceCollection.AddTransient(typeof(IExampleService<>), typeof(ExampleService<>));

Note: Use typeof() for unbound generics. No analyzer support (runtime errors only).

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
10.0.0 143 11/22/2025
9.0.0 623 1/31/2025
8.0.1 831 4/26/2024
8.0.0 407 1/11/2024
3.1.1 202 4/26/2024
3.1.0 202 1/11/2024