Separation of concerns (SoC)
Dependency injunction registrtered in ConfigureServices (method of startup class) consist of different DI's like Repository, Fluent Validations etc.
How would I go about separating DI registration into separate files (as shown below)
The ConfigureServices method is a public method on your Startup class that takes an IServiceCollection instance as a parameter and optionally returns an IServiceProvider . The ConfigureServices method is called before Configure .
Use ConfigureServices method to add services to the container. Use Configure method to configure the HTTP request pipeline.
Startup class conventions: The app can define multiple Startup classes for different environments. The appropriate Startup class is selected at runtime. The class whose name suffix matches the current environment is prioritized.
The ConfigureServices method Called by the host before the Configure method to configure the app's services.
Create an extension method to hold any additional configuration you want
public static class MyExtensions {
public static IServiceCollection AddFluentValidation(this IServiceCollection services) {
//...add services
return services;
}
}
And then called in the ConfigureServices
in Startup
public void ConfigureServices(IServiceCollection services) {
//...
services.AddFluentValidation();
services.AddRepository();
//...
}
The use of extension methods for populating the services collection is commonly used by the framework and 3rd party extensions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With