Solved

Building the basics of an MVP WinForms app.

Posted on 2010-08-16
5
448 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 500 total points
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Glad to help anytime
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now