Traditionally, we register multiple services, multiple interfaces under ConfigureServices method in the Startup class. This is feasible if we have five or ten registrations. But what if we have fifty or more such registrations?
public void ConfigureServices(IServiceCollection services) {
service.AddTransient<ISQLDB, SQLDB>();
service.AddTransient<IEmployeeBusiness, EmployeeBusiness>();
service.AddTransient<IEmployeeRepository, EmployeeRepository>();
service.AddTransient<IEmployeeValidation, EmployeeValidation>();
service.AddTransient<IDepartmentBusiness, DepartmentBusiness>();
service.AddTransient<IDepartmentRepository, EmployeeRepository>();
service.AddTransient<IDepartmentValidation, EmployeeValidation>();
...
...
...
// some 50 or more
}
I want to move the registering part to another custom InjectDependency class and call this in ConfigureServices method like:
public class InjectDependency{
public InjectDependency(IServiceCollection service)
{
service.AddTransient<ISQLDB, SQLDB>();
service.AddTransient<IEmployeeBusiness, EmployeeBusiness>();
service.AddTransient<IEmployeeRepository, EmployeeRepository>();
service.AddTransient<IEmployeeValidation, EmployeeValidation>();
service.AddTransient<IDepartmentBusiness, DepartmentBusiness>();
service.AddTransient<IDepartmentRepository, EmployeeRepository>();
service.AddTransient<IDepartmentValidation, EmployeeValidation>();
...
...
...
...
}
}
Is there any way to achieve this?
IServiceCollectionreference to yourInjectDependencyclass, this should work just fine. Are you receiving an error? Are you just not sure how to call this? Note that since this is a stateless function, it may make more sense to treat it as a static method rather than a constructor.