I am working on a little file copying program for my company and it’s getting large enough that I’m starting to care about code design (I know, I should have from the beginning…).
Right now my problem is that I want my form class code to be as lean as possible. Based on my research, it sounds like I want to implement Duplicate Observed Data in order to keep the View decoupled from the rest of the program. Are there any tips on how to do this in VS2010/C#? Or are there better ways to do what I want to do?
I’m using Visual Studio 2010, .Net 4.0, C#, and it’s a Windows Forms Application.
Suggestions on tutorials, books, or open source examples are welcome.
Edit: I just found this article about the MVP pattern, which is probably relevant to me. But I’d still appreciate input.
We’ve approached this in past projects by applying a modified version of the MVVM pattern (usually associated with WPF) in Windows Forms. There is a lot of boilerplate code, so get your typing fingers ready, but it pays off very well in the long run.
Obviously, this is very long and will probably be overwhelming. Take your time and implement each piece at a time. It is worth noting that in WPF, many of the work below is done for you.
First, we start with three name spaces: (for extra points, put each namespace in a separate assembly, but that is not required.
Creating Domain Classes
Changedevents. For example, create an event calledNameChangedif you have a property calledName. The event should be raised from within the setter every time that property’s value changes.Delete(). These methods should throw exceptions if there is an error. (In more advanced designs, you may wish to have a controller object that contains the business logic.)Creating ViewModel Classes
A ViewModel class is mostly a designer code-behind class “on steroids”, except it doesn’t have any knowledge of the actual controls or layout of how it will be used, just behavior. Usually there is one ViewModel for each Model class, but you can also make a ViewModel such as
ClassroomViewthat displays aList<Student>.ViewModelBaseclass that implementsIDisposableandINotifyPropertyChanged.ViewModel.PersonViewModelclass, if you want to display a full name, create a property calledFullName, where the getter could concatenate theFirstNameandLastNameproperty of an associatedPersonmodel.boolproperties likeIsHeadOfHouseHouseholdfor binding to checkboxes or radio buttons.Colorproperties likeHighlightColorfor binding toBackColor/ForeColor/etc of various controls.stringproperties that allow the underlying data model object to be edited, by binding to textboxes.FirstNameChanged. In the eventhandlers, you should raise thePropertyChangedevent for any properties in the view model that are affected. For example, the handler for theFirstNameChangedevent should raisePropertyChangedforFullName.Disposemethod, you should unregister from the data model events to prevent memory leaks.Delete(). The methods should be written from a user perspective, so that they can raise dialog boxes to say “Are you sure?” Then they should call methods on the data model object, or some other controller object.Creating Views
OK, now the easy part:
Clickevent handlers to call the appropriate public method on the ViewModel.The End Result