This project is read-only.

ASP.NET MVVM and Event Broker

In the Presentation Model pattern, a.k.a. the Model-View-ViewModel pattern, the Presentation Model publishes events and the View subscribes to the events. The Event Broker pattern offers an elegant method to manage the publication and subscription relationship.

In the Microsoft Composite UI Application Block, there is an event broker system that enables to publish and to subscribe events between components. An event publisher can public events like below.

[EventPublication("event://UpdatesAvailable", PublicationScope.Global)]
public event SomeEventHandler UpdatesAvailable;
An event consumer can subscribe to events like below.

[EventSubscription("event://UpdatesAvailable")]
public void NewUpdates(object sender, SomeEventArgs numUpdates)
{
}
The ASP.NET MVVM brings this pattern to ASP.NET and simplified it.

1. All events in the Presentation Model are published.
The ASP.NET MVVM assumes all events in the Presentation Model are. There is no Event Publication Attribute. By doing so, the Presentation Model has no dependency the ASP.NET MVVM framework. It should be able to reuse in other application platform, such as WPF and Silverlight without any change.

2. Event names are event topics
The ASP.NET MVVM framework matches the publication and subscription by the event name and function name by default. It can also match them by providing the event name and property name of the Presentation Model instance. E.g. the event and functions below will match.

// The event in the Presentation Model / ViewModel
Public event EventHandler CustomerChanged;

// In the View, subscribe to event with same name 
[EventSubscription]
private void CustomerChanged(object sender, EventArgs e)
{
}
// In the View, subscribe to event that matches a name 
[EventSubscription(EventName= “CustomerChanged”)]
private void controller_CustomerChanged(object sender, EventArgs e)
{
}
// In the View, subscribe to event that matches a name and the Presentation Model property name 
[EventSubscription(EventName=“CustomerChanged”, ControllerName=”controller”)]
private void controller_CustomerChanged(object sender, EventArgs e)
{
}

In ASP.NET, if subscribed to some events when page loaded, the events must be unsubscribed when page unloaded. Otherwise if the event publisher is in Session or Application, it holds references to the event handlers in the View. The View then will not be garbage collected, which could cause memory leak.

[Inject]
public ICustomerList controller { get; set;}

protected void Page_Load(object sender, EventArgs e)
{
    controller.CustomerListChanged += new EventHandler(CustomerListChanged);
}

protected void Page_Unload(object sender, EventArgs e)
{
    controller.CustomerListChanged -= new EventHandler(CustomerListChanged);
}
To prevent memory leak, the ASP.NET MVVM framework stores the events and subscribing method links. When web page unloads, the ASP.NET MVVM framework un-subscribes the events using the stored links. This is done behind the scene. No unsubscribing code is required in the View.

Last edited Mar 17, 2009 at 2:40 AM by yysun, version 2

Comments

No comments yet.