Is there a robust way to register dependencies in ASP.NET Core 3.1 beside adding everything into Startup class?
Grouping related dependencies into custom extension methods is a very common way to do this. ASP.NET Core already does this for many of the internal services, and you can easily expand on top of that and set them up the way you need for your application. For example to set up authentication and authorization:
public IServiceCollection AddSecurity(this IServiceCollection services)
{
services.AddAuthentication()
.AddCookie();
service.AddAuthorization(options =>
{
options.DefaultPolicy = …;
});
return services;
}
You can do the same for your application-specific services and group them logically in separate extension methods.
If you have a lot of service registrations that are very similar, you can also employ a convention-based registration e.g. using Scrutor. For example, this registers all services within a certain namespace as transient for their respective interface:
services.Scan(scan => scan
.FromAssemblyOf<Startup>()
.AddClasses(c => c.InNamespaces("MyApp.Services"))
.AsImplementedInterfaces()
.WithTransientLifetime()
);
Scrutor allows for very complex rules to scan for services, so if your services do follow some pattern, you will likely be able to come up with a rule for that.
Create a custom attribute (called AutoBindAttribute)
public class AutoBindAttribute : Attribute
{
}
Use it like bellow (Decorate all the implementations that you want to automatically bind with [AutroBind])
public interface IMyClass {}
[AutoBind]
public class MyClass : IMyClass {}
Now create an extention method for IServiceCollection
public static class ServiceCollectionExtensions
{
public static void AutoBind(this IServiceCollection source, params Assembly[] assemblies)
{
source.Scan(scan => scan.FromAssemblies(assemblies)
.AddClasses(classes => classes.WithAttribute<AutoBindAttribute>())
.AsImplementedInterfaces()
.WithTransientLifetime();
}
}
Now call it in Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AutoBind(typeof(Startup).Assembly);
}
}
Note: You can improve the ServiceCollectionExtentions
class to support all scopes such as singleton, etc. This example shows only for Transient lifetime.
Enjoy!!!