MicrosoftDI.AddXRefactoring 1.0.1

dotnet add package MicrosoftDI.AddXRefactoring --version 1.0.1
NuGet\Install-Package MicrosoftDI.AddXRefactoring -Version 1.0.1
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="MicrosoftDI.AddXRefactoring" Version="1.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MicrosoftDI.AddXRefactoring --version 1.0.1
#r "nuget: MicrosoftDI.AddXRefactoring, 1.0.1"
#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 MicrosoftDI.AddXRefactoring as a Cake Addin
#addin nuget:?package=MicrosoftDI.AddXRefactoring&version=1.0.1

// Install MicrosoftDI.AddXRefactoring as a Cake Tool
#tool nuget:?package=MicrosoftDI.AddXRefactoring&version=1.0.1

Nuget codecov

Microsoft DI AddX Refactoring Provider

This project provides Roslyn code refactoring, which automatically adds Add(Singleton|Scoped|Transient) inferred from your class declaration into nearest DI registration method. It could be default ConfigureServices in Startup.cs, or your custom extension method.

Screenshot_20220219_203218 Screenshot_20220219_202805

Assumptions made

  1. You use Microsoft.Extensions.DependencyInjection to register services
  2. You use default ConfigureServices in Startup.cs and/or custom extension methods (Registration Method) to register services with following signature:
public static class Module 
{
    public static IServiceCollection RegisterServices(this IServiceCollection services)
    {
        return services.AddSingleton<Foo>()
            ...
            ;
    }
}
  1. You want to modularize and split your registration method into multiple files, and keep them closer to classes which they register OR you just want to reduce number of keystrokes you need to register class in DI container
  2. You don't want to use tools like Scrutor for automated assembly scanning
  3. Then this code refactoring can make your life a little bit easier, generating registration method call for you 🙂

Features

  • Code Actions with AddSingleton|AddScoped|AddTransient methods with appropriate type parameters will be inferred from your class declaration
  • Exact signature of AddX will be inferred from position, where refactoring was triggered:
    • If it was triggered on class name itself, then refactoring will register it as AddX<ClassName>
    • If it was triggered on a base type or an interface of a class, then refactoring will register it as AddX<BaseType, ClassName>
  • Refactoring will add registration into nearest registration method using one of two heuristics:
    • If registration method body is empty or it does not contains chain calls with length greater than 1, then registration will be added onto first line with separate statement services.AddX<IFoo, Foo>();
    • If Registration Method contains call chain of length greater than 1, then registration will be appended to that call chain
  • You can annotate method which follows convention, but you don't want to be considered as registration method with [IgnoreRegistrationMethod] attribute
  • You can annotate method which does not follow convention, but you want it to be considered as registration method with [RegistrationMethod] attribute
  • Adds using to registration method's file, if it's required

Installation

  • It can be installed in any project through 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>

Contributing

Feel free to use Github issues for questions or feature requests

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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
1.0.1 1,364 2/22/2022
1.0.0 705 2/19/2022
0.6.2 453 2/19/2022
0.6.1 468 2/19/2022
0.6.0 425 2/15/2022
0.5.0 682 2/5/2022
0.4.1 708 2/4/2022
0.4.0 756 1/31/2022
0.3.0 670 1/30/2022
0.2.0 650 1/29/2022
0.1.2 690 1/27/2022
0.1.1 707 1/17/2022
0.1.0 677 1/16/2022