0
votes

Intro: I am using the MVP as a design pattern for a windows form application with some basic CRUD operations.

Requirement I want to implement an IView interface as an abstract class since it will be used by multiple views, and i can provide some common concrete implementations (like showing dialog boxes with error messages). Now my IView was implemented in the same project as the Presenter. Because of this i cannot add any Windows.Forms references out in this project as it violates the MVP pattern.

Proposed Solution: My solution was to move the IView to a different and new project. Add my win forms references here. This will allow me accomplish the above.

Problem I am just not sure if this is good design. Am i violating any rules here?

1
If you have any dependency on a particular view implementation, then you've violated the pattern. The IView interfaces should be with the Presenters. All WinForms implementations of those interfaces should be separate.JosephHirn

1 Answers

3
votes

You could declare the view interface in the Presenter project and the abstract base view class in the Views project, like so :

(Presenter project)

public interface IView 
{
  void ShowErrorMessage(string message);
}

public interface IFooView : IView
{
  string SomeField { get; set; }
  //...
  event EventHandler SomethingClicked;
  //...
}

public class FooPresenter 
{
  private readonly IFooView _view;

  public FooPresenter(IFooView view)
  {
    // inject view here
  }

  public void DoSomething()
  {
    // error case
    _view.ShowErrorMessage("some message");
  }
}

(View project)

public abstract class BaseView : IView
{
  protected void ShowErrorMessage(string message)
  {
    // implement shared behavior here
  }
}

public class FooView : BaseView, IFooView
{
  // view specific behavior here
}