Codelisk.AutoRegisterInject.Generator 2.13.100

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

// Install Codelisk.AutoRegisterInject.Generator as a Cake Tool
#tool nuget:?package=Codelisk.AutoRegisterInject.Generator&version=2.13.100

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 four attributes:

  • [RegisterScoped]
  • [RegisterSingleton]
  • [RegisterTransient]
  • [RegisterHostedService]

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.

Alternatively, you can register hosted services by:

[RegisterHostedService]
class Foo;

and get:

serviceCollection.AddHostedService<Foo>();

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 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. 
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
2.13.100 110 9/6/2023
2.13.99 89 9/6/2023
2.13.98 93 9/6/2023
2.13.97 96 9/6/2023
2.13.95 102 9/4/2023
2.13.90 99 8/31/2023
2.13.89 113 8/25/2023
2.13.86 107 8/25/2023
2.13.85 108 8/25/2023
2.13.84 109 8/25/2023
2.13.83 103 8/25/2023
2.13.82 99 8/25/2023
2.13.81 98 8/25/2023
2.13.79 99 8/25/2023
2.13.78 104 8/25/2023
2.13.77 103 8/25/2023
2.13.76 106 8/25/2023
2.13.75 114 8/25/2023
2.13.74 104 8/25/2023
2.13.72 104 8/24/2023
2.13.71 106 8/24/2023
2.13.70 103 8/24/2023
2.13.69 106 8/24/2023
2.13.68 98 8/24/2023
2.13.66 102 8/24/2023
2.13.64 104 8/24/2023
2.13.63 103 8/24/2023
2.13.62 104 8/23/2023
2.13.61 101 8/23/2023
2.13.60 98 8/23/2023
2.13.59 101 8/23/2023
2.13.57 105 8/23/2023
2.13.56 104 8/23/2023
2.13.54 103 8/23/2023
2.13.53 101 8/23/2023
2.13.52 101 8/22/2023
2.13.51 101 8/22/2023
2.13.50 102 8/22/2023
2.13.49 95 8/22/2023
2.13.48 96 8/22/2023
2.13.47 93 8/22/2023
2.13.46 101 8/22/2023
2.13.45 100 8/22/2023
2.13.44 94 8/22/2023
2.13.43 89 8/22/2023
2.13.42 97 8/22/2023
2.13.41 103 8/22/2023
2.13.37 136 8/22/2023
2.13.36 177 8/22/2023
2.13.34 150 8/22/2023
2.13.33 188 8/22/2023
2.13.32 158 8/22/2023
2.13.30 194 8/22/2023
2.13.29 185 8/22/2023
2.13.26 114 8/22/2023
2.13.25 102 8/21/2023
2.13.21 106 8/21/2023
2.13.20 99 8/21/2023
2.13.19 107 8/21/2023
2.13.18 99 8/21/2023
2.13.17 99 8/21/2023
2.13.16 103 8/21/2023
2.13.15 103 8/21/2023
2.13.14 118 8/19/2023
2.13.13 97 8/18/2023
2.13.12 101 8/18/2023
2.13.9 100 8/18/2023
2.13.8 110 8/17/2023
2.13.7 113 8/16/2023
2.13.6 117 8/16/2023
2.13.5 125 8/8/2023
2.13.4 118 8/8/2023
2.13.3 119 8/8/2023
2.13.2 118 8/8/2023
2.13.1 121 8/8/2023