Autoregister dependencies (usually services and repositories) on Microsoft.Extensions.DependencyInjection services container.
Either download the package from NuGet.org or clone and compile this repository and add the "using" reference to your Startup.cs file. AutoregisterServices can then be called as an extension of IServiceCollection:
services.AutoregisterServices<TInterface,TPointer>(ServiceLifetime);-
TInterface: Interface used to "mark" any services that implement it to be picked up for autoregistration.
-
TPointer: Class used to get the assembly containing the services.
-
ServiceLifetime: Transient / Scoped / Singleton (keep in mind that this param applies to all the services marked with TInterface)
(Both classes should be implemented by the user)
(see screenshot below for more details)
The "interface hierarchy" goes: SampleRepository implements ISampleRepository, as with any normal project, then ISampleRepository implements IRepositoryPointer.
RepositoryPointer is used only as a method to get the services assembly.
Files contents:
public class SampleRepository : ISampleRepository
{
//Repository logic
}public interface ISampleRepository : IRepositoryPointer
{
//Interface methods
}public class RepositoryPointer
{
//EMPTY
}public interface IRepositoryPointer
{
//EMPTY
}