Implementing the Passive View Pattern in WinForms (formerly Model-View-Presenter)

What a treat it is. We implemented a strict Passive View (MVP) pattern using WinForms, complete with NUnit tests and the whole enchilada (or shebang, whichever you prefer).

I tried to do the interleaved code thing here, and the interface sucks for that somehow.  I want to attached a zip with the project in it, but WordPress won’t allow the filetype.  Leave me a comment and I will e-mail the zip to you, or wait until I convert if all to a pdf(?).

How we did it:

Sketch a View (on paper)

OK, Imagine that we have a simple login form here; UsernameTextBox, PasswordTextBox, a LoginButton and a StatusMessageLabel.  Very dull, but a nice short example with all the bits and pieces..

Create a first cut at an interface that reflects the functionality of the View:

What we have in this case is an interface called ILoginView.  It has a declaration of a delegate for the LoginButton, get/set properties for Username and Password, a set property for StatusMessage, and a LoginClick event declaration.

Start writing tests that exercise the interface:

There are tests to validate that the Presenter can exercise the View through the interface.  I use a stub view that is just a class that implements ILoginView.

Create the Presenter class to satisfy the tests via the interface..

Lather, rinse, repeat.

Once you have the View’s interface implemented in tests, and you have near 100% coverage, now you can create your WinForm View (implementing View’s interface); and plug in the UI functionality.

Advertisements

7 thoughts on “Implementing the Passive View Pattern in WinForms (formerly Model-View-Presenter)

  1. that would be very awesome, if you had at least some screen shots of the code and unit tests. Do you know of a good place for a winform project like this I can download and study?

    I am new to MVC and want to use the passive view for unit testing, which I am (intermediate new) too…

    thanks

  2. Hey John,

    Guilty as charged.. I’ll root around here and see if I can find the spike that lead to the implementation.

    Thanks for looking in – Jon

  3. Hi there Jon,
    I just read your post on the TDD forum and followed your link here. I would love to have a look at your code if it is still available.

    I have been playing around with MVP/Passive View in WinForms for a while now. After a few false starts I am starting to make some headway, but it is always good to see how others are doing it.

    Cheers,
    Brad

  4. Hi Jon

    i am trying to get my head around MVP passive for winforms, and i would also love to take a look at your code. if you could email it to me, i would be really grateful. thanks.

  5. Hey Martin,

    Sorry to say that I lost that code in a move from PC to PC.. I would be glad to answer any questions though. Send an e-mail with your e-mail address to agilista ‘the symbol’ yahoo.com and I’ll get back to you.

    Thanks for your interest and sorry for the subterfuge – Jon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s