Dybal.Utils.Extensions.DependencyInjection 1.0.0-pre-07

This is a prerelease version of Dybal.Utils.Extensions.DependencyInjection.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Dybal.Utils.Extensions.DependencyInjection --version 1.0.0-pre-07                
NuGet\Install-Package Dybal.Utils.Extensions.DependencyInjection -Version 1.0.0-pre-07                
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="Dybal.Utils.Extensions.DependencyInjection" Version="1.0.0-pre-07" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dybal.Utils.Extensions.DependencyInjection --version 1.0.0-pre-07                
#r "nuget: Dybal.Utils.Extensions.DependencyInjection, 1.0.0-pre-07"                
#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 Dybal.Utils.Extensions.DependencyInjection as a Cake Addin
#addin nuget:?package=Dybal.Utils.Extensions.DependencyInjection&version=1.0.0-pre-07&prerelease

// Install Dybal.Utils.Extensions.DependencyInjection as a Cake Tool
#tool nuget:?package=Dybal.Utils.Extensions.DependencyInjection&version=1.0.0-pre-07&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, and Transient.
  • 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 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. 
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
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