A simple auto loader that scans for service classes and configure in the dependency injection provider.
Install-Package DIContextAutoLoader
Annotate your services with the ConfigureInjection attribute:
[ConfigureInjection]
public class ServiceOne
{
...
}
Optionally you can specify the lifetime. (Default is Scoped.)
[ConfigureInjection(Lifetime = InjectionLifetime.Singleton)]
public class ServiceOne
{
...
}
You can also specify the InjectionType. (Default is Auto.)
- Auto = If the class implement interfaces, DI configuration will be done by them. If not, it will be done by the implementation type;
- ByImplementationType = DI configuration will be done by implementation type;
- ByServiceType = DI configuration will be done by service/interface types;
- ByBoth = DI configuration will be done by service/interface types and by implementation type.
[ConfigureInjection(Lifetime = InjectionLifetime.Transient, InjectionType = InjectionType.ByServiceType)]
public class ServiceTwo: ISomeService
{
...
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AutoLoadServices(typeof(SomeTypeInAssembly).Assembly);
...
}
}