-
Notifications
You must be signed in to change notification settings - Fork 6
Home
AutoRegisterInject, also referred to as ARI (pronounced AH-REE), is a C# source generator to automatically register types with Microsoft's Dependency Injection container based on one of four attributes.
[RegisterScoped]
public class Foo { }
Turns into
serviceCollection.AddScoped<Foo>();
AutoRegisterInject supports any environment where .NET Standard 2.0 can be referenced.
Install the package via Nuget, through dotnet add package AutoRegisterInject
or add a package reference.
<PackageReference Include="AutoRegisterInject" />
AutoRegisterInject should be installed into every project where you wish to use its attributes and register services.
Classes should be decorated with one of three 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>();
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.
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.