Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Building the basics of an MVP WinForms app.

Posted on 2010-08-16
5
Medium Priority
?
478 Views
Last Modified: 2013-12-17
I have created various folders in my WinForms project:

Models
Views
Presenters
Interfaces

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.

Thanks,
newbieweb
0
Comment
Question by:newbieweb
  • 3
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
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; }
}

or

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.

DaTribe
0
 

Author Comment

by:newbieweb
ID: 33447065
DaTribe,

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?
0
 
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.

DaTribe

0
 

Author Closing Comment

by:newbieweb
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 :)

Thanks.
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 33451848
Glad to help anytime
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

927 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