I am using Ninject as an IOC for an XNA project and want to port it to Ninject 2.0. However, XNA is not injection-friendly, as certain classes must be created in the game class constructor, but must also pass the game class to their constructors. For instance:
public MyGame ()
{
this.graphicsDeviceManager = new GraphicsDeviceManager (this);
}
The article here describes one interaction where the IOC container is explicitly informed about which instance is used to resolve the service.
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));
}
private void bindToThis (IKernel kernel, Type serviceType)
{
StandardBinding binding = new StandardBinding (kernel, serviceType);
IBindingTargetSyntax binder = new StandardBinder (binding);
binder.ToConstant (this);
kernel.AddBinding (binding);
}
However, I'm not sure how to do this in Ninject 2.0, since I think this is equivalent code
if (type != typeof (Game))
{
kernel.Bind (type).ToConstant (this).InSingletonScope ();
}
kernel.Bind (typeof (Game)).ToConstant (this).InSingletonScope ();
kernel.Bind (typeof (NinjectGame)).ToConstant (this).InSingletonScope ();
still creating a StackOverflowException. Any thoughts, at least about where you can come from here, will be appreciated.