Selecting a MVC/MVP Implementation for a Winforms Project

I'm not a computer scientist. I'm also not one of the many über programmers that create and analyze software frameworks and techniques. I simply design and develop software that attempts to meet my customer's needs. To that end I'm always looking for the best tools available to get the job done.

Jeremy Miller states the importance of design patterns well:

I know many people blow off design patterns as ivory tower twaddle and silly jargon, but I think they're very important in regards to designing user interface code. Design patterns give us a common vocabulary that we can use in design discussions. A study of patterns opens up the accumulated wisdom of developers who have come before us.

My opinion: You don't need to be a rocket scientist to understand design patterns. Most are just common sense. Complex patterns are designed to solve complex problems. Design patterns should be thought of as a tool that you use just like any other. Don't let the 'ivory tower twaddle' scare you away.

I think most people would agree that one of the key components to creating a successful software product is quality. I've developed .NET applications in the past and have experienced the difficulty of testing and maintaining the functionality of Winform forms and components when they are created with the default Visual Studio tools. If you're not careful, here's what you end up with:

Spaghetti

Photo hat tip: Josh Smith from Using MVC to Unit Test WPF Applications -- a very good and relevant article.

I should note here that the development of software for medical devices already has rigorous verification and validation processes to ensure quality. See FDA Good Manufacturing Practice (GMP - Quality System Regulation) subpart C–Design Control (§ 820.30 sections f & g). However, these requirements do not preclude the need for development techniques that make the software more robust and maintainable. On the contrary, the higher the software quality, the easier it is to meet these standards.

I've recently spent some time trying to select a GUI architecture that will allow us to create a more robust unit testing environment. This is why I started looking at Model-View-Controller (MVC) and Model-View-Presenter (MVP) design patterns. The need for these types of design patterns is twofold:

  1. Separation of Concerns (SoC), which allows for
  2. Improved Testability.

There are many articles and blog posts that describe MVC, MVP, and their numerous variations. These techniques have been around for many years but the current corner-stone comes from these Martin Fowler articles:

Once you understand these concepts you can start to grasp the trade-offs of all of the available MVC/MVP flavors. If you're like me, one of the problems you'll run into is that there are so many different approaches (and opinions) that you'll be left wondering which is best to implement. The only advice you'll get is that it's a matter of choice. Great, thanks! From the article above, Josh puts it best:

If you put ten software architects into a room and have them discuss what the Model-View-Controller pattern is, you will end up with twelve different opinions.

This is when you turn from the theory and start looking for concrete implementations that might be suitable for your situation. Microsoft has released an ASP.NET MVC Framework as part of VS2008, but all of the Winform code samples I found were part of either blog posts or articles.

As you look at the different implementations (and relevant snippets), you quickly realize that following these design patterns requires significantly more work than just adding your application's logic directly to the IDE generated delegates. The additional work is expected and is the trade-off for improved testability.

That's fine, and worth it, but it's still time and money. We do not have the resources, or experience, to undertake a full Test-Driven Development (TDD) effort. We will implement MVC/MVP on only the displays that we feel are the most vulnerable.

I'm not going to list all of the candidate examples I looked at. I will mention that Jeremy's series of articles (here) dig deep into these issues and have lots of good code examples. Each approach has their pros and cons, just like the one I'll present here. We'll try to use it, but may end up with something else in the end. As we become more experienced, I suspect we'll evolve into a customized solution that best meets our needs.

A Promising Candidate:

Implementing the Passive View -- a Derivative of the Model-View-Control by Matthew Cochran.

Passive View — a Derivative of the Model-View-Control

This hybrid approach appealed to me for a couple of reasons. The first is that I spent several years doing Swing development, which uses a MVC that also allows multiple simultaneous views of a single model. I also like the event driven approach, which is not only heavily used in Java, but is also well supported in .NET. In any case, the View is passive and all of the important functional logic is centralized in the Controller class which can be easily tested with real or mock Model and View objects.

Matthew has done a good job of providing support generic classes that make implementation somewhat less cumbersome. The MvcControlBase class provides generic Control-View wiring while ChangeRequestEvents manages all events in a single class.

The project download provided by the article is a VS2008 solution. We're still using VS2005, but I was able to back-port the project to VS2005 with only minor modifications that had no effect on functionality. The VS2005 project is available for download here:

MVC-PV-2005.zip (23K)

Final Thoughts:

I see adoption of MVC/MCP methodology for GUI development as a critical component for improvement in software reliability, quality, and long-term maintainability. Also, structuring the application side with MVC/MVP is only half the battle. Developing an effective testing strategy must go along with it in order to achieve these objectives. Until Microsoft provides an integrated Winforms MVC solution like they did for ASP.NET, we'll just have to continue to roll our own.

I'd like to hear about your experiences, suggestions, and recommendations on any of these topics.

Thanks!

UPDATE (6-May-08): Here's a good MVC article by Jeff Atwood: Understanding Model-View-Controller

UPDATE (16-Jun-08): Another reference: Everything You Wanted To Know About MVC and MVP But Were Afraid To Ask

UPDATE (11-Sep-08): Still more: MVC vs. MVP: A Hillbilly's Journey.

Tags: , , , ,

11 Responses to “Selecting a MVC/MVP Implementation for a Winforms Project”

  1. Richard says:

    Good post. It’s ironic how MVC dates back to Smalltalk-80 and how with each new application framework, we always seem to have to relearn how useful a design pattern it is.

  2. Floroskop says:

    Hello!
    I think this try.

  3. Marom says:

    I am hearing more and more about MVPC pattern that split the presenter into two parts.

    http://aviadezra.blogspot.com/2007/07/twisting-mvp-triad-say-hello-to-mvpc.html
    Although, I am not a big fane of the controller in this article, it shows how the ‘Passive View’ and ‘Supervising Controller’ can be combined and it has C# source code attached.

    http://www.martinhunter.co.nz/articles/MVPC.pdf
    This is also new approach of MVP – here the controller makes more sense.

  4. Thanks for this post. It reflects *exactly* the decisions and thoughts I am currently making/having. Your post is highly infomrative and I will add a link to it on my blog.

  5. Baliou says:

    There is not a lot of MVC winforms implementation on the web, even if the principles are well explained.
    Do you have a link for real world implementation of this pattern?

    What do you think of these implementations on codeplex?

    http://koosserymvcwin.codeplex.com/
    http://wrails.codeplex.com/
    http://winformsmvc.codeplex.com/

    Does any one has a feedback of one of these implementation?

    Regards

    Regards

  6. […] “Selecting a MVC/MVP Implementation for a Winforms Project” – Weblog posting about MVC. […]

  7. […] This post was mentioned on Twitter by onion_soup. onion_soup said: http://tinyurl.com/2dnp8ds Selecting a MVC/MVP Implementation for a Winforms Project | Bob on Medical Device Software(RDN-CONSULTING.COM) […]

  8. leye says:

    Hi, very interest article
    If Name or Age change in catview1, catwiew2 is notified and display new Name and Age values , OK

    But if I’ve a control ( combox ) in catview1 and I’ve another combox in catview2 displaying the same data ( List.

    So, I want to change the selected index of combobox in catview1 and I don’t want to notify catwiew2 combobox.

    How doing this ?

  9. nick says:

    Let’s say you want to save the cat info to a database. You add a method to the model to do just that. My question is how do you hook up the call from the view to the model? Do you add another class similar to ChangeRequestEvents.cs to handle method calls? Or do you just use ChangeRequestEvents.cs for both?

    thanks

  10. […] Selecting a MVC/MVP Implementation for a Winforms Project | Bob on Medical Device Software. This entry was posted on lunedì, luglio 18th, 2011 at 09:51and is filed under .NET, Informatica. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. […]

  11. Selecting a MVC/MVP Implementation for a Winforms Project | Bob on Medical Device Software…

    Thank you for submitting this cool story – Trackback from Blue Ray Plus – Latest Technology News…

Leave a Reply