views:

5997

answers:

8

Where can I find a good example on how to completely implement the MVC pattern in Windows Forms. I found many tutorials and code examples on various sites (e.g. CodeProject, .NetHeaven) but many are more representative for the observer pattern than MVC. Since the application I want to develop is for a school project, I am reluctant to using frameworks like PureMVC or MVC#.

+5  A: 

Have you looked at PureMVC? I've found that no one can agree on what MVC really looks like once they start building a specific implementation.

Update: You could build your own starting with something simpler such as MobileMVC. Compact Framework code should compile/run OK on Windows. Since this is a school assignment I would suggest that you actually spend some time learning how MVC actually works.

BrianLy
I have a school assignment for a very simple application to manage a bookstore, and I am quite reluctant to using a framework like PureMVC. I am looking for something simpler.
iulianchira
+1  A: 

You might want to take a look at Differential Execution.

Here it is in SourceForge

IMO, it is a vast improvement on MVC, though it is still quite unusual.

Mike Dunlavey
+5  A: 

I wrote an article last year, Selecting a MVC/MVP Implementation for a Winforms Project, that provides an example of a pretty simple passive view framework. Also see here and here.

Bob Nadler
Bob, I downloaded the code in your article; but when I click on Form1.cs, I get the following:"Could not find type 'MVCArticle.UI.CatView1'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built. "
gnomixa
@gnomixa I'm not sure why you're having a problem. I just downloaded and the Form1.cs designer load, build, and run worked fine. Maybe CatView1.cs didn't get unpacked properly?
Bob Nadler
+1  A: 

A good example at rolling your own implementation of MVC using Windows Forms can be found here. Source code is included.

As you read, study, and write code for this assignment you are going to find that there are a lot of disagreements on how MVC should be implemented. This one is a simple case that reflects the separation of concerns as well as a good example of the 'plumbing' required to hook this up.

When you are out of school you will probably want to fall back on a framework like the other posters have recommended.

Gary.Ray
+27  A: 

I am of the view that applications are so different from each other and our understanding of how applications should be written is sill very limited. Past Windows Forms applications I have worked on have been so different from each other, some of the design differences I have seen are (including most combinations):

  • Directly talk to database (2 tier)
  • Use a backend that has been written for the given application (3 tier)
  • Use a set of web services that were written for use by many applications and can’t be changed for your application. (Service-oriented architecture)
  • Updates being done by CRUD operations
  • Updates being done with the command pattern (sending commands to backend server)
  • Lots of usages of data binding / no usages of data binding
  • Most data being “table like” (e.g. invoices) that work well in standard grid controls / need custom controls for most of the UI data.
  • 1 Developer / Teams of 10 or 20 developers (just on the UI)
  • Lots of unit test using mocks etc / no unit tests

Therefore I don’t think it’s possible to create one implementation of MVC (or MVP) that always fits well.

The best posts I have seen really explaining MVC and why a MVC system is built the way it is, is the "Build Your Own CAB" series by Jeremy D Miller. After working though it you should be able to understand your options a lot better. Microsoft's Smart Client Guidance (CAB / Microsoft Composite Application Block) should also be considered, it is a bit complex but can work well for applications that have a good fit.

Selecting a MVC/MVP Implementation for a Winforms Project give an overview that is worth reading. A lot of people like PureMVC, I have never used it, but will look at it the next time I need a MVC framework.

"Presenter First" is a software development approach that combines the ideas of the Model View Presenter (MVP) design pattern and Test-Driven Development. It lets you start of by writing tests in the customer’s language .e.g

"When I click the 'save' button then the file should be saved and the unsaved file warning should disappear.”

I have no experience using "Presenter First," but will give it a try when I get a chance, as it looks very promising.

Other stackoverflow questions you may may wish to look at here and here.

If you are thinking of useing WPF at any point take a look at the Model-View ViewModel (MVVM) pattern. Here is a very good video you should take a look at: Jason Dolinger on Model-View-ViewModel.

MVVM (Model View View Model) Design Pattern for Winforms give another option that may make it easer to convert to WPF if every needed.

Ian Ringrose
+1  A: 

Microsoft Composite Interface Application block started its life as a MVC implementation (amongst other patterns it implemented). The release version, however, evolved into an MVP implementation, which can be argued to be a kind of a different interpretation of the MVC concept.

If you are willing to check the code of a very complete (and somehow complex) MVP implementation, you can find the MS-CAB as one of the components of Microsoft Smart Client Software Factory. It comes with source code. You can find it here. Good luck!

Rui Craveiro
+1  A: 

I've only had a cursory glance at the code but this "tutorial" and sample project might be useful:

http://rdn-consulting.com/blog/2008/02/01/selecting-a-mvcmvp-implementation-for-a-winforms-project/

Jonathan Parker
+19  A: 

UPDATE: In addition to my previous answer below, I suggest reading about the "Presenter First" approach (especially the PDF articles)

I would recommend MVP (PassiveView pattern actually) instead of MVC. You don't really need any special frameworks for this, it's just how you organize your code.

One approach (which I usually take) is to split each windows form into three entities:

  1. A presenter/controller class - this is what you actually start with when developing a form. This is where most/all of your "business" logic should reside.
  2. A view interface (IView), which contains the methods, properties and events. This interface is all that the presenter knows about your form.
  3. At the end, when you finish implementing the presenter and the view (including unit tests), you can then create the actual form class and make it implement the IView interface. Then it's just a question of adding appropriate controls to the form and wiring them to the interface.

Example code (a simple pseudocode, just for illustration):

interface IView
{
    string Username { get; set; }
    string Password { get; set; }

    event EventHandler LogOnButtonClicked;

    void InformUserLogOnFailed();
    void MoveToMainScreen();
}

class Presenter
{
    public Presenter(IView view)
    {
        this.view = view;
        view.LogOnButtonClicked += new EventHandler(OnLogOnButton);
    }

    private void OnLogOnButton()
    {
        // we ask some service to verify the username/password
        bool isLogOnOk = logOnService.IsUserAndPasswordOk(view.Username, view.Password);
        if (isLogOnOk)
            view.MoveToMainScreen();
        else
        {
            view.Username = "";
            view.Password = "";
            view.InformUserLogOnFailed();
        }
    }

    private IView view;
}

class Form : IView
{
    public Form()
    {
        presenter = new Presenter(this);
    }

    public string Username
    {
        get { return TextBoxUsername.Text; }
        set { TextBoxUsername.Text = value; }
    }

    public string Password
    {
        get { return TextBoxPassword.Text; }
        set { TextBoxPassword.Text = value; }
    }

    public void InformUserLogOnFailed()
    {
        MessageBox.Show("Invalid username or password.");
    }

    public void MoveToMainScreen()
    {
        // code for opening another form...
    }

    private Presenter presenter;
}
Igor Brejc
This is an implementation of the MVP variant called PassiveView. In the passive view the view should not choice his presenter.For a similar example (but with a view REALLY passive) check this sample http://www.danieleteti.it/?p=221 (example in Delphi language)
Daniele Teti