Building the basics of an MVP WinForms app.

Posted on 2010-08-16
Last Modified: 2013-12-17
I have created various folders in my WinForms project:


and I am ready to build an interface for my first View, let's call it the DispatcherView dialog.

What should the view's interface inherit from?  Form?

Or do I make the View inherit from Form then also implement the IDispatcherView?

Also, DispatcherPresenter does not inherit from any object or implement any interface.  Right?

How about the DispatcherModel?

Please make suggestions.

Question by:newbieweb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 18

Accepted Solution

Richard Lee earned 500 total points
ID: 33446886
The idea of using the MVP is separation of concerns which bring along with it other benefits. One of the issues is not being able to test the view in the context of unit tests therefore your form should implement the interface and leave the interface be.

public class DispatcherForm : Form, IDispatcherView

The DispatcherPresenter's job is to handle events passed over from the view. There must be some way that the presenter gains knowledge of the view. Here are a few ways this could be done - by no means the only ways.

public class DispatcherPresenter
    // This is public allowing the View/Form to assign itself to
    // this property after it has created an instance of the presenter.
    public IDispatcherView View { get; set; }


public class DispatcherPresenter
    private IDispatcherView View { get; set; }

    // Pass the view via the constructor and do not
    // allow changing the view throughout the lifetime
    // of the presenter unless through the presenter methods
    public DispatcherPresenter(IDispatacherView view)
       View = view;

The consideration of passing multiple views to the presenter should be a considerations, especially for complex applications or event driven applications. Think of the observer pattern and updating the UI at many places automatically as the widgets have subscribed to an event.

The Model is the data used by the view plus the model interacts with the business and/or data layer. You can have something as follows:

public class DispatcherModel
   public Guid Id { get; set; }
   public bool Dispatached { get; set; }

   public void RefreshDispatacherObject()

    public static DispatcherModel GetDispatcherObject(Guid id)

Hope this helps.


Author Comment

ID: 33447065

Thanks. That helps.  For now, multiple Views is the last of my concerns.

I prefer passing in the IView to the Presenter's constuctor.  Is this "inversion of control"?

Do I also need a Dependency Injection tool like the Spring Framework?  I never really understood why I needed more than what you have already described.

- The View implements an IView
- The View constructs the Presenter
- The Presenter gets the IView on construction
- The View can call the Presenter through a private member variable
- The Presenter can call the View through a private member variable

What am I missing?
LVL 18

Assisted Solution

by:Richard Lee
Richard Lee earned 500 total points
ID: 33447334
You have the basics for constructing an application & it will work, however there are more concerns. When building large applications you will face several obstacles which include bugs, change, delivery of features on time and to specification, etc. To combat these problems ways of working like SCRUM and test driven development (TDD) have been developed.

Test driven development is designed to allow features to be developed to specification and no more, plus provides the white box testing confidence (unit tests) that ensures that bugs are identified and new features can be implemented with confidence (no fear of older code being broken). Test driven development is tricky and requires fake objects to be created to test against; this is satisfied by mocking framework and mocking framework rely heavily on interfaces.

Now you mentioned dependency injection & spring framework. These are handy for saving time when constructing object that have dependencies such as your presenter; they can do more however. You can now inject different objects based on whether you are running in debug, release or test mode. You can create a mock view or use a mocking framework to do this automatically and inject it during test into the presenter and test the presenter methods & properties easily. Why would you want to do this? Suppose your view accessing resources which are not available during test, then the tests will always fail. Another one is suppose during method calls to the n depth

  Presentation                Business Layer                Data Layer
View --> Presenter --> Business Objects --> Data Access Objects

you wish to test a feature within your presentation or business but the business layer is using a data object accessing the database and its not required. Then dependency injection can be applied to automatically substitute a mock object during testing. Another benefit of dependency injection frameworks is that they can be configured externally which gives the benefits of changing behaviour at run time.

I hope this is clear.



Author Closing Comment

ID: 33447391
Finally, I got an explanation of what DI tools can do for me.  At the moment, I am the sole developer on a tiny addition to a legacy program and ned it finished ASAP.  Many of the great things I can do with MVP will have to wait until my company grows by at least one person :)

LVL 18

Expert Comment

by:Richard Lee
ID: 33451848
Glad to help anytime

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question