Reputation: 350
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.
Upvotes: 3
Views: 1165
Reputation: 2398
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.
Changed
events. For example, create an event called NameChanged
if you have a property called Name
. 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.)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 ClassroomView
that displays a List<Student>
.
ViewModelBase
class that implements IDisposable
and INotifyPropertyChanged
.ViewModel
.PersonViewModel
class, if you want to display a full name, create a property called FullName
, where the getter could concatenate the FirstName
and LastName
property of an associated Person
model.bool
properties like IsHeadOfHouseHousehold
for binding to checkboxes or radio buttons.Color
properties like HighlightColor
for binding to BackColor
/ForeColor
/etc of various controls.string
properties that allow the underlying data model object to be edited, by binding to textboxes.FirstNameChanged
. In the eventhandlers, you should raise the PropertyChanged
event for any properties in the view model that are affected. For example, the handler for the FirstNameChanged
event should raise PropertyChanged
for FullName
.Dispose
method, 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.OK, now the easy part:
Click
event handlers to call the appropriate public method on the ViewModel.Upvotes: 2