Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Building the basics of an MVP WinForms app.

Posted on 2010-08-16
Medium Priority
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 2000 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 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