Generic repository in ASP.NET Core without having a separate AddScoped line per table in Startup.cs?
Just use the non-generic registration overloads (the ones where you need to pass the 2 Type
objects.) Then provide the open generic types of both your interface and the implementation:
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
In your controller, add a dependency for a repository of a specific type (a closed generic type):
public HomeController(IGenericRepository<Lookup1> repository)
{
...
}
If you would like to register all IGenericRepository<>
implementations in Assembly:
services.AddAllGenericTypes(typeof(IGenericRepository<>), new[] {typeof(MyDbContext).GetTypeInfo().Assembly});
With extension from: https://gist.github.com/GetoXs/5caf0d8cfe6faa8a855c3ccef7c5a541