Injectio 4.0.0
dotnet add package Injectio --version 4.0.0
NuGet\Install-Package Injectio -Version 4.0.0
<PackageReference Include="Injectio" Version="4.0.0" />
paket add Injectio --version 4.0.0
#r "nuget: Injectio, 4.0.0"
// Install Injectio as a Cake Addin #addin nuget:?package=Injectio&version=4.0.0 // Install Injectio as a Cake Tool #tool nuget:?package=Injectio&version=4.0.0
Injectio
Source generator that helps register attribute marked services in the dependency injection ServiceCollection
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 attribute 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 { }
Support resolving multiple services with IEnumerable<T>
[RegisterSingleton(Duplicate = DuplicateStrategy.Append)]
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> { }
Generic Attributes
You can use generic attributes to register services if your project targets .NET 7.0+
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>
</Project>
Generic attributes allow declaration to be more compact by avoiding the typeof calls
[RegisterSingleton<IService>]
public class ServiceImplementation : IService { }
Keyed Services
You can register keyed services with version 8.0+ of Microsoft.Extensions.DependencyInjection
Register a keyed service
[RegisterSingleton<IServiceKeyed>(ServiceKey = "Alpha")]
public class ServiceAlphaKeyed : IServiceKeyed
{
}
[RegisterSingleton<IServiceKeyed>(ServiceKey = "Beta")]
public class ServiceBetaKeyed : IServiceKeyed
{
}
Register using an enum
public enum ServiceType
{
Alpha,
Beta
}
[RegisterSingleton<IServiceKeyed>(ServiceKey = ServiceType.Alpha)]
public class ServiceAlphaTypeKeyed : IServiceKeyed
{
}
[RegisterSingleton<IServiceKeyed>(ServiceKey = ServiceType.Beta)]
public class ServiceBetaTypeKeyed : IServiceKeyed
{
}
Register using an factory method
[RegisterSingleton<IServiceKeyed>(ServiceKey = "Charlie", Factory = nameof(ServiceFactory))]
[RegisterSingleton<IServiceKeyed>(ServiceKey = "Delta", Factory = nameof(ServiceFactory))]
public class ServiceFactoryKeyed : IServiceKeyed
{
public ServiceFactoryKeyed(object? serviceKey)
{
ServiceKey = serviceKey;
}
public object? ServiceKey { get; }
public static IServiceKeyed ServiceFactory(IServiceProvider serviceProvider, object? serviceKey)
{
return new ServiceFactoryKeyed(serviceKey);
}
}
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)
{
// add and bind configuration options, Microsoft.Extensions.Configuration.Binder
services
.AddOptions<PollingOption>()
.Configure<IConfiguration>((settings, configuration) => configuration
.GetSection(PollingOption.SectionName)
.Bind(settings)
);
}
}
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 extension 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>
<ItemGroup>
<CompilerVisibleProperty Include="InjectioName" />
</ItemGroup>
var services = new ServiceCollection();
services.AddLibrary();
Registration Tags
Control what is registered when calling the generated extension method using Tags
Tag the service
public interface IServiceTag
{
}
[RegisterSingleton(Tags = "Client,FrontEnd")]
public class ServiceTag : IServiceTag
{
}
Tags can be passed to register methods
public static class ServiceRegistration
{
[RegisterServices]
public static void Register(IServiceCollection services, ISet<string> tags)
{
}
}
Specify tags when adding to service collection. Note, if no tags specified, all services are registered
var services = new ServiceCollection();
services.AddInjectioTestsLibrary("Client");
Product | Versions 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 is compatible. 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 is compatible. |
.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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Injectio:
Package | Downloads |
---|---|
NoeticTools.Git2SemVer.Core
Package Description |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Injectio:
Repository | Stars |
---|---|
loresoft/EntityFrameworkCore.Generator
Generate Entity Framework Core model from an existing database
|
Version | Downloads | Last updated |
---|---|---|
4.0.0 | 3,169 | 11/14/2024 |
3.3.0 | 10,203 | 9/7/2024 |
3.2.0 | 236 | 9/5/2024 |
3.1.0 | 50,223 | 12/5/2023 |
3.0.1 | 585 | 11/29/2023 |
3.0.0 | 290 | 11/24/2023 |
3.0.0-beta.3 | 76 | 11/21/2023 |
3.0.0-beta.2 | 73 | 11/16/2023 |
3.0.0-beta.1 | 67 | 11/16/2023 |
2.7.0 | 2,555 | 10/25/2023 |
2.6.2 | 5,289 | 8/9/2023 |
2.6.1 | 512 | 7/29/2023 |
2.6.0 | 132 | 7/29/2023 |
2.5.87 | 5,088 | 3/25/2023 |
2.5.65 | 4,612 | 1/30/2023 |
2.0.54 | 1,256 | 1/2/2023 |
2.0.52 | 262 | 1/2/2023 |
2.0.43 | 337 | 12/28/2022 |
1.0.33 | 1,019 | 10/25/2022 |
1.0.30 | 358 | 10/24/2022 |
1.0.28 | 339 | 10/22/2022 |
1.0.25 | 333 | 10/21/2022 |
1.0.23 | 403 | 10/20/2022 |
1.0.20 | 364 | 10/20/2022 |
1.0.18 | 362 | 10/20/2022 |
1.0.16 | 346 | 10/5/2022 |
1.0.14 | 348 | 10/5/2022 |
1.0.12 | 352 | 10/5/2022 |
1.0.10 | 330 | 10/4/2022 |
1.0.8 | 419 | 9/25/2022 |