我正在尝试在应用程序中使用依赖注入技术,但如果此上下文需要 DI,我会感到困惑..
考虑一个接口:
public class ICardsHub {
public T GetPlayersList<T>();
}
及其实施:
public class GenericHub : ICardsHub {
LANPlayers LANPlayers;
WaNPlayers WANPlayers;
bool AlgorithmsUseTildainLambdas=false;
public GenericHub(LANPlayers _LANPlayers, WaNPlayers _WANPlayers,
bool algorithmsUseTildainLambdas=false) {
LANPlayers = _LANPlayers;
WANPlayers = _WANPlayers;
AlgorithmsUseTildainLambdas = algorithmsUseTildainLambdas;
}
public T GetPlayersList<T>() {
....
}
}
并且驱动程序类应该执行以下操作:
ICardsHub _ifCards = new GenericHub(_LANPlayers, _WANPlayers);
listOfPlayers = _ifCards.GetPlayersList<List<string>>();
现在,我们还有两个实现ICardsHub
;RedHub
并且BlackHub
稍后在驱动程序类中实例化。
问题
- Unity IoC/DI如何使这种情况受益?
- 如果striping out method parameters from the interface and injecting them to overload constructor解决了问题,那么 DI 容器使用的合适场景是什么?