I have a bunch of services that use dependency injection to inject a collection of objects like this:
IRepository<Games> gamesRepo
this allows the services to use this method without supplying the this IRepository<Games> repo parameter:
GetSingleGame(this IRepository<Games> repo, GameType type) => repo.Query().Single(p => p.GameType == type); They can do it like this:
var games = GamesRepo.GetSingleGame(GameType.RPG);
because IRepository<Games> is already supplied via dependency injection.
But I have one controller that needs to use the above method without dependency injection. Is there a way to do this?
When I try to use it like I do above (var games = GamesRepo.GetSingleGame(GameType.RPG);), I get this error:
IRepository does not contain a definition for
GetSingleGame
How can I construct IRepository<Games> gamesRepo outside of using dependency injection?
usingstatement to the namespace where theGetSingleGameextension method is defined.IRepositorywithout injecting it into the controller's constructor, then you need to create it manually using thenewoperator. But it's not clear how you are callingvar games = GamesRepo.GetSingleGame(GameType.RPG);if you don't have an instance? Who isGamesRepothen? And why you can't inject it into this specific controller? What's special about it?