I'm pretty new to the .Net world and I faced a problem that I couldn't find a solution to and I would like you to help me with it please.
I have a generic class that implements a generic interface and when I was implementing the dependency injection I didn't know how I can make it dynamic.
For example:
services.AddSingleton<IGenericRepository<Game>, GenericRepository<Game>>();
I have multiple classes that i want to pass to the generic class/interface (Game is one of them) and I know that I have to repeat this line for every single one of them but i was wondering of there is any way to make this dynamic.
Thank you.
What you're looking for is called an "open generic" registration. You didn't say exactly what DI framework you're using, but for most DI frameworks, the syntax is something like:
services.AddSingleton(typeof(IGenericRepository<>), typeof(GenericRepository<>));
My guess is that you're using Microsoft DI framework. If so, the relevant documentation is here. Search for the word "generic".