share
Stack OverflowLooking for a MVC Sample for WinForms
[+84] [10] urini
[2008-08-05 14:45:25]
[ winforms user-interface design-patterns ]
[ http://stackoverflow.com/questions/2406/looking-for-a-mvc-sample-for-winforms ] [DELETED]

Can you point me to a good real-world WinForms MVC application/framework?

I understand the Model part - that's your data, and I know the forms are the views, but what's the controller? Especially if I want to use data binding.

(3) You may find the answers to stackoverflow.com/questions/654722/… helpful as they talk about the different options when implementing MVC and MVP - Ian Ringrose
[+52] [2008-08-07 06:05:06] FryHard

Before using MVC make very sure you know why your are putting yourself thought the pain. Having sections of code split into logical groups is great, moving logic out of the UI is great, but going the full nine yards can be more effort that it is worth. (imho)

Adding data binding to the mix will cause you to end up hybrid in most cases. In the project that I am currently working on we reached the same position as you and after much talk and research ended up with the following.

Model

This would be your data as you said, in our case it is our Domain objects that contain information on the domain. eg. Person, Company ...

These domain object are data bound to the forms, in this way providing direct communication between your MODEL and your VIEW.

View

This would be your user interface or windows form. Now since you are using data binding you will most probably have a BindingSource on your form with its Datasource pointing to your domain object. Having the View being directly connected to the Model already start to break MVC. Take a look at the wikipedia article on MVC [1] and its related diagram on the accepted communication. But as I said earlier, we are going to end up with a hybid anyway...

Controller

The controller will end up not doing anything close to what a real controller is meant to do. In the ideal MVC world, the controller will handle all interactions with the view. click a button: the controller works out what to do and updates the view after doing it. select an item in a combo box: the controller works out what to do and updates the view after doing it. and so on...

For this "implementation" the controller will be charged with providing all the nasty code that we dont want cluttering our UI .cs file. So if dropping down a combo box needs to populate another related combo box, put this code in the controller. If a form needs to conduct a complicated save routine to put together three object and save them in the right order, put this code into the controller.

What does this mean in implementation terms

When you create a new instance of a form you need to do two things.

  1. Create a new instance of the form's controller and save it on the form
  2. Set your domain object as the binding source for the form.
  3. Pass the form to the controller so that the controller can communicate back to the form.

But this question is best served with an example [2], so check out the example solution that I threw together. I have chosen to communicate between the Form (VIEW) and CONTROLLER with direct calls, but you could always make use of events (which i think is the more accepted norm).

Hope it helps..

[1] http://en.wikipedia.org/wiki/Model-view-controller
[2] http://www.fryhard.com/downloads/stackoverflow/MvcApplication.zip

(1) In your example application the controller makes calls to View's methods (for example: MyForm.SaveComplete(saveObject)). This kind of behavior violates MVC principle, where the controller cannot change View, but a change in Model make changes in View. Am I right? (your program appears to be a MVP pattern, not MVC). - RHaguiuda
(1) This is a really great example. It is short, sweet and to the point and provides a simple practical and usable example. Thank you. - MikeMalter
1
[+46] [2009-02-10 17:52:17] Lucas

I am also new to MVC/MVP patterns on Windows Forms, so I've been doing some research recently. Apparently, the pattern used in Windows Forms is actually MVP, a derivative of MVC, because of the way the controls/widgets handle the UI events themselves, not the controllers (even if only to delegate to the Controller/Presenter). MVP itself can be subdivided into Supervising Controller and Passive View patterns.

I found the following MVP frameworks for WinForms:

  • Microsoft patterns & pratices: Smart Client Software Factory [1] (SCSF) relies on the Enterprise Library (EntLib) and the Composite Application Block (CAB). It's very big and generates a lot of (apparently good) code for you. Requires installation of Guidance Automation Extensions (GAX) and Guidance Automation Toolkit (GAT) VS add-ins to create a wizard-like experience and context-sensitive menus ("add view", "add controller", etc). Doesn't support EntLib 4.1 or VS2008SP1 without some fixes [2]. For my very small project, this was bloated overkill.

  • Microsoft patterns & practices: Composite Application Guidance for WPF [3] (aka Prism or Composite Application Library, CAL) is newer than SCSF and a lot simpler. p&p seems to have listened to developers by removing the requirements for EntLib, GAX, etc. Its only dependency is Unity as the default DI container (you can use others). Implements other related patterns such as Commands and Event Aggregation for further decoupling. They tried to keep the WPF-specific stuff separate, so WinForms support can be added [4]. Prism 2.0 was released Feb 2009 and adds Silverlight support.

  • MVC# [5]: MVP framework which supports WinForms, WebForms, Silverlight, and the Compact Framework (Oleg Zhukov). It is simpler than p&p's offerings and has no external dependencies. Does not use a separate DI container nor does it implement event aggregation, etc. I didn't like the way MVC# tries to be very declarative by default. It relies so much on attributes that you need to declare "magic strings" as fields just to hang the attributes on. Then again, it also supports XML file configuration instead of attributes and you can write your own config providers. Generics support (IController<IView>) seems to have been added only recently.

Source code is available for those frameworks. I also found some samples and walk-thoughs that can get you started if you want to roll your own:

Further reading (you'll find lots of cross-linking between these):

I haven't decided yet if I will use Prism (with Winforms extensions), MVC# (and write my own providers, say no to magic strings), or end up writing my own very very simple MVP "framework". Or I could switch to WPF and use Prism as-is.


EDIT: Composite Application Guidance for WPF and Silverlight (Feb 2009)(aka Prism v2) has been released.

[1] http://www.codeplex.com/smartclient
[2] http://artazor.wordpress.com/2008/12/07/scsf-april-2008-fix-for-visual-studio-2008-sp1/
[3] http://www.codeplex.com/CompositeWPF
[4] http://www.softinsight.com/bnoyes/2008/10/13/CompositeExtensionsForWindowsForms.aspx
[5] http://www.mvcsharp.org/
[6] http://www.codeproject.com/KB/architecture/DotNetMVPFramework%5FPart1.aspx
[7] http://www.codeproject.com/KB/architecture/DotNetMVPFramework%5FPart2.aspx
[8] http://www.codeproject.com/KB/architecture/DotNetMVPFramework%5FPart3.aspx
[9] http://codebetter.com/blogs/jeremy.miller/archive/2007/07/25/the-build-your-own-cab-series-table-of-contents.aspx
[10] http://www.c-sharpcorner.com/UploadFile/rmcochran/PassiveView01262008091652AM/PassiveView.aspx
[11] http://rdn-consulting.com/blog/2008/02/01/selecting-a-mvcmvp-implementation-for-a-winforms-project/
[12] http://haacked.com/archive/2008/06/16/everything-you-wanted-to-know-about-mvc-and-mvp-but.aspx
[13] http://codebetter.com/blogs/kyle.baley/archive/2008/09/10/mvc-vs-mvp-a-hillbilly-s-journey.aspx
[14] http://martinfowler.com/eaaDev/uiArchs.html
[15] http://martinfowler.com/eaaDev/ModelViewPresenter.html

2
[+3] [2008-08-05 14:46:44] Brett Veenstra

Check out StoryTeller source code [1].

It's a work in progress, but should give you plenty of ideas/sample uses:

And yes, I am still actively developing it, it just got shelved for other projects. I'm using it for acceptance testing in my new job and I'll release after the (many, many) kinks and annoyances are ironed out. Ah, the joys of dogfooding.

I believe Jeremy is writing a book on WinForms development as well.

[1] http://codebetter.com/blogs/jeremy.miller/archive/2008/02/01/access-to-the-storyteller-source-code.aspx

3
[+3] [2008-09-10 19:56:06] Brian B

Have you checked out the MVP (Model/View/Presenter) format of the Smart Client Software Factory (SCSF), part of Microsoft's Patterns and Practices?
See www.codeplex.com/smartclient [1]. It is a big committment, with a learning curve, I'd recommend you at least read David Platt's book to get a good basic understanding of it ( www.rollthunder.com/Books/CABandSCSF/index.htm [2]). It generates a lot of code for you (the code is pretty good, but since you didn't write it, it can be daunting until you understand it better). We used it successfully to create a client application where two separate but integrated UI compomponents were created by different vendors, fairly independently, and the framework made them coexist very nicely.

[1] http://www.codeplex.com/smartclient
[2] http://www.rollthunder.com/Books/CABandSCSF/index.htm

4
[+2] [2008-08-18 17:26:56] Andrei Rînea

Shouldn't an MVP (instead of MVC) architecture suit better a WinForms application?


5
[+1] [2011-11-28 19:53:22] Sean Kearon

We should not forget to mention the Magellan [1] framework by Paul Stovell [2]. The Winform specifics are here [3].

[1] http://code.google.com/p/magellan-framework/
[2] http://www.paulstovell.com/blog
[3] http://code.google.com/p/magellan-framework/wiki/WinForms

6
[0] [2008-08-18 17:35:08] Rick

It's not a real-world sample, but Martin Fowler has some good articles on GUI Architectures [1] where he discusses MVC and MVP. In fact he's coined the term Supervising Controller [2] when using a MVC pattern with a UI framework that supports data binding.

[1] http://martinfowler.com/eaaDev/uiArchs.html
[2] http://martinfowler.com/eaaDev/SupervisingPresenter.html

7
[0] [2008-08-31 21:39:35] pirho

Craig Shoemaker made couple of good screencasts and podcasts about this on his show.

MV-patterns [1]

MVP Commander for creating stubs [2]

[1] http://polymorphicpodcast.com/shows/mv-patterns/
[2] http://polymorphicpodcast.com/shows/mvpcommander/

8
[0] [2008-09-03 02:57:48] Garo Yeriazarian

I've got a few posts up (and some sample code) for a simple Model-View-Presenter with Windows Forms and data binding on my blog here: http://garoyeri.blogspot.com. Look for the posts tagged "stocksample". Usually I mix Model-View-Presenter with a Controller to actually perform actions and load the presenters.


9
[-4] [2008-08-05 14:50:08] Ronnie

Check out http://www.asp.net/mvc/ - there are some links to webcasts that Scott Hanselman has been doing.

He also has some interesting stuff regularly on his blog and podcast - see http://www.hanselman.com


(3) I think the question was regarding winforms mvc not asp.net - Nathan Lee
I think mvc pattern is platform independent - Ask
10