Hi Ian,
I'm having trouble instantiating a Form that implements multiple view
interfaces. I set it up so that my Form has a property-injected
presenter for each view that it implements. The presenter has a
constructor-injected view interface.
When my Form only implemented one view interface, everything was
correctly created. Code:
// binding and app start
kernel.Bind<IMainView>().To<MainForm>().InSingletonScope();
this.MainForm = kernel.Get<IMainView>() as Form;
// presenter
public class MainViewPresenter
{
private IMainView view;
public MainViewPresenter(IMainView view)
{
this.view = view;
}
}
// form
public class MainForm: Form, IMainView
{
private MainViewPresenter presenter;
[Inject]
public MainViewPresenter
{
set
{
this.presenter = value;
}
}
}
This works fine. The kernel builds a MainForm, and then property-
injects a presenter. Because IMainView was bound to MainForm as a
singleton, the presenter's constructor dependency on IMainView can be
resolved.
If MainForm implements multiple views, I get a cyclical dependency
ActivationException.
kernel.Bind<MainForm>().ToSelf.InSingletonScope();
kernel.Bind<IStatisticsView>().ToMethod(context =>
context.Kernel.Get<MainForm>());
this.MainForm = kernel.Get<MainForm>();
StatisticsViewPresenter has a constructor dependency on
IStatisticsView, so Ninject should resolve this by calling the method
kernel.Get<MainForm>(). Because MainForm is bound as a singleton and
because it doesn't need a StatisticsViewPresenter until *after* the
MainForm has been created (since it's property injected), it should
return the current instance of MainForm. But it appears to try to
build a new instance of MainForm; it seems like Ninject doesn't
consider an object instantiated until its properties have been
injected--else, why would context.Kernel.Get<MainForm>() not give me
the already-created (but properties have not yet been injected)
instance of MainForm?
Is there a way to resolve this? I could have the presenter manually
inject itself into the view inside the presenter's constructor, but
then I have to remember to inject every time I make a new presenter
class.