Register services into an IServiceCollection by tagging classes with attributes
$ dotnet add package AttributedServiceRegistrationRegister IServiceCollection services by tagging them with attributes rather than manually registering each service individually.
Provides RegisterAttributedServices() as an extension method to IServiceCollection.
Supports;
Classes with no interface inheritance.
Classes that inherit from an interface (providing the interface is named the same as the implementation class, prefixed with an 'I').
Singleton, Scoped, and Transient lifetimes.
Optional Service Key can be provided to register the service as a Keyed service.
Either register services in the calling assembly, or provide a list of assemblies to the RegisterAttributedServices method.
Example;
public interface IMySingletonService
{
...
}
[SingletonService]
public class MySingletonService : IMySingletonService
{
...
}
...
[ScopedService(12345)]
public class MyKeyedScopedService
{
...
}
[TransientService]
public class MyTransientService
{
...
}
...
myServices.RegisterAttributedServices();
Would be equivalent to;
myServices.AddSingleton<IMySingletonService, MySingletonService>();
myServices.AddKeyedScoped<MyKeyedScopedService>(12345);
myServices.AddTransient<MyTransientService>();