HLCores.Configurations.DI 1.0.0

dotnet add package HLCores.Configurations.DI --version 1.0.0                
NuGet\Install-Package HLCores.Configurations.DI -Version 1.0.0                
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="HLCores.Configurations.DI" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HLCores.Configurations.DI --version 1.0.0                
#r "nuget: HLCores.Configurations.DI, 1.0.0"                
#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 HLCores.Configurations.DI as a Cake Addin
#addin nuget:?package=HLCores.Configurations.DI&version=1.0.0

// Install HLCores.Configurations.DI as a Cake Tool
#tool nuget:?package=HLCores.Configurations.DI&version=1.0.0                

HLCores.Configurations.DI

HLCores.Configurations.DI is DependencyInjection using ServiceLifetime by attribute.

Why is DependencyInjectionConfiguration ?

In the below example, the IServiceA interface defines a Call method:

public interface IServiceA
{
    void Do(string name);
}

This interface is implemented by ServiceA:

public class ServiceA : IServiceA
{
    void Do(string name)
    {
        // implementation
    }
}

The example app registers the IServiceA with ServiceA by AddScoped method:

using DependencyInjectionConfigurationExample.Interfaces;
using DependencyInjectionConfigurationExample.Services;

var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddScoped<IServiceA, ServiceA>();
...
var app = builder.Build();

The IServiceA service is requested and used to call the Call method:

public class TestController : ControllerBase
{
    private readonly IServiceA _serviceA;

    public TestController(IServiceA serviceA)
    {
        _serviceA = serviceA;
    }
    public void OnRequest()
    {
        _serviceA.Do("SD3team");
    }
}

If you forget to register any nessessory service into DI engine or not defined before using, there are some annoying error can be happened:

System.InvalidOperationException: Unable to resolve service for type ...
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, ...
With DependencyInjectionConfiguration, above problem can be solved.

First of all, you need to create IServiceA service with DependencyInjection attribute and specify that the service will be created every time it is requested (Transient) :

[DependencyInjectionAttribute(ServiceLifetime.Transient)]
public interface IServiceA
{
    void Do();
}

Finally, register the DependencyInjection by adding this line into Program.cs:

builder.Services.AddDependencyInjectionConfiguration(typeof(Program));

And now, all of your services that attributed with DependencyInjection will be automatically registered in the dotnet core DI engine. When you create a new service with DependencyInjection attribute, you do not need to add any new code line into DI engine for the new service register.

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 124 8/16/2024