我一直在一个XNA项目中使用Ninject作为IOC容器,现在想将其迁移到Ninject 2.0。然而,XNA并不友好地支持依赖注入,因为某些类必须在游戏类的构造函数中实例化,但同时也必须将游戏类传递给它们的构造函数。例如:
public MyGame ()
{
this.graphicsDeviceManager = new GraphicsDeviceManager (this);
}
这篇文章讲述了一种解决方法,其中IOC容器明确知道要使用哪个实例来解析服务。
/// <summary>Initializes a new Ninject game instance</summary>
/// <param name="kernel">Kernel the game has been created by</param>
public NinjectGame (IKernel kernel)
{
Type type = this.GetType ();
if (type != typeof (Game))
{
this.bindToThis (kernel, type);
}
this.bindToThis (kernel, typeof (Game));
this.bindToThis (kernel, typeof (NinjectGame));
}
/// <summary>Binds the provided type to this instance</summary>
/// <param name="kernel">Kernel the binding will be registered to</param>
/// <param name="serviceType">Service to which this instance will be bound</param>
private void bindToThis (IKernel kernel, Type serviceType)
{
StandardBinding binding = new StandardBinding (kernel, serviceType);
IBindingTargetSyntax binder = new StandardBinder (binding);
binder.ToConstant (this);
kernel.AddBinding (binding);
}
然而,我不确定如何在Ninject 2.0中实现这一点,因为我认为等效的代码可能是什么。
if (type != typeof (Game))
{
kernel.Bind (type).ToConstant (this).InSingletonScope ();
}
kernel.Bind (typeof (Game)).ToConstant (this).InSingletonScope ();
kernel.Bind (typeof (NinjectGame)).ToConstant (this).InSingletonScope ();
仍然会产生StackOverflowException
。如果有任何想法至少在哪里继续,请告诉我们。