GithubHelp home page GithubHelp logo

patrickklaeren / autoregisterinject Goto Github PK

View Code? Open in Web Editor NEW
67.0 2.0 5.0 92 KB

C# Source Generator to automatically register dependencies in Microsoft Dependency Injection Service Collection

License: MIT License

C# 100.00%
csharp dependency-injection microsoft source-generator

autoregisterinject's Introduction

AutoRegisterInject

AutoRegisterInject, also referred to as ARI, is a C# source generator that will automatically create Microsoft.Extensions.DependencyInjection registrations for types marked with attributes.

This is a compile time alternative to reflection/assembly scanning for your injections or manually adding to the ServiceCollection every time a new type needs to be registered.

For example:

namespace MyProject;

[RegisterScoped]
public class Foo { }

will automatically generate an extension method called AutoRegister() for IServiceProvider, that registers Foo, as scoped.

internal IServiceCollection AutoRegister(this IServiceCollection serviceCollection)
{
    serviceCollection.AddScoped<Foo>();
    return serviceCollection;
}

In larger projects, dependency injection registration becomes tedious and in team situations can lead to merge conflicts which can be easily avoided.

AutoRegisterInject moves the responsibility of service registration to the owning type rather than external service collection configuration, giving control and oversight of the type that is going to be registered with the container.

Installation

Install the Nuget package, and start decorating classes with ARI attributes.

Use dotnet add package AutoRegisterInject or add a package reference manually:

<PackageReference Include="AutoRegisterInject" />

Usage

Classes should be decorated with one of four attributes:

  • [RegisterScoped]
  • [RegisterSingleton]
  • [RegisterTransient]
  • [RegisterHostedService]

Variants for keyed and the service Try register pattern are also available:

  • [TryRegisterScoped]
  • [TryRegisterSingleton]
  • [TryRegisterTransient]
  • [RegisterKeyedScoped]
  • [RegisterKeyedSingleton]
  • [RegisterKeyedTransient]

Each keyed attribute has a Try counterpart.

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>();

Register as interface

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.

Multiple assemblies

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.

Ignoring interfaces

By default ARI will register a type with all the interfaces it implements, however this excludes System.IDisposable and its IAsyncDisposable counterpart.

You can ignore interfaces by telling ARI to explicitly register with only declared interfaces in the given attributes:

public interface IFoo { }
public interface IBar { }
[RegisterScoped(typeof(IBar))]
public class Foo;

this will result in Foo ONLY being registered as IBar and the following output:

serviceCollection.AddTransient<IBar, Foo>();

IFoo will be ignored entirely.

Where you want to register as multiple interfaces, you can pass multiple types.

[RegisterScoped(typeof(IBar), typeof(IFoo))]
public class Foo;

This works for all applicable attributes.

License

AutoRegisterInject is MIT licensed. Do with it what you please under the terms of MIT.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.