Solved

Building the basics of an MVP WinForms app.

Posted on 2010-08-16
5
457 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
[X]
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
5 Comments
 
LVL 18

Accepted Solution

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

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

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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