AutoRegisterInject 1.2.0
See the version list below for details.
dotnet add package AutoRegisterInject --version 1.2.0
NuGet\Install-Package AutoRegisterInject -Version 1.2.0
<PackageReference Include="AutoRegisterInject" Version="1.2.0" />
paket add AutoRegisterInject --version 1.2.0
#r "nuget: AutoRegisterInject, 1.2.0"
// Install AutoRegisterInject as a Cake Addin #addin nuget:?package=AutoRegisterInject&version=1.2.0 // Install AutoRegisterInject as a Cake Tool #tool nuget:?package=AutoRegisterInject&version=1.2.0
AutoRegisterInject
AutoRegisterInject, also referred to as ARI, is a C# source generator that will automatically create Microsoft.Extensions.DependencyInjection registrations for types marked with attributes.
This is a compile time alternative to reflection/assembly scanning for your injections or manually adding to the ServiceCollection
every time a new type needs to be registered.
For example:
namespace MyProject;
[RegisterScoped]
public class Foo { }
will automatically generate an extension method called AutoRegister()
for IServiceProvider
, that registers Foo
, as scoped.
internal IServiceCollection AutoRegister(this IServiceCollection serviceCollection)
{
serviceCollection.AddScoped<Foo>();
return serviceCollection;
}
In larger projects, dependency injection registration becomes tedious and in team situations can lead to merge conflicts which can be easily avoided.
AutoRegisterInject moves the responsibility of service registration to the owning type rather than external service collection configuration, giving control and oversight of the type that is going to be registered with the container.
Installation
Install the Nuget package, and start decorating classes with ARI attributes.
Use dotnet add package AutoRegisterInject
or add a package reference manually:
<PackageReference Include="AutoRegisterInject" />
Usage
Classes should be decorated with one of three attributes:
[RegisterScoped]
[RegisterSingleton]
[RegisterTransient]
Register a class:
[RegisterScoped]
class Foo;
and get the following output:
serviceCollection.AddScoped<Foo>();
Update the service collection by invoking:
var serviceCollection = new ServiceCollection();
serviceCollection.AutoRegister();
serviceCollection.BuildServiceProvider();
You can now inject Foo
as a dependency and have this resolved as scoped.
Register as interface
Implement one or many interfaces on your target class:
[RegisterTransient]
class Bar : IBar;
and get the following output:
serviceCollection.AddTransient<IBar, Bar>();
Important note: AutoRegisterInject is opinionated and Bar
will only be registered with its implemented interface. ARI will not register Bar
. Bar
will always need to be resolved from IBar
in your code.
Implementing multiple interfaces will have the implementing type be registered for each distinct interface.
[RegisterTransient]
class Bar : IBar, IFoo, IBaz;
will output the following:
serviceCollection.AddTransient<IBar, Bar>();
serviceCollection.AddTransient<IFoo, Bar>();
serviceCollection.AddTransient<IBaz, Bar>();
Important note: AutoRegisterInject is opinionated and Bar
will only be registered with its implemented interfaces. ARI will not register Bar
. Bar
will always need to be resolved from IBar
, IFoo
or IBaz
in your code.
Multiple assemblies
In addition to the AutoRegister
extension method, every assembly that AutoRegisterInject is a part of, a AutoRegisterFromAssemblyName
will be generated. This allows you to configure your service collection from one, main, executing assembly.
Given 3 assemblies, MyProject.Main
, MyProject.Services
, MyProject.Data
, you can configure the ServiceCollection
as such:
var serviceCollection = new ServiceCollection();
serviceCollection.AutoRegisterFromMyProjectMain();
serviceCollection.AutoRegisterFromMyProjectServices();
serviceCollection.AutoRegisterFromMyProjectData();
serviceCollection.BuildServiceProvider();
AutoRegisterInject will remove illegal characters from assembly names in order to generate legal C# method names. ,
, .
and
will be removed.
License
AutoRegisterInject is MIT licensed. Do with it what you please under the terms of MIT.
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 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. |
.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
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on AutoRegisterInject:
Package | Downloads |
---|---|
GoLive.Blazor.Controls
Some random Blazor Web Controls. |
GitHub repositories
This package is not used by any popular GitHub repositories.