Dybal.Utils.Extensions.DependencyInjection
1.0.0-pre-06
See the version list below for details.
dotnet add package Dybal.Utils.Extensions.DependencyInjection --version 1.0.0-pre-06
NuGet\Install-Package Dybal.Utils.Extensions.DependencyInjection -Version 1.0.0-pre-06
<PackageReference Include="Dybal.Utils.Extensions.DependencyInjection" Version="1.0.0-pre-06" />
paket add Dybal.Utils.Extensions.DependencyInjection --version 1.0.0-pre-06
#r "nuget: Dybal.Utils.Extensions.DependencyInjection, 1.0.0-pre-06"
// Install Dybal.Utils.Extensions.DependencyInjection as a Cake Addin #addin nuget:?package=Dybal.Utils.Extensions.DependencyInjection&version=1.0.0-pre-06&prerelease // Install Dybal.Utils.Extensions.DependencyInjection as a Cake Tool #tool nuget:?package=Dybal.Utils.Extensions.DependencyInjection&version=1.0.0-pre-06&prerelease
Dybal.Utils.Extensions.DependencyInjection
This library provides a straightforward and seamless way to register all of your application's services in a dotnet project using attributes. With this library, you can significantly reduce the complexity and verbosity of your Startup
class and make your service registration more clean and easy to understand.
Features
- Provides attributes for marking services for registration.
- Automatic service registration using reflection.
- Support for all service lifetimes:
Singleton
,Scoped
, andTransient
. - Support register a service with a specific interface or as self.
Installation
The library can be installed via NuGet.
dotnet add package Dybal.Utils.Extensions.DependencyInjection
Usage
First, you need to call the AddMarkedServicesInAssembly
extension method in your Startup
class' ConfigureServices
method.
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddMarkedServicesInAssembly(Assembly.GetExecutingAssembly());
// ...
}
This method will scan the assembly for classes that are decorated with any of the registration attributes and add them to the DI container with the specified lifetime.
Add services from multiple assemblies
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddMarkedServicesFromAllAssemblies();
// or
services.AddMarkedServicesFromAllAssemblies("MyProject.");
// ...
}
This method loops over all loaded assemblies, filtering them by the provided assemblyNamePrefix
parameter, and registers all services decorated with the registration attributes from those assemblies. Parameter assemblyNamePrefix
is optional all assembiels are scanned if it is not provided.
Register a service with its concrete type
[RegisterService(ServiceLifetime.Scoped)]
public class SomeScopedService
{
// Implementation goes here...
}
With RegisterService
attribute, the service is registered with its concrete type.
Register a service with a specific interface
[RegisterService<IScopedService>(ServiceLifetime.Scoped)]
public class SomeScopedService : IScopedService
{
// Implementation goes here...
}
RegisterService
attribute can also be used to register a service with a specific interface.
Shortcut attributes for specific lifetimes
[RegisterSingletonService]
public class SomeSingletonService
{
// Implementation goes here...
}
[RegisterTransientService]
public class SomeTransientService
{
// Implementation goes here...
}
[RegisterScopedService]
public class SomeScopedService
{
// Implementation goes here...
}
These attributes register the service with their respective lifetimes. They can also be used with interfaces.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 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. |
-
net7.0
- Microsoft.CodeAnalysis.CSharp (>= 4.5.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.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.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 354 | 11/15/2023 |
1.0.0-pre-07 | 362 | 7/12/2023 |
1.0.0-pre-06 | 128 | 7/5/2023 |