-
Get a monthly update on best practices for delivering successful software.
PureMVC is open source framework that allows creation of application using Model, View and Controller pattern, and was original designed for Flex, Air and Flash application. It has since been port to various languages include the .NET C# language. So when I first heard about it, several months ago, I was working on a pretty large WinForm application design around the MVP - Model, View and Presenter pattern.
Our presenters were responsible for handling the user interaction coming from the views by subscribing to events published by the views, and coordinating these actions with changes needs from the models such that these changes are then reflected back in the views themselves.
Although this approached work, it requires a lot of wiring and unwiring of events from the various presenters to views, presenters to model, and presenters to presenters. So the code quickly begins to look like the following:
public class presenter1
{
public event EventHandler<EventArgs> SomethingHappened;
public presenter1()
{
_model = new Model();
_model.StateChanged += OnModelChange;
_model.DocumentSaved += OnModelSaved;
_view = new View();
_view.OnMouseDown += OnMouseDown;
_view.OnMouseUp += OnMouseUp;
_view.OnMouseMove += OnMouseMove;
_view.OnMouseEnter += OnMouseEnter;
}
…
…
As you can see, for every user interaction on the view or model change a particular presenter is interested in, you must subscribe to an event, and if other presenters are interested in particular actions from views they are not responsible for, well, I’m sure you can get the picture. So act II, introduction of PureMVC framework for WinForms.
So exploring the capabilities of PureMVC framework, uncovered a simplification of our code. At its core, it consists of four singleton objects Model, View, Controller and Façade. The Model, View and Controller contain literal string references to Proxies which are data model, Mediators which provide and manage the user interface, and Commands which interact with the Proxies and Mediators or other commands.
But the simplification revolves around the Façade which is responsible to provide messaging throughout the core framework using an Observer notification scheme. So now our code quickly begins to look like the following:
public class presenter1 : Mediator
{
public presenter1()
{
}
public override void ListNotificationOfInterest()
{
return new List<string> { Nofication.ViewMouseClick, Notification.ModelChange,};
}
public override void HandleNofication(INofication notification)
{
switch(nofication.Name)
{
case Notification.ViewMouseClick:
Dosomething();
break;
}
}
…
…
public class presenter2 : Mediator
{
public presenter2()
{
}
public override void ListNotificationOfInterest()
{
return new List<string> { Nofication.ViewMouseClick,};
}
public override void HandleNofication(INofication notification)
{
switch(nofication.Name)
{
case Notification.ViewMouseClick:
DoSomethingFun();
break;
}
}
…
…
public partial class view1 : Page
{
…
…
protected void view_mouseclicked(object sender, MouseEventArgs e)
{
Façade.NotifyObserver(new PureMVC.Pattern.Notification(Notification.Name));
}
…
…
Both presenter1 and presenter2 state they are interested in Notication.MouseClick event, and view1 uses the Façade to notify both presenters that the user has clicked the mouse button. No need to publish and subscribe to any events outside what’s naturally needed in the view.
The only thing I dislike is the name references, but as long as we are using constants you should be fine.
Related Services: .Net Application Development, Custom Software Development
Related posts:
[...] week, Karega Scott made a nice post on how Pure MVC makes development of Windows applications using C# simple. I thought I ll piggyback [...]
Pingback by Pathfinder Development | Software Developers | Blogs | More on Pure MVC for C#, Tuesday, July 28, 2009 @ 11:51 pm