I've been trying to grasp the concept of Dependency Injection within .NET Core but have hit a problem. I think I'm missing the point somewhere along the line.
As an example, say I have a top level GameHandler class, then a Game class and a ScoreCard class.
My Game class has a dependency on the ScoreCard class and this needs to be registered as a transient service since each new game needs a new scorecard. I can achieve that by doing this...
public class Game
{
private readonly IScoreCard _scoreCard;
public Game(IScoreCard scoreCard)
{
_scoreCard = scoreCard;
}
}
Then I register the service in startup.cs in the ConfigureServices method
services.AddTransient<IScoreCard, ScoreCard>();
This is all good, it's a simple one to one relationship where the Game class needs a single instance of the ScoreCard class.
My GameHandler class also has a dependency on the Game class. However, in this case, I want to potentially have multiple games, probably stored in a dictionary of some sort along with a key. And I don't know how many I'll need because it depends on who is playing at a particular time so I'd need to create new Game objects whenever I need them.
If I try and do this using new, like this
Game _newGame = new Game();
it obviously doesn't work because my Game constructor now has the DI ScoreCard stuff in there and that doesn't match. So how do I create new instances of Game using DI without using new?