MicrosoftDI.AddXRefactoring.Attributes 1.0.1

.NET Standard 2.0
NuGet\Install-Package MicrosoftDI.AddXRefactoring.Attributes -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.
dotnet add package MicrosoftDI.AddXRefactoring.Attributes --version 1.0.1
<PackageReference Include="MicrosoftDI.AddXRefactoring.Attributes" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MicrosoftDI.AddXRefactoring.Attributes --version 1.0.1
#r "nuget: MicrosoftDI.AddXRefactoring.Attributes, 1.0.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.Attributes as a Cake Addin
#addin nuget:?package=MicrosoftDI.AddXRefactoring.Attributes&version=1.0.1

// Install MicrosoftDI.AddXRefactoring.Attributes as a Cake Tool
#tool nuget:?package=MicrosoftDI.AddXRefactoring.Attributes&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

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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
1.0.1 98 2/22/2022
1.0.0 86 2/19/2022
0.6.2 79 2/19/2022
0.6.1 87 2/19/2022
0.6.0 240 2/15/2022
0.5.0 98 2/5/2022
0.4.1 97 2/4/2022
0.4.0 99 1/31/2022
0.3.0 98 1/30/2022
0.2.0 99 1/29/2022
0.1.2 107 1/27/2022
0.1.1 103 1/17/2022
0.1.0 103 1/16/2022