This project provides a Roslyn code refactoring, which automatically adds call to Add(Singleton|Scoped|Transient) inferred from your class declaration into a nearest DI registration method. It could be a default ConfigureServices in Startup.cs, or your custom extension method.
- You use Microsoft.Extensions.DependencyInjection to register services
- You use default
ConfigureServices
in Startup.cs and/or custom extension methods (Registration Method) to register services, with the following signature:
public static class Module
{
public static IServiceCollection RegisterServices(this IServiceCollection services)
{
return services.AddSingleton<Foo>()
...
;
}
}
- You want to modularize and split your registration method into multiple files, and keep them closer to the classes which they register OR you just want to reduce the number of keystrokes you need to register classes in a DI container
- You don't want to use tools like Scrutor for an automated assembly scanning
- Then this code refactoring can make your life a little bit easier, generating a registration method call for you 🙂
- Code Actions with AddSingleton|AddScoped|AddTransient methods with appropriate type parameters will be inferred from your class declaration
- Exact signature of an AddX will be inferred from position, where the refactoring was triggered:
- If it was triggered on a class name itself, then the refactoring will register it as
AddX<ClassName>
- If it was triggered on a base type or an interface of a class, then the refactoring will register it as
AddX<BaseType, ClassName>
- If it was triggered on a class name itself, then the refactoring will register it as
- Refactoring will add a registration into a nearest registration method using one of the two heuristics:
- If a registration method body is empty or it does not contain chain calls with a length greater than 1, then a registration will be added to the first line with a separate statement
services.AddX<IFoo, Foo>();
- If a Registration Method contains call chain of length greater than 1, then a registration will be appended to that call chain
- If a registration method body is empty or it does not contain chain calls with a length greater than 1, then a registration will be added to the first line with a separate statement
- You can annotate a method which follows the convention, but you don't want to be considered as a registration method with a
[IgnoreRegistrationMethod]
attribute - You can annotate a method which does not follow the convention, but you want it to be considered as a registration method with a
[RegistrationMethod]
attribute - Refactoring automatically adds usings to a registration method's file, if it's required
- This refactoring can be installed in any project through the nuget package . It works with Rider 2021.3 and Visual Studio 2022.
- To install it in all projects for a given solution, create Directory.Build.props file with following content:
<Project>
<ItemGroup>
<PackageReference Include="MicrosoftDI.AddXRefactoring" Version="{CurrentVersion}">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Feel free to use Github issues for questions or feature requests