DBaker.DepRegAttributes
10.0.0
dotnet add package DBaker.DepRegAttributes --version 10.0.0
NuGet\Install-Package DBaker.DepRegAttributes -Version 10.0.0
<PackageReference Include="DBaker.DepRegAttributes" Version="10.0.0" />
<PackageVersion Include="DBaker.DepRegAttributes" Version="10.0.0" />
<PackageReference Include="DBaker.DepRegAttributes" />
paket add DBaker.DepRegAttributes --version 10.0.0
#r "nuget: DBaker.DepRegAttributes, 10.0.0"
#:package DBaker.DepRegAttributes@10.0.0
#addin nuget:?package=DBaker.DepRegAttributes&version=10.0.0
#tool nuget:?package=DBaker.DepRegAttributes&version=10.0.0
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 | Versions 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. |
-
.NETStandard 2.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.