You should be able to inject factory delegates for the components instead of the actual components. NInject supports binding delegates out-of-the-box via its Bind<>().ToMethod()
.
The nice thing about such a construct is that you get the benefits of constructor injection while at the same time enables the instance (MasterEngine
in this case) to control when dependencies are instantiated.
Your constructor should look something like this:
public MasterEngine(IInputReader inputReader,
Func<MasterEngine,GraphicsDeviceManager> graphicsDeviceFactory,
Func<MasterEngine,GamerServicesComponent> gamerServicesFactory)
{
this.inputReader = inputReader;
graphicsDeviceManager = graphicsDeviceFactory(this);
Components.Add(gamerServicesFactory(this));
}
Here's how you bind the factory delegates, which imo is a tidier way:
Bind<Func<MasterEngine, GraphicsDeviceManager>>()
.ToMethod(context => engine => new GraphicsDeviceManager(engine));
Bind<Func<MasterEngine, GamerServicesComponent>>()
.ToMethod(context => engine => new GamerServicesComponent(engine));
This can also be done with delegate types:
public delegate GraphicsDeviceManager GdmFactory(MasterEngine engine);
public delegate GamerServicesComponent GscFactory(MasterEngine engine);
...
Bind<GdmFactory>()
.ToMethod(context => engine => new GraphicsDeviceManager(engine));
Bind<GscFactory>()
.ToMethod(context => engine => new GamerServicesComponent(engine));
...
public MasterEngine(IInputReader inputReader,
GdmFactory graphicsDeviceFactory,
GscFactory gamerServicesFactory)
{
...
}