Alex Mueller on Software and Technology 
Sunday, April 29, 2007

In my ASP.NET application, I have a need to update other user controls when data one user control changes state. As with most problems in life, there are often several ways to solve them. For this situation, I chose to apply the observer pattern. This post will discuss the Observer pattern and provide a demonstration using ASP.NET with C#.

The Observer Pattern "defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically" (Freeman, Freeman, Sierra, & Bates, 2004). Before implementing this solution, I spent some time looking into an event driven model in ASP.NET, as well as the Publish-Subscribe pattern, but in the end, I chose the Observer pattern for usability and simplicity.

The Publish-Subscribe pattern uses events and an event channel as a means of abstraction between the subject and its observers. You can read more about the Publish-Subscribe pattern here. In the end, you still need to wire up the events, which means some module needs to be responsible for establishing this relationship. The implementation of the Observer pattern I use decouples the subject from its observers using interfaces. I have read articles that pitch one pattern against another, which I do not like. I am not arguing for nor against the Publish-Subscribe pattern. Each pattern exists to serve a purpose.

In my ASP.NET applications I am using Ajax to improve the user experience wherever it is appropriate. I say, "wherever it is appropriate," because there are both pros and cons when using Ajax. I am using the Telerik Ajax framework, which is built upon Microsoft's Atlas. My implementation of the Observer pattern will execute using Ajax requests. The beauty of Telerik's framework is that I develop code as if there were no Ajax requirements, then establish a mapping of which controls I wish to "Ajaxify." I recommend reading more about Telerik here.

One last note, my applications use a Supervising Controller pattern for ASP.NET. I still refer to this as Model-View-Presenter (MVP), so the code in this post will reference "presenters" and "views." The ASPX page, or view initializer, is responsible for establishing the relationships among the one-to-many views on the ASPX page, handling page redirects and events, and in my scenario, generating the subject to observer relation.

The goal of the Observer pattern is to provide a relationship where objects, "observers," are notified when the subject, "observed," changes state. Loose coupling is another goal of this association. When objects are loosely coupled, they are able to interact without the dependency of knowing anything about the other. In the Observer pattern, the subject only knows about an observer interface. It does not actually know about the observer implementation. New observers may be added at any time and the subject remains ignorant of any changes, other than an incremented count to the observer's array.

The following image shows the Observer pattern and its relationships.

My implementation of the observer is seen below. I am not implementing the "GetState" functionality in my concrete subject, only because I do not have a need for it at this time.

Show code

The Subject and Observer interfaces

Show code

The subject or the “observed.”

Show code

The First Observer.

Show code

The Second Observer.

Show code

Finally, the actual page that declares the subject and observer relationships.

The code is fairly straight-forward. I have not included my ASPX/ASCX view code. The actual Observer-Subject associations are defined via the presenters. My master, ASPX view is responsible for initializing the relationships. Since I am not showing my ASPX/ASCX view code, one cannot tell that I am using the Telerik Ajax framework, but this entire scenario occurs during an Ajax request.

Below are screen shots to show the user flow of this sample demo. The first screen shot shows the DoubleList view initialized with the default entries for my listbox. These views are trivial. This is not a blog entry showing how to design an appealing user interface.

The next photo shows the user selecting multiple listbox items to add them to our collection.

Lastly, after adding items to our collection, the DoubleList view, and both Observer 1 and 2 are updated with information immediately. Observers 1 and 2 are updated via the DoubleList view subject.

I read through the Observer and Publish-Subscribe patterns just before retiring to sleep for the evening, with the latter on my mind as my head hit the pillow. I woke up the next morning with the implementation for the Observer fresh on my mind. It is amazing how that works. Now I need to convince my boss at work that I should nap for three hours every day to help solve mission, critical problems.

Several months back, I posted a series of articles on object-oriented principles. I have always wanted to do this for design patterns, and perhaps this could be the first of many. Only time will tell, as I struggle to find time to post because of other commitments. I hope this article was helpful in introducing and explaining the Observer pattern, and how one can apply it to ASP.NET using C#.

References:

Freeman, E., Freeman, E., Sierra, K., & Bates, B. (2004). Head First Design Patterns (1st ed.). Sebastopol, CA: O'Reilly Media, Inc.

Sunday, April 29, 2007 6:50:07 PM (Mountain Standard Time, UTC-07:00) | Comments [1] | Design | Frameworks/Patterns | Technology#
MuellerDesigns.net
Search
On This Page
The Split Personality of the Tester/Developer
Cross Site Scripting (XSS)
Creating files with FSUTIL
PowerShell Management Library for Hyper-V
Installing Windows 7
Installing Linux in Hyper-V
Internet Explorer 8 Release Candidate 1
PowerShell Documentation
Automate Daily Tasks with PowerShell
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
XPathNavigator.CheckValidity new for 2.0
SQL Server 2005 Connection Issues
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
Changing File Ownership In Vista and Longhorn
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska