Is there anything that helps provide structure for good separation of concerns in a thick client or smart client app like asp.net mvc does for web?
views:
93answers:
5While not WinForms the WPF Toolkit from Codeplex has a model-view application template for Visual Studio.
This MSDN article on patterns also has some downloadable content that might be useful.
For WinForms, I don't know any such framework (although it must probably exist). But if you're willing to switch to WPF, the MVVM pattern is exactly what you need, and there are many frameworks to make your life easier with this pattern.
You can find a list of MVVM frameworks on Jeremy Alles's blog, and even a comparison matrix
It might be worth looking at the Smart Client Software Factory. Much of the focus was on composite applications but it does use views and I think the MVP pattern.
For WPF or Silverlight have a look at Prism.
MVC is just a pattern, and may be implemented quite easily without any framework whatsoever. It mostly dictates Separation of Concern [SoC] as concerns Gui and business functionality. I would also recommend you look up Model-View-Presenter [MVP] and Model-View-ViewModel [MVVM] - slight variations of MVC. MVVM is more prevalent than strict MVC.
Ahem, that aside, there are several enablers of MVC, such as Inversion of Control [IoC] and Dependency Injection [DI]. These patterns facilitate the creation of, and automate the "wiring" of your MVC components so that they may communicate with each other. In this respect, you may wish to take a look at Microsoft's ObjectBuilder [if targetting .Net 2.0], or roll with the much lighter-weight Unity container if you would like something more recent. I personally prefer Castle Windsor container for all of my IoC work, but Unity is maturing and I may switch over soon.
Of course you could also look at the frameworks these components are part of, namely Composite Application Block [CAB] or Prism respectively, but I wouldn't advise it. They offer some nifty solutions [you can emulate "Commands" in WinForms with CAB's EventBroker] but unless you have specific requirements, it's a lot of extra baggage imo.
If you understand the pattern [MVC], then a template is largely unnecessary.
If you are wondering how the bits come together, a quick MVC crash course:
// this is the controller. manages business logic, handles
// Gui inputs
public class Controller
{
// constructor
public Controller (View view, Model model)
{
// nothing magical about wiring things up
// obtain reference through ctor here, and
// hook up events, or provide bindings
// attach event handler - obviously make sure
// signatures match etc, i'm winging this whole thing
view.ButtonSubmit.Click += Submit;
// understand this will set the value of view's
// text once, here on instantiation. the problem
// with WinForms is databinding, you will have to
// roll your own. to provide proper databinding
// Model class must expose events when they change
// and then Controller or View must listen to event
// and bind new value to View
view.TextBox.Text = model.Count;
}
// Submit button clicked!
public void Submit () { ... }
}
// this is the model. contains all data relevant
// to Gui component
public class Model
{
// a count of stuff
public int Count { get; set; }
}
// this is the view. simple WinForms object,
// contains buttons, white fluffy clouds, and other
// good stuff
public class View : Control
{
public TextBox TextBoxCount;
public Button ButtonSubmit;
}
public static class Program
{
static void Main ()
{
// you can do this explicitly like below, or implicitly
// if you configure and use an IoC container.
View view = new View ();
Model model = new Model ();
Controller controller = new Controller (view, model);
}
}
The main disadvantage to MVC + WinForms, is above in the Controller. The Controller is referencing WinForms objects directly, which makes most scenarios difficult to unit test. What you would like is an abstraction from Gui elements and the events they throw. Also, you don't want to do the repetitive data binding task I allude to by inline comment. WPF adddresses these by introducing Commands and databinding.