Codelisk.AutoRegisterInject.Generator 2.13.36

There is a newer version of this package available.
See the version list below for details.
dotnet add package Codelisk.AutoRegisterInject.Generator --version 2.13.36
NuGet\Install-Package Codelisk.AutoRegisterInject.Generator -Version 2.13.36
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.36" />
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.36
#r "nuget: Codelisk.AutoRegisterInject.Generator, 2.13.36"
#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.36

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

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.

There are no supported framework assets in this 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 114 9/6/2023
2.13.99 91 9/6/2023
2.13.98 95 9/6/2023
2.13.97 98 9/6/2023
2.13.95 104 9/4/2023
2.13.90 101 8/31/2023
2.13.89 115 8/25/2023
2.13.86 109 8/25/2023
2.13.85 110 8/25/2023
2.13.84 111 8/25/2023
2.13.83 105 8/25/2023
2.13.82 101 8/25/2023
2.13.81 100 8/25/2023
2.13.79 101 8/25/2023
2.13.78 106 8/25/2023
2.13.77 105 8/25/2023
2.13.76 108 8/25/2023
2.13.75 116 8/25/2023
2.13.74 106 8/25/2023
2.13.72 106 8/24/2023
2.13.71 108 8/24/2023
2.13.70 105 8/24/2023
2.13.69 108 8/24/2023
2.13.68 100 8/24/2023
2.13.66 104 8/24/2023
2.13.64 106 8/24/2023
2.13.63 105 8/24/2023
2.13.62 106 8/23/2023
2.13.61 103 8/23/2023
2.13.60 100 8/23/2023
2.13.59 103 8/23/2023
2.13.57 107 8/23/2023
2.13.56 106 8/23/2023
2.13.54 105 8/23/2023
2.13.53 103 8/23/2023
2.13.52 103 8/22/2023
2.13.51 103 8/22/2023
2.13.50 104 8/22/2023
2.13.49 97 8/22/2023
2.13.48 98 8/22/2023
2.13.47 95 8/22/2023
2.13.46 103 8/22/2023
2.13.45 102 8/22/2023
2.13.44 96 8/22/2023
2.13.43 91 8/22/2023
2.13.42 99 8/22/2023
2.13.41 105 8/22/2023
2.13.37 138 8/22/2023
2.13.36 179 8/22/2023
2.13.34 152 8/22/2023
2.13.33 190 8/22/2023
2.13.32 160 8/22/2023
2.13.30 196 8/22/2023
2.13.29 187 8/22/2023
2.13.26 116 8/22/2023
2.13.25 104 8/21/2023
2.13.21 108 8/21/2023
2.13.20 101 8/21/2023
2.13.19 109 8/21/2023
2.13.18 101 8/21/2023
2.13.17 101 8/21/2023
2.13.16 105 8/21/2023
2.13.15 105 8/21/2023
2.13.14 120 8/19/2023
2.13.13 99 8/18/2023
2.13.12 103 8/18/2023
2.13.9 102 8/18/2023
2.13.8 112 8/17/2023
2.13.7 115 8/16/2023
2.13.6 119 8/16/2023
2.13.5 127 8/8/2023
2.13.4 120 8/8/2023
2.13.3 121 8/8/2023
2.13.2 120 8/8/2023
2.13.1 123 8/8/2023