使用微软unity的依赖注入
本文关键字:依赖 注入 unity 微软 | 更新日期: 2023-09-27 18:14:17
我正在尝试基于这篇文章实现依赖注入。在本文中有两种类型的游戏:
a)井字游戏
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)井字棋
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();
}
我建议注册所有游戏,让业务逻辑根据用户的选择来决定应该创建什么游戏。
这可能不是最好的方法,因为除此之外可能还有更好的方法,但让我介绍一下我的解决方案。
解决方案上下文:
- 我注册了所有具有独特名称的游戏,如tictactoe, trivaiapuruit和马里奥在unitycontainer中注册。
- 我创建了一个工厂来创建基于类型和注入的游戏Unitycontainer。"I think factory is way to go"
- 我添加了Table中实现的Table,并注册了它,并使用Table解决了这个问题,这样如果你以后决定用不同的实现来装饰它,它会给你灵活性
- 我创建了一个服务来执行业务逻辑。
示例代码:
// 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