我正在尝试基于this article实现依赖注入。 在本文中,有两种类型的游戏
a)Tic Tac Toe
b)TrivialPursuit
类型注册由以下代码完成
// Declare a Unity Container
var unityContainer = new UnityContainer();
// Register IGame so when dependecy is detected
// it provides a TrivialPursuit instance
unityContainer.RegisterType<IGame, TrivialPursuit>();
// Instance a Table class object through Unity
var table = unityContainer.Resolve<Table>();
Console.WriteLine(table.GameStatus());
Console.ReadLine();
如果我有多个游戏,并希望业务层中有一些逻辑来决定选择哪个游戏
a)Tic tac toe
b)马里奥
c)TrivialPursuit
我是否必须单独注册三个游戏。如果我需要确保它适用于所有游戏以及代码可以根据所通过的游戏选择游戏,那么最好的方法是什么。
是否有任何Vb.net示例。
// Declare a Unity Container
var unityContainer = new UnityContainer();
string game ;
if(game="tictactoe")
{
// it provides a TictacToe instance
unityContainer.RegisterType<IGame, TictacToe>();
// Instance a Table class object through Unity
var table = unityContainer.Resolve<Table>();
Console.WriteLine(table.GameStatus());
Console.ReadLine();
}
elseif(game="TrivialPursuit")
{
// Register IGame so when dependecy is detected
// it provides a TrivialPursuit instance
unityContainer.RegisterType<IGame, TrivialPursuit>();
// Instance a Table class object through Unity
var table = unityContainer.Resolve<Table>();
Console.WriteLine(table.GameStatus());
Console.ReadLine();
}
elseif(game="Mario")
{
// Register IGame so when dependecy is detected
// it provides a Mario instance
unityContainer.RegisterType<IGame, Mario>();
// Instance a Table class object through Unity
var table = unityContainer.Resolve<Table>();
Console.WriteLine(table.GameStatus());
Console.ReadLine();
}
答案 0 :(得分:0)
我建议注册所有游戏,让业务逻辑决定用户选择应创建哪种游戏。
这可能不是最好的方法,因为除此之外可能有更好的方法,但让我介绍一下我的解决方案。
解决方案背景:
示例代码:
// Put this registration in composite root
var unityContainer = new UnityContainer();
unityContainer.RegisterType<IGame, TictacToe>("tictactoe");
unityContainer.RegisterType<IGame, TrivialPursuit>("triviapursuit");
unityContainer.RegisterType<IGame, Mario>("mario");
unityContainer.RegisterType<IGameFactory, GameFactory>(unityContainer);
unityContainer.RegisterType<IGameService, GameService>();
unityContainer.RegisterType<ITable, Table>();
// Game factory implementation
public class GameFactory : IGameFactory
{
private readonly IUnityContainer _unityContainer;
public GameFactory(IUnityContainer unityContainer)
{
if( unityContainer == null )
throw new NullReferenceException("unityContainer");
_unityContainer = unityContainer;
}
// GameType should be equal to the name upon registratoin
// Ex. Game registered is tictactoe
// GameType should be also tictactoe upon resolving.
public ITable CreateGame(string gameType)
{
var gameSelected = _unityContainer.Resolve<IGame>(gameType)
return _unityContainer.Resolve<ITable>(new ResolverOverride[]
{
new ParameterOverride("IGame", gameSelected)
});
}
}
// Game service implementation
public class GameService : IGameService
{
private readonly IGameFactory _gameFactory;
public GameService(IGameFactory gameFactory)
{
if( gameFactory == null )
throw new NullReferenceException("gameFactory");
_gameFactory = gameFactory;
}
// To avoid exception for unkown gametype enforce with constant
// variable or enum depend on your choice
public void StartGame(string gameType)
{
var tableGame = _gameFactory.CreateGame(gameType);
tableGame.Play();
tableGame.GameStatus();
}
}
注意:此代码未经过测试,我也没有将其翻译成VB.net。