I have an interesting situation in my current project. I have a Form (View) with a Presenter and I have a Control (View) with it's own Presenter. The control presenter needs information that the form presenter has. To facilitate this, I created an intermediate interface (IMessageProvider) that the form's presenter implements and the constructor for the control's presenter takes a parameter of IMessageProvider.
Here is a basic example of the code:
public class FormPresenter: IMessageProvider
{
public FormPresenter(IFormView view, IControlView controlView)
{
new ControlPresenter(controlView, this);
}
public IMessage GetMessage()
{
return (IMessage)SomethingThisFormOwns;
}
}
public class ControlPresenter
{
private IMessageProvider _messageProvider;
public ControlPresenter(IControlView view, IMessageProvider messageProvider)
{
_messageProvider = messageProvider;
}
public void DoSomething()
{
IMessage message = _messageProvider.GetMessage();
ProcessTheMessage(message);
}
}
I like this solution. The end result is what I think is a good Inversion of Control pattern usage. I can unit test the control's presenter without specific knowledge of the form's presenter, yet in the real application, the control's presenter communicates directly with the form's presenter to get the information needed (via the IMessageProvider interface, of course).
The only thing I don't like is how I am constructing the Control Presenter. Right now, I am having the FormPresenter build the ControlPresenter and pass the needed IMessageProvider (this) into the ControlPresenter. This creates a situation where I must always provide a ControlView when creating a FormView, solely for the purpose of the ControlView being able to call a method on the FormView. So my question is:
Is it acceptable for one presenter to have knowledge of / creation rights to another presenter?
The only other solution I could come up with is to have the Form View implementation create the ControlPresenter. This prevents my FormPresenter from having knowledge of the ControlPresenter, but forces my Form View to have knowledge of the ControlPresenter. Personally, I think it's more appropriate for the View to have knowledge of the Control / Control Presenter since the control is really part of the specific Form View implementation. I think forcing the FormPresenter to know about / create a ControlPresenter is limiting the FormView implementation by requiring that particular control and I'm not sure I like that idea.
I guess it comes down to your individual situation, though. There may be times when it really is necessary for the FormPresenter to know about the ControlPresenter - most likely this would be a case where the FormPresenter is requesting information from the ControlPresenter. Even in that situation, though, I would provide an Interface to keep the FormPresenter from actually knowing it's a ControlPresenter.
What do you think? Which situation would you consider more preferable? Or better yet - can you help me find a cleaner implementation?