The following implementation that I created provides
notification about a change to an underlying data source (which could be a data
table or a business object collection). Imagine this as an application that
needs to make sure the information provided is current, so every postback of
the page will rebind the data. All of the observers will receive any relevant
information from the page. For instance, say certain objects trigger some
event whenever the selection of an item within the page occurs. Say the
selection of a row of data then displays detailed information about that row,
as well as triggering other items (say relevant keywords, other relevant data
that is similar to the selected row, or a myriad of other features).
There are multiple ways to implement this, and the approach
I am going to use is to use an interface that the child user controls will
implement. This interface looks as such:
Listing 1
public interface ISelectedObserver
{
void Update(Assignment assignment);
}
Each respective custom user control class inherits from a
single or multiple custom user control class, depending on the setup with the
defined interface to receive updates. It could look something like this:
Listing 2
public abstract class AssignmentDetailsUserControlBase : ISelectedObserver
{
void ISelectedObserver.Update(Assignment assignment)
{
if (assignment == null)
this.Enabled = false;
else
{
this.Enabled = true;
this.txtDescription.Text = assignment.Description;
this.txtDueDate.Text = assignment.DueDate.ToShortDateString();
this.Requestor.Text = assignment.Requestor;
}
}
}
The custom page class defines a SetupObservers method called
during page initialization, which adds all the observers to the collection. Any
derivative page classes can then add additional observers, if needed.
Listing 3
protected virtual void SetupObservers()
{
this.Observers.Add(this.AssignmentDetails);
this.Observers.Add(this.Comments);
this.Observers.Add(this.SimilarItems);
}
The Observers property is a local collection, which is adding
instances of user controls. The user controls shown above are exposed as page
properties (AssignmentDetails is a property of the custom AssignmentPage class).
Alternatively, the page could create a RegisterObserver method that registers
an observer with the page.
When the page loads after the observers are initialized, the
code checks whether the main subject is selected (such as a gridview). For
instance, when an item in a gridview is selected or deselected, a notification can
be sent.
Listing 4
protected override void OnLoad(EventArgs e)
{
AssignmentCollection assignments = AssignmentsFactory.GetAssignments();
this.AssignmentsGridView.DataSource = assignments;
this.AssignmentsGridView.DataBind();
if (this.AssignmentsGridView.SelectedIndex != -1)
this.NotifyObservers(assignments[this.AssignmentsGridView.SelectedIndex]);
else
this.NotifyObservers(null);
}
This notification controls whether an item is in the selected
or deselected state, and populates any relevant information.