Posts Tagged ‘winform’

Selecting a MVC/MVP Implementation for a Winforms Project

Friday, February 1st, 2008

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.