WPF PRISM (CAL) / MVVM – DelegateCommand Extensions – Part 1

July 1, 2010

tags: ,
no comments

Well, it has certainly been a while since I got to a good posting rhythm, I hope it changes soon because I have a series of posts in mind.

This isn’t a post about general help and description for PRISM, MVVM, or DelegateCommand. There are many resources about these already.
This post is to demonstrate how I managed to extend the DelegateCommand with some really cool features.

The DelegateCommand that ships with CAL expects an “execute method” and a “can execute method” arguments in its constructor.
This is nice, but I wanted something more.

I am involved in a big WPF development project where we have many commands that their state depends on several state-related parameters. I could just “push” it all in the “can execute” method, however, I don’t quite like it due to the following:

  1. The code is written very imperatively
  2. You need to reserve fields for every command to trigger its “CanExecuteChanged” when the appropriate state changes
  3. No encapsulation, the code is scattered around, code gets repeated
  4. Difficulty to maintain – you need to search for all related places when the requirement changes and synchronize everything appropriately.
  5. Plain and simple – not pretty

The goal is to make the Delegate Command to be attachable with numerous state bound objects that can alert it when it needs to be refreshed.

Starting Point – Attachable Components

I needed to make a common interface for the attachable components that could be attached to the DelegateCommand, as follows –

Code Snippet
  1. public interface INotifyCanExecuteChanged
  2. {
  3.     event EventHandler CanExecuteChanged;
  5.     bool CanExecute();
  6. }

To make things a bit more easy for future classes, I created also a base class –

Code Snippet
  1. public abstract class NotifyCanExecuteChangedBase : INotifyCanExecuteChanged
  2. {
  3.     public event EventHandler CanExecuteChanged;
  4.     protected virtual void OnCanExecuteChanged()
  5.     {
  6.         var evt = CanExecuteChanged;
  7.         if (evt != null)
  8.         {
  9.             evt(this, EventArgs.Empty);
  10.         }
  11.     }
  13.     public abstract bool CanExecute();
  14. }

Step 2 – Enable INotifyCanExecuteChanged attachments to the DelegateCommand

Code Snippet
  1. public abstract class BaseDelegateCommandEx<T> : ICommandEx
  2. {
  3.     public BaseDelegateCommandEx(Action<T> executeMethod, params INotifyCanExecuteChanged[] notifiers)
  4.         : this(executeMethod, (IEnumerable<INotifyCanExecuteChanged>)notifiers)
  5.     { … }
  6. }

At this point I got a delegate command that can get attachable notifiers. My command binds to the event of each notifier and raises its own CanExecuteChanged.
Additionally, there’s logic in the CanExecute to call the notifiers as well.

In Conclusion for this Part

This seals the first part of the Delegate Command Extensions.
The delegate command can get attachable notifiers which it bounds to and mange calls to them. This is the basic implementation that allows the developers to define a command and bind it to numerous state-bound notifiers.

Next Time..

I will show the continuation of the solution which turned our real nice, I recommend you stay tuned 🙂

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>