Giter Site home page Giter Site logo

patrickklaeren / autoregisterinject Goto Github PK

View Code? Open in Web Editor NEW
54.0 54.0 4.0 81 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

Hey there, the name's Patrick ๐Ÿ‘‹

Metrics

autoregisterinject's People

Contributors

patrickklaeren avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

autoregisterinject's Issues

vNext roadmap

Tracking issue for vNext

This list represents issues and ideas to be implemented in vNext. Below will not necessarily be released at the same time, but rather over a longer term.

  • Add ability to ignore interfaces
  • Add ability to auto register hosted services

Support Generated Interfaces

We are actually generating our interfaces (we're actually using multiple generators together).

[AutoInterface]
[AutoConstruct]
public partial class ItemsService : IItemsService
{
   // removed for brevity
}

However, AutoRegisterInject isn't finding our generated interfaces.

Both:

[AutoInterface]
[AutoConstruct]
[RegisterTransient]
public partial class ItemsService : IItemsService
{
   // removed for brevity
}

And:

[AutoInterface]
[AutoConstruct]
[RegisterTransient(typeof(IItemsService))]
public partial class ItemsService : IItemsService
{
   // removed for brevity
}

Produce:

// <auto-generated>
//     Automatically generated by AutoRegisterInject.
//     Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
using Microsoft.Extensions.DependencyInjection;
public static class AutoRegisterInjectServiceCollectionExtension
{
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromServices(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        return AutoRegister(serviceCollection);
    }

    internal static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegister(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        serviceCollection.AddTransient<Services.ItemsService>();
        return serviceCollection;
    }
}

Not sure whether it will be possible to address this due to the use of multiple generators. Was hoping to not have to chose one generator in favor of another when they do not compete in equivalent features.

Keyed services

Hi is there any plans for adding support for keyed services?

Support TryAdd methods

Instead of using methods such as AddTransient and AddSingleton, can it use TryAddTransient and TryAddSingleton (including the other possible lifetimes)? Or possibly make it configurable?

Doesn't register as abstract service, only as implementation.

I have this class:

public sealed class Settings
{
   public required string ConnectionString { get; init; }
}

I have a fluent validator like so:

[RegisterTransient(typeof(IValidator<Settings>))]
public class SettingsValidator : AbstractValidator<Settings>
{
   public SettingsValidator() =>
      RuleFor(static settings => settings.ConnectionString)
        .NotEmpty()
        .WithMessage($"{nameof(Settings.ConnectionString)} must have a valid connection string.")
        .Must(BeAbleToConnect)
        .WithMessage($"{nameof(Settings.ConnectionString)} is not valid or cannot connect to the database.");

   static bool BeAbleToConnect(string connectionString)
   {
      try
      {
         using SqlConnection connection = new (connectionString);

         connection.Open();

         return true;
      }
      catch { return false; }
   }
}

I expect it to register like so:

serviceCollection.AddSingleton<IValidator<Settings>, SettingsValidator>();

However, it always registers it like this instead:

// <auto-generated>
//     Automatically generated by AutoRegisterInject.
//     Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
using Microsoft.Extensions.DependencyInjection;
public static class AutoRegisterInjectServiceCollectionExtension
{
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromMyProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        return AutoRegister(serviceCollection);
    }

    internal static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegister(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        serviceCollection.AddTransient<MyProject.SettingsValidator>();
        return serviceCollection;
    }
}

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.