3

Having almost no architectural experience I'm trying to design a DRY KISS solution for the .NET 4 platform taking an MVP approach that will eventually be implemented as a Desktop (WinForms) and Web (ASP.NET or Silverlight) product. I did some MVC, MVVM work in the past but for some reason I'm having difficulties trying to wrap my head around this particular one so in an effort to get a grip of the pattern I've decided to start with the simplest sample and to ask you guys for some help.

So assuming a quite simple Model as follows (in practice it'd most definitely be a WCF call):

internal class Person
{
    internal string FirstName { get; set; }
    internal string LastName { get; set; }
    internal DateTime Born { get; set; }
}

public class People
{
    private readonly List<Person> _people = new List<Person>();
    public List<Person> People { get { return _people; } }
}

I was wondering:

  1. What would be the most generic way to implement its corresponding View/Presenter triad (and helpers) for say, a Console and a Forms UI?
  2. Which of them should be declared as interfaces and which as abstract classes?
  3. Are commands always the recommended way of communication between layers?

And finally: by any chance is there a well-docummented, testeable, light framework to achieve just that?

Nano Taboada
  • 4,148
  • 11
  • 61
  • 89
  • You should familiarize yourselve with MVP visiting links that I've posted here http://stackoverflow.com/q/5381847/575659 – dantuch Aug 02 '11 at 18:21
  • @dantuch: Thanks for the comment - I've checked those and found them useful but what I'm actually looking is a more implementation-oriented approach (think: conventions, code samples, frameworks) rather than the conceptual one. – Nano Taboada Aug 02 '11 at 18:46

1 Answers1

5

I've written a number of apps that require a GUI and a winforms UI, the approach I have typically taken to implementing MVP is to create a generic view interface (you can subclass this for more specific views) and a controllerbase class which is given a view. You can then create different view implementations which inherit from the IView (or more specific view) interface

interface IView
{
    event EventHandler Shown;
    event EventHandler Closed;

    void ShowView(IView parentView);
    void CloseView();
}

class ControllerBase<T> where T: IView
{
    private T _view;

    public ControllerBase(T view)
    {
        _view = view;
    }

    public T View
    {
        get { return _view; }
    }

    public void ShowView(IView owner)
    {
        _view.ShowView(owner);
    }

    public void ShowView()
    {
        ShowView(null);
    }

    public void CloseView()
    {
        _view.CloseView();
    }
}

Heres an example of how it would work in a specific case

interface IPersonView: IView
{
    event EventHandler OnChangeName;
    string Name { get; set; }
}

class PersonController: ControllerBase<IPersonView>
{
    public PersonController(string name,IPersonView view) : base(view)
    {
        View.Name = name;
        View.OnChangeName += HandlerFunction;
    }

    private void HandlerFunction(object sender, EventArgs e)
    {
        //logic to deal with changing name here
    }
}

To implement this view in winforms, just make sure your form inherits from IPersonView and implements all the required properties/events and you're good to go. To instantiate the view/controller you'd do something like the following

PersonForm form = new PersonForm();
PersonController controller = new PersonController("jim",form);
controller.ShowView();
Glenn
  • 111
  • 3
  • Thanks for the contribution Glenn -- to avoid the inevitable MVP/MVC confusion would you mind elaborating about the main difference between those? – Nano Taboada Sep 01 '11 at 18:48
  • 1
    With MVC you have a view which invokes actions on a controller, the controller then changes the model which fires off events to the view. MVP is different in that the model and view never directly communicate, all view events go to the presenter which updates the model, and then the presenter is also responsible for updating the view again. – Glenn Sep 01 '11 at 22:16
  • 1
    I'm not following why you named the presenter class `ContollerBase` (shouldn't it be `PresenterBase`?) and how is it communicating with the Model. – Nano Taboada Sep 04 '11 at 19:12