Skip to content

Uses of Action<T>: Displaying a Message to the User

March 25, 2012

The Action<T> generic delegates can often be used to make difficult code appear simple.  This holds as well for its cousin the Func<T,TResult> generic delegate that returns a result. 

This article explores another simple but powerful use of the Action<T> delegate:   Using an Action<T> delegate injected into the ViewModel by the View to display a message box, providing good separation of concerns, excellent information hiding, and great simplicity. 

The main reason the MVVM pattern exists and is so popular is that it provides an excellent separation of concerns.  Strong separation of concerns makes code less snarled, and less expensive to extend and maintain.  The MVVM separates to concerns of the Gui into 3 different kinds of classes, each having specific non-overlapping responsibilities generally as follows.  (Note there are several variations of responsibilities in common use, so the ones below are approximate.  Please consult other sources for more precise definitions of these responsibilities.):

  •  The View’s responsibility is to do all the work necessary to display things, i.e. the Gui Logic.
  •  The ViewModel’s responsibility is to do the Presentation Logic work.
  •  The Model’s responsibility is to contain the Business Logic and Data.

This clean separation of concerns is broken when code is implemented in one of the above classes that is outside its area of responsibility.  When this happens, code becomes more snarled.

A common area where it is easy to violate these class responsibilities is displaying a message to the user.  For example, say some service was unavailable and the user needed to know about that.  The quickest way to deal with this is for the ViewModel to directly display a MessageBox telling the user of the situation.  However, it is the View’s job to do all displaying, so this approach violates class responsibilities and causes snarled code.

In order to honor the responsibilities of the View and ViewModel, the implementation should have the ViewModel detect the error condition and then notify the View to display the message.  Here the ViewModel does the Presentation Logic of finding when something went wrong, and then relies on the View to do its job of displaying the error to the user.

There are  a number of ways to implement the ViewModel notifying the View of its need to display a message.  Ollie Riches blog appeared on Silverlight Cream on 3-22-2012, presenting a really nice overview of various ways of doing this in code.  Thanks Ollie! You can see it at:!/2012/03/showing-message-box-from-viewmodel-in.html

However, there is a simpler way to do such a display using the Action<T> delegate, as follows:

  1. The View contains a public method that displays a message box, called MessageBoxDisplayer(string message). 
  2. The ViewModel contains a public property of type Action<string> called DisplayMessageBoxDelegate.  Note that the signature of this delegate matches the View’s method MessageBoxDisplayer(string message).
  3. After the View instantiates the ViewModel, it deposits (injects) the address of its MessageBoxDisplayer() method into the ViewModel’s DisplayMessageBoxDelegate Action<T> property.
  4. Now, all the ViewModel has to do when it wants to have the View display a message box, is to make the below simple call:

   DisplayMessageBoxDelegate(“Here is a message for the View to display.”);

That’s it.  No commands to hook up.  No events to hook up.  No messages to hook up.  Pure simplicity!

A code example may be downloaded from the OneDrive file named ActionTForVm-ViewMsgBoxDisplay.  Just click the link, then click on “Download” on OneDrive’s top menu bar when it appears.

Creative Commons License
dotnetsilverlightprism blog by George Stevens is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: