MeleagantDependencyScan 1.2.0
dotnet add package MeleagantDependencyScan --version 1.2.0
NuGet\Install-Package MeleagantDependencyScan -Version 1.2.0
<PackageReference Include="MeleagantDependencyScan" Version="1.2.0" />
paket add MeleagantDependencyScan --version 1.2.0
#r "nuget: MeleagantDependencyScan, 1.2.0"
// Install MeleagantDependencyScan as a Cake Addin #addin nuget:?package=MeleagantDependencyScan&version=1.2.0 // Install MeleagantDependencyScan as a Cake Tool #tool nuget:?package=MeleagantDependencyScan&version=1.2.0
MeleagantDependencyScan
The purpose of this library is to provide tools to facilitate dependency injection when using dotnet.
How to use (classic case)
First : Decorate needed classes
Singleton (without interfaces)
The simplest use case is the singleton without interfaces :
using MeleagantDependencyScan.Attributes;
[MeleagantInjection] // By default LifeTime is intialized to Singleton, so there's no need to specify it in this case
public class AuthService
{
// Your auth logic
}
Transient or Scoped (without interfaces)
Transient use case without interfaces :
using MeleagantDependencyScan.Attributes;
[MeleagantInjection(LifeTime = ServiceLifetime.Transient)]
public class AuthService
{
// Your auth logic
}
Scoped use case without interfaces :
using MeleagantDependencyScan.Attributes;
[MeleagantInjection(LifeTime = ServiceLifetime.Scoped)]
public class AuthService
{
// Your auth logic
}
With interfaces (Scoped)
Let's assume that we have an IAuthService
interface
public interface IAuthService
{
string Register(RegisterDto registerDto);
}
And a custom auth service that implement this interface here it's MySuperAuthService
. And we needed it with the scoped life time.
using MeleagantDependencyScan.Attributes;
[MeleagantInjection(LifeTime = ServiceLifetime.Scoped, VisibleFromInterface = true, VisibleAs = [typeof(IAuthService)])]
public class MySuperAuthService : IAuthService
{
// Your auth logic
public string Register(RegisterDto registerDto)
{
// Register logic
}
}
With interfaces (Transient)
Same as Scope
you just need to change the LifeTime
to Transient
With interfaces (Singleton)
Same as Scope
you just need to change the LifeTime
to Singleton
.
Note that LifeTime is set to Singleton by default
Then : Scan your assemblies
To register your classes in the ServiceCollection
you must add the ScanAssemblies
instruction to your Program.cs
Single assembly
using MeleagantDependencyScan.Extensions;
// Some logic ...
// Use this instruction
builder.Services.ScanAssemblies("MySuperAssembly");
// Before this one
var app = builder.Build();
Multiple assembly
using MeleagantDependencyScan.Extensions;
// Some logic ...
// Use this instruction
builder.Services.ScanAssemblies("MySuperAssembly", "MyAwesomeAssembly");
// Before this one
var app = builder.Build();
How to use (keyed case)
First : Decorate needed classes
With interfaces (Scoped)
Let's assume that we have an IAuthService
interface
public interface IAuthService
{
string Register(RegisterDto registerDto);
}
And two custom auth services that implement this interface here it's MySuperAuthService
and MyFabulousAuthService
. And we needed it with the scoped life time.
using MeleagantDependencyScan.Attributes;
[MeleagantInjectionKeyed(LifeTime = ServiceLifetime.Scoped, VisibleFromInterface = true, VisibleAs = [typeof(IAuthService)], Key = "Super")]
public class MySuperAuthService : IAuthService
{
// Your auth logic
public string Register(RegisterDto registerDto)
{
// Register logic
}
}
[MeleagantInjectionKeyed(LifeTime = ServiceLifetime.Scoped, VisibleFromInterface = true, VisibleAs = [typeof(IAuthService)], Key = "Fabulous")]
public class MyFabulousAuthService : IAuthService
{
// Your auth logic
public string Register(RegisterDto registerDto)
{
// Register logic
}
}
With interfaces (Transient)
Same as Scope
you just need to change the LifeTime
to Transient
With interfaces (Singleton)
Same as Scope
you just need to change the LifeTime
to Singleton
.
Note that LifeTime is set to Singleton by default
Then : Scan your assemblies
To register your classes in the ServiceCollection
you must add the ScanAssemblies
instruction to your Program.cs
Single assembly
using MeleagantDependencyScan.Extensions;
// Some logic ...
// Use this instruction
builder.Services.ScanKeyedAssemblies("MySuperAssembly");
// Before this one
var app = builder.Build();
Multiple assembly
using MeleagantDependencyScan.Extensions;
// Some logic ...
// Use this instruction
builder.Services.ScanKeyedAssemblies("MySuperAssembly", "MyAwesomeAssembly");
// Before this one
var app = builder.Build();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 is compatible. 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. |
-
net5.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
net6.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
net7.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add the attribute identifying the component to be added to the extension's ServiceCollection by key in order to scan the Assembly.