I am a beginner programmer and I got some class design problems. I want to have Load()
method in the game class that is quite universal and polymorphic with dependency injection. Example follows:
Lets say I have a class:
public class MyGame {
public IGameData _data;
public ISolver _solver;
public ILoader _loader;
public MyGame(ILoader loader, ISolver solver) {
_loader = loader;
_solver = solver;
}
public Load() {
/*
The loader might need a param or not,
how can I make the method Dependency Injection friendly
*/
_data = _loader.Load();
}
}
The problem is that I would like to have FileLoader()
, NetworkLoader()
and etc. classes. Each of those requires different parameters. One solution I got is to pass the parameter when I am constructing the concrete Loader:
var fileLoader = new FileLoader('somegamestate.txt');
var someSolver = new SomeSolver();
var game = new MyGame(fileLoader, someSolver);
But such a design does not seem to work well with C# Unity dependency injection framework as I it is logically wrong to bind filename to FileLoader
in the system as a whole.
I though on using factory, but that did not satisfy my soul :)
Do you have some ideas on the design and how it should be done?
Factory example:
public class MyGameFactory {
public ISolver _solver;
public MyGameFactory(ISolver solver){
_solver = solver;
}
public MyGame MakeGame(ILoader loader) {
return new MyGame(loader, _solver);
}
}
P.S. I am coding in C#.