Saturday, July 17, 2004

Implementing MVC in Windows Forms 2.

MVC (Model-View-Controller) requires a separation between the app controller, the app views (the Win Forms and controls), and the app model (the objects and collections that store data and control behaviour). The Controller knows about (i.e. references) the View and the Model, the View knows about the Model, and the Model is ignorant of both Controller and View. This enables the Model (business logic) to be UI agnostic, and can be shared between different UI implementations (web or thick client). This separation also makes the app more robust, as the app's object model and its UI is not interspersed; this makes it easier to change the UI without breaking business logic.
 
So what problems will you face implementing this in .NET Win Forms?
 
First of all, when you create a Win Forms project, the default project template will create a default Form class for you, and apply the [STAThread] attribute to it; it'll also add a call to Application.Run, passing the default form. This model is very similar to VB.old, and many VB apps tend to lump all of an app's code into the main form. Needless to say, this is the anithesis of MVC, and is not a good model. As mentioned in an earlier post, MS have corrected this in VS 2005, and a new Program class will be created by default to contain the startup Main() method.
 
Another problem is that you will feel an overwhelming urge to lump all your UI logic into a single monolithic assembly. Your exe will contain UI logic (controls, validation, events) with business logic for your app (an object model, data access logic, web service or Remoting calls, etc). You will do this even though it feels wrong; you will create an event handler for a menu item click, and in the method you will create instances of forms and show them, so that the navigation of your UI becomes tightly bound and "hard-coded" into the UI, and quickly breaks if requirements change or you have to change navigation.
 
So the first step is to create three separate projects in your Win Forms solution. Create a Win Forms project, and two class library projects.
 
In the Win Forms project, delete the default form class. Add a new class, and call it "Controller". This class will contain your Main() method, decorated with the [STAThread] attribute. In here you can place any app initialisation logic, to set up access to resources (which you should implement as a separate assembly containing resx files for string and image resources, along with wrapper classes to provide access to them).
 
One of the class libraries will be your model, so give it a suitable name.
 
The other class library will contain your views - so add the usual references (System.Drawing and System.Windows.Forms). You need to create a base class for your forms, and classes to represent each form's context (more on this in a future post).
 
In the exe project (the controller), add references to both the forms and model projects. In the forms project, add a reference to the model.  This setup will enforce the logical separation between Model-View-Controller, and physically stop you from breaking this separation. The Controller will communicate with the Views by creating instances of form classes, setting properties and calling methods. The Views will communicate with the Controller by raising events. The Controller will respond to the events by creating and showing more views, updating the Model, and so on.
 
In addition, another benefit is that you will be able to use NUnitForms to test your UI project; if you've used NUnit before, you'll know that you need to create a separate class library that will contain your unit test classes, and add references to the assemblies containing the classes you're testing. NUnitForms follows the same model, but as you probably know, you can't add a reference to an exe. Having your forms in a separate class library solves this problem.
 
More to follow...

0 Comments:

Post a Comment

<< Home