In a conventional MDI WinForms application, one would go like this:
public partial MainMdiForm : Form {
public MainMdiForm() { InitializeComponent(); }
private void manageCustomersToolStripMenuItem_Click(object sender, EventArgs e) {
var cmf = new CustomerManagementForm();
cmf.MdiParent = this;
cmf.Show();
}
}
So that the CustomerManagementForm
never gets out of bounds.
Using Model-View-Presenter along with Dependency Injection, the code adds an abstraction layer which actually doesn’t allow me to set the MdiParent of my view/form.
MainMdiForm
public partial MainMdiForm : Form, IMainView {
public MainMdiForm() { InitializeComponent(); }
public IMainViewUiHandler Handler { get; set; }
private void manageCustomersToolStripMenuItem_Click(object sender, EventArgs e) {
Handler.ManageCustomers();
}
}
IMainView
public interface IMainView : IView, IHasUiHandler<IMainViewUiHandler> { }
IHasUiHandler
public interface IHasUiHandler<H> where H : IUiHandler {
H Handler { get; set; }
}
IUiHandler
public interface IUiHandler { }
IMainViewUiHandler
public interface IMainViewUiHandler : IUiHandler {
void ManageCustomers();
}
MainPresenter
public class MainPresenter : Presenter<IMainView>, IMainViewUiHandler {
public MainPresenter(IMainView view
, ICustomerManagementPresenterFactory customerManagementPresenterFactory)
: base(view) {
view.Handler = this;
this.customerManagementPresenterFactory = customerManagementPresenterFactory;
}
public void ManageCustomers() {
var p = customerManagementPresenterFactory.Create();
p.ShowView();
}
private readonly ICustomerManagementPresenterFactory customerManagementPresenterFactory;
}
As you can see, there’s nowhere to set the Form.MdiParent property for the view that the presenter handles. And exposing such property might break the abstraction and tight couple it to WinForms, since no other platform uses such a hierarchy with the different user-interfaces. Although this application is destined to be a simple example of using MVP along with DI, I wish to make it as clean as possible.
I thought of creating a single interface which would use the Parent terminology, and compose my view with it only from within the Windows Forms application, so that other platforms would not inherit from this useless property.
Any idea on how I should go along with this?
4