MicrosoftDI.AddXRefactoring 0.6.1

There is a newer version of this package available.
See the version list below for details.
Install-Package MicrosoftDI.AddXRefactoring -Version 0.6.1
dotnet add package MicrosoftDI.AddXRefactoring --version 0.6.1
<PackageReference Include="MicrosoftDI.AddXRefactoring" Version="0.6.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 0.6.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MicrosoftDI.AddXRefactoring, 0.6.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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=0.6.1

// Install MicrosoftDI.AddXRefactoring as a Cake Tool
#tool nuget:?package=MicrosoftDI.AddXRefactoring&version=0.6.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 to it.

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 Add 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 Add[X]<BaseType, ClassName>
  • Refactoring will add registration into nearest RegistrationMethod using one of two heuristics:
    • If RegistrationMethod 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 RegistrationMethod 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 RegistrationMethod with [IgnoreRegistrationMethod] attribute
  • You can annotate method which does not follow convention, but you want it to be considered as RegistrationMethod with [RegistrationMethod] attribute
  • Adds missing using to RegistrationMethod

Installation

  • It can be installed in any project through nuget package
  • 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 create Github issue 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 232 2/22/2022
1.0.0 298 2/19/2022
0.6.2 74 2/19/2022
0.6.1 76 2/19/2022
0.6.0 81 2/15/2022
0.5.0 88 2/5/2022
0.4.1 91 2/4/2022
0.4.0 92 1/31/2022
0.3.0 81 1/30/2022
0.2.0 83 1/29/2022
0.1.2 89 1/27/2022
0.1.1 90 1/17/2022
0.1.0 86 1/16/2022