HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Handling Parent-Child Relationship using MVP within a WinForms Application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingapplicationparentmvpwinformswithinusingchildrelationship

Problem

I recently asked on Programmers:

How should one handle a Parent-Child relationship using MVP?

Based on the comments received, I tried this:

IParentView

public interface IParentView { }


IChildView

public interface IChildView where V : IParentView {
    void Show(V parentView);
}


IChildPresenter

public interface IChildPresenter where V : IParentView {
    void ShowView(V parentView);
}


CustomerManagementPresenter

public class CustomerManagementPresenter : Presenter
    , ICustomerManagementUiHandler
    , IChildViewPresenter {
    public CustomerManagementPresenter(ICustomerManagementView view
        , ICustomerManagementService service) : base(view) {
        view.Handler = this;
        if (service == null) throw new ArgumentNullException("service");
        customerManagementService = service;
    }

    public void LoadCustomers() {
        var customers = customerManagementService.GetCustomers();
        View.ViewModel.Customers = customers;
    }

    public void ShowView(IMainView parentView) { View.Show(parentView); }

    private readonly ICustomerManagementService customerManagementService;
}


IMainView

public interface IMainView : IView, IParentView, IHasUiHandler { }


MainPresenter

public class MainPresenter : Presenter, IMainViewUiHandler {
    public MainPresenter(IMainView view, ICustomerManagementPresenterFactory factory)
        : base(view) {
        view.Handler = this;
        if (factory == null) throw new ArgumentNullException("factory");
        this.customerManagementPresenterFactory = factory;
    }

    public void ManageCustomers() {
        var p = customerManagementPresenterFactory.Create();
        var parent = this.View;
        p.ShowView(parent);
    }
}


CustomerManagementForm

```
public partial class CustomerManagementForm : Form, IMainView {
public CustomerManagementForm(CustomerManagementViewModel viewModel) {
InitializeComponent();

if (viewModel ==

Solution

I'm no expert in design patterns, but I fail to see the benefit in having a completely empty interface.

public interface IParentView { }


Interfaces do two things for us.

  • They enforce a contract. Anything implementing this interface must have this specific public api. Which allows for...



  • The ability to code against an expected api, not an implementation.



While you've done a good job of coding against IParentView instead of IChildView, what does the parent view get us? Nothing as far as I can tell. There's no real contract being made here, so there's no reason you couldn't have coded directly against IChildView. (Which, at that point, would not be a good name.)

But don't take just my word for it, 24 people on stack overflow agree that this pattern is a code smell.

Code Snippets

public interface IParentView { }

Context

StackExchange Code Review Q#63886, answer score: 3

Revisions (0)

No revisions yet.