IL.AttributeBasedDI
1.3.2
dotnet add package IL.AttributeBasedDI --version 1.3.2
NuGet\Install-Package IL.AttributeBasedDI -Version 1.3.2
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="IL.AttributeBasedDI" Version="1.3.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IL.AttributeBasedDI --version 1.3.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: IL.AttributeBasedDI, 1.3.2"
#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 IL.AttributeBasedDI as a Cake Addin #addin nuget:?package=IL.AttributeBasedDI&version=1.3.2 // Install IL.AttributeBasedDI as a Cake Tool #tool nuget:?package=IL.AttributeBasedDI&version=1.3.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
IL.AttributeBasedDI
Control dependencies and decorators via custom attributes - extends Microsoft.Extensions.DependencyInjection
How to use
- Simply reference IL.AttributeBasedDI in your project
- Use registration extensions coming with library to activate functionality:
(IServiceCollection)services.AddServiceAttributeBasedDependencyInjection()
or(WebApplicationBuilder)builder.AddServiceAttributeBasedDependencyInjection()
- Allows to filter assemblies reflection search with optional parameter
assemblyFilters
("MyProject.*" for example)
- Allows to filter assemblies reflection search with optional parameter
- Use
[Service]
attribute for your classes with optional params for auto registration in DI container:- Lifetime - to define current service registration lifetime
- ServiceType - Specifies which service is target for DI registration. If left null/default service will be automatically retrieved either from first interface current class implements or the class itself will become a serviceType.
- Use
[Decorator]
attribute for your classes with optional params for auto registration of decorator for specific service type in DI container:- ServiceType - Specifies which service is target for decoration. If left null/default service will be automatically resolved to first interface current class implements.
- DecorationOrder - Defines order of decoration. Lower decoration order will be closer to original implementation in chain of execution order. And, respectively, decorator with highest DecorationOrder will be executed last.
- Keyed Services and Decorators support! If you are using .NET 8 or higher it's possible to assign Keys to your services via corresponding attributes
Examples
IService resolves to:
- DecoratorA
- wrapping a SampleService
[Service]
class SampleService : IService {}
[Decorator]
class DecoratorA : IService {}
IService resolves to:
- DecoratorB
- wrapping a DecoratorA
- wrapping a SampleService
- wrapping a DecoratorA
[Service(serviceType: typeof(IService), lifetime: Lifetime.Singleton)]
class SampleService : IService {}
[Decorator(serviceType: typeof(IService), decorationOrder: 1)]
class DecoratorA : IService
{
public DecoratorA(IService service)
{
//IService service here is actually sample service
}
}
[Decorator(serviceType: typeof(IService), decorationOrder: 2)]
class DecoratorB : IService
{
public DecoratorB(IService service)
{
//IService service here is actually decoratorA
}
}
.NET 8 Examples
[FromKeyedServices("randomKey")]
resolves to:
- SampleServiceDefault
[FromKeyedServices("testKey")]
resolves to:
- DecoratorA
- wrapping a SampleService
[Service(Key="randomKey")]
class SampleServiceDefault : IService {}
[Service(Key="testKey")]
class SampleService : IService {}
[Decorator(Key="testKey")]
class DecoratorA : IService {}
public class Test([FromKeyedServices("randomKey")] IService randomSvc, [FromKeyedServices("testKey")] IService svc);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- IL.Misc (>= 1.1.1)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
-
net7.0
- IL.Misc (>= 1.1.1)
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
-
net8.0
- IL.Misc (>= 1.1.1)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.