ServiceInjection 1.0.2

dotnet add package ServiceInjection --version 1.0.2
                    
NuGet\Install-Package ServiceInjection -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="ServiceInjection" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ServiceInjection" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="ServiceInjection" />
                    
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 ServiceInjection --version 1.0.2
                    
#r "nuget: ServiceInjection, 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 ServiceInjection@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=ServiceInjection&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=ServiceInjection&version=1.0.2
                    
Install as a Cake Tool

ServiceInjection: Una biblioteca para automatizar la inyección de dependencias

ServiceInjection es una biblioteca que utiliza generadores de código fuente para generar automáticamente el código necesario para la inyección de servicios en C#. Los servicios se definen mediante una clase y un atributo personalizado.

Uso de la biblioteca

A continuación, se muestra un ejemplo de cómo usar esta biblioteca en tu proyecto:

  1. Define tu servicio e implementa la interfaz correspondiente:
public interface IMyInterface
{
    void DoSomething();
}

public class MyService : IMyInterface
{
    // Implementación del servicio
}
  1. Aplica el atributo Service a tu clase de servicio:
[Service(ServiceType.Scoped, nameof(IMyInterface))]
public class MyService : IMyInterface
{
    // Implementación del servicio
}
  1. Si tienes más de un servicio que implementa la misma interfaz, puedes usar una clave para diferenciarlos:
[Service(ServiceType.Scoped, nameof(IMyInterface), "serv1")]
public class MyService1 : IMyInterface
{
    // Implementación del servicio
}

[Service(ServiceType.Scoped, nameof(IMyInterface), "serv2")]
public class MyService2 : IMyInterface
{
    // Implementación del servicio
}
  1. La biblioteca ServiceInjection generará automáticamente el código necesario para la inyección de estos servicios en tu contenedor de inyección de dependencias:
public static class ServiceInjectionExtension
{
    public static IServiceCollection AddServiceInjection(this IServiceCollection services)
    {
        services.AddScoped<IMyInterface, MyService1>("serv1");
        services.AddScoped<IMyInterface, MyService2>("serv2");

        return services;
    }
}
  1. Luego, en tu método ConfigureServices en Startup.cs, puedes usar la extensión generada automáticamente para agregar tus servicios al contenedor de inyección de dependencias:
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddServiceInjection();
}
  1. Finalmente, puedes inyectar tus servicios en tus controladores usando la interfaz y la clave:
public class MyController : Controller
{
    private readonly IMyInterface _service1;
    private readonly IMyInterface _service2;

    public MyController([FromKeyedServices("serv1")] IMyInterface myService1, [FromKeyedServices("serv2")] IMyInterface myService2)
    {
        _service1 = myService1;
        _service2 = myService2;
    }

    public IActionResult Index()
    {
        _service1.DoSomething();
        _service2.DoSomething();

        return View();
    }
}

Si no estás utilizando una interfaz, puedes omitir el parámetro de la interfaz al aplicar el atributo Service:

[Service(ServiceType.Singleton)]
public class MySingletonService
{
    // Implementación del servicio
}

En este caso, la biblioteca ServiceInjection generará el siguiente código:

public static class ServiceInjectionExtension
{
    public static IServiceCollection AddServiceInjection(this IServiceCollection services)
    {
        services.AddSingleton<MySingletonService>();

        return services;
    }
}

Y puedes inyectar tu servicio directamente en tus controladores:

public class MyController : Controller
{
    private readonly MySingletonService _service;

    public MyController(MySingletonService service)
    {
        _service = service;
    }

    public IActionResult Index()
    {
        _service.DoSomething();

        return View();
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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 116 4/6/2024
1.0.1 113 2/7/2024