Injectio 1.0.33

.NET Standard 2.0
dotnet add package Injectio --version 1.0.33
NuGet\Install-Package Injectio -Version 1.0.33
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="Injectio" Version="1.0.33" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Injectio --version 1.0.33
#r "nuget: Injectio, 1.0.33"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Injectio as a Cake Addin
#addin nuget:?package=Injectio&version=1.0.33

// Install Injectio as a Cake Tool
#tool nuget:?package=Injectio&version=1.0.33

Injectio

Source generator that helps register discovered services in the dependency injection container

Features

  • Transient, Singleton, Scoped service registration
  • Factory registration
  • Module method registration
  • Duplicate Strategy - Skip,Replace,Append
  • Registration Strategy - Self, Implemented Interfaces, Self With Interfaces

Usage

Add package

Add the nuget package project to your projects.

dotnet add package Injectio

Prevent dependances from including Injectio

<PackageReference Include="Injectio" PrivateAssets="all" />

Registration Attributes

Place registration attribute on class. The class will be discovered and registered.

  • [RegisterSingleton] Marks the class as a singleton service
  • [RegisterScoped] Marks the class as a scoped service
  • [RegisterTransient] Marks the class as a transient service
  • [RegisterServices] Marks the method to be called to register services
Attribute Properties
Property Description
ImplementationType The type that implements the service. If not set, the class the interface is on will be used.
ServiceType The type of the service. If not set, the Registration property used to determine what is registered.
Factory Name of a factory method to create new instances of the service implementation.
Duplicate How the generator handles duplicate registrations. See Duplicate Strategy
Registration How the generator determines what to register. See Registration Strategy
Duplicate Strategy
Value Description
Skip Skips registrations for services that already exists
Replace Replaces existing service registrations
Append Appends a new registration for existing services
Registration Strategy
Value Description
Self Registers each matching concrete type as itself
ImplementedInterfaces Registers each matching concrete type as all of its implemented interfaces
SelfWithInterfaces Registers each matching concrete type as all of its implemented interfaces and itself
Singleton services
[RegisterSingleton]
public class SingletonService : IService { }

Explicit service type

[RegisterSingleton(ServiceType = typeof(IService))]
public class SingletonService : IService { }
Scoped Services
[RegisterScoped]
public class ScopedService : IService { }
Transient Services
[RegisterTransient]
public class TransientService : IService { }
Factories
[RegisterTransient(Factory = nameof(ServiceFactory))]
public class FactoryService : IFactoryService
{
    private readonly IService _service;

    public FactoryService(IService service)
    { 
        _service = service;
    }

    public static IFactoryService ServiceFactory(IServiceProvider serviceProvider)
    {
        return new FactoryService(serviceProvider.GetService<IService>());
    }
}
Open Generic
[RegisterSingleton(ImplementationType = typeof(OpenGeneric<>), ServiceType = typeof(IOpenGeneric<>))]
public class OpenGeneric<T> : IOpenGeneric<T> { }
Register Method

When the service registration is complex, use the RegisterServices attribute on a method that has a parameter of IServiceCollection or ServiceCollection

public class RegistrationModule
{
    [RegisterServices]
    public static void Register(IServiceCollection services)
    {
        services.TryAddTransient<IModuleService, ModuleService>();
    }
}
Add to container

The source generator creates an extension method with all the discovered services registered. Call the generated extension method to add the services to the container. The method will be called Add[AssemblyName]. The assembly name will have the dots removed.

var services = new ServiceCollection();
services.AddInjectioTestsConsole();

Override the extension method name by using the InjectioName MSBuild property.

<PropertyGroup>
  <InjectioName>Library</InjectioName>
</PropertyGroup>
var services = new ServiceCollection();
services.AddLibrary();
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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.33 413 10/25/2022
1.0.30 122 10/24/2022
1.0.28 120 10/22/2022
1.0.25 120 10/21/2022
1.0.23 150 10/20/2022
1.0.20 131 10/20/2022
1.0.18 135 10/20/2022
1.0.16 106 10/5/2022
1.0.14 104 10/5/2022
1.0.12 104 10/5/2022
1.0.10 106 10/4/2022
1.0.8 125 9/25/2022