wis.nuget.auto.dependencyInjection 1.0.2

dotnet add package wis.nuget.auto.dependencyInjection --version 1.0.2
                    
NuGet\Install-Package wis.nuget.auto.dependencyInjection -Version 1.0.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="wis.nuget.auto.dependencyInjection" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="wis.nuget.auto.dependencyInjection" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="wis.nuget.auto.dependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add wis.nuget.auto.dependencyInjection --version 1.0.2
                    
#r "nuget: wis.nuget.auto.dependencyInjection, 1.0.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.
#:package wis.nuget.auto.dependencyInjection@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=wis.nuget.auto.dependencyInjection&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=wis.nuget.auto.dependencyInjection&version=1.0.2
                    
Install as a Cake Tool

AutoServiceRegistration

AutoServiceRegistration simplifies the registration of services in ASP.NET Core's Dependency Injection (DI) container by automatically discovering and registering services based on custom attributes.(Works fine in .Net 6.0)

Usage

Step 1: Register Services in the DI Container

Use the AutoRegisterServices extension method to automatically register your services in the Startup.cs file:

var builder = WebApplication.CreateBuilder(args);
... //Other builder
// Auto-register services
builder.Services.AutoRegisterServices();

// Other configuration methods
}

Step 2: Decorate Your Services

Decorate your service implementations with the [Service], specifying the service lifetime and priority if needed. This version add option for lifetime and priority

public interface IMyService
{
    void DoWork();
}

[Service(ServiceLifetime.Transient, priority: 1)]
public class FirstImplementation : IMyService
{
    public void DoWork()
    {
        Console.WriteLine("First Implementation");
    }
}

In case there is no option for life time default value will be generate: ServiceLifetime lifetime = ServiceLifetime.Transient, int priority = 0

Step 3: Resolve and Use Services

Resolve your services as usual via dependency injection in your controllers or other service consumers:

public class MyController : ControllerBase
{
    private readonly IMyService _myService;

    public MyController(IMyService myService)
    {
        _myService = myService;
    }

    public IActionResult DoWork()
    {
        _myService.DoWork(); // Output: "First Implementation"
        return Ok();
    }
}

Bonus

I also make it can be used with [Lombok.NET] for both controller and service so if you want you can declare like this

[Inside Controller]

[RequiredArgsConstructor]
public partial class MyController : ControllerBase
{
    private readonly IMyService _myService;

//function apply same as above
}

[Inside Service]

[Service]
[RequiredArgsConstructor]
public partial class MyService: IMyService
{
  private readonly IMyRepository _myRepository;

  public void DoWork()
  {
      Console.WriteLine("First Implementation"); // Output: "First Implementation"
  }
}
Product 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 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. 
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.2 593 5/19/2024
1.0.1 197 5/19/2024
1.0.0 228 5/19/2024 1.0.0 is deprecated because it has critical bugs.