patrickklaeren / autoregisterinject Goto Github PK
View Code? Open in Web Editor NEWC# Source Generator to automatically register dependencies in Microsoft Dependency Injection Service Collection
License: MIT License
C# Source Generator to automatically register dependencies in Microsoft Dependency Injection Service Collection
License: MIT License
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 GitHub Actions to build and release to Nuget.
main
releaseWe 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.
New attributes to register as keyed services, string to indicate their key.
Hi is there any plans for adding support for keyed services?
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?
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;
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.