title: Is Josh Smith's implementation of the RelayCommand flawed?
body: <p>Consider the reference <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="noreferrer">Josh Smith' article WPF Apps With The Model-View-ViewModel Design Pattern</a>, specifically the example implementation of a <code>RelayCommand</code> (In Figure 3).  (No need to read through the entire article for this question.)  </p>
  
  <p>In general, I think the implementation is excellent, but I have a question about the delegation of <code>CanExecuteChanged</code> subscriptions to the <code>CommandManager</code>'s <code>RequerySuggested</code> event.  The <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.requerysuggested%28VS.100%29.aspx" rel="noreferrer">documentation for <code>RequerySuggested</code></a> states:</p>
  
  <blockquote>
    <p>Since this event is static, it will
    only hold onto the handler as a weak
    reference. Objects that listen for
    this event should keep a strong
    reference to their event handler to
    avoid it being garbage collected. This
    can be accomplished by having a
    private field and assigning the
    handler as the value before or after
    attaching to this event.</p>
  </blockquote>
  
  <p>Yet the sample implementation of <code>RelayCommand</code> does not maintain any such to the subscribed handler:</p>
  
  <pre><code>public event EventHandler CanExecuteChanged
  {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
  }
  </code></pre>
  
  <ol>
  <li>Does this leak the weak reference up to the <code>RelayCommand</code>'s client, requiring that the user of the <code>RelayCommand</code> understand the implementation of <code>CanExecuteChanged</code> and maintain a live reference themselves?</li>
  <li><p>If so, does it make sense to, e.g., modify the implementation of <code>RelayCommand</code> to be something like the following to mitigate the potential premature GC of the <code>CanExecuteChanged</code> subscriber:</p>
  
  <pre><code>// This event never actually fires.  It's purely lifetime mgm't.
  private event EventHandler canExecChangedRef;
  public event EventHandler CanExecuteChanged
  {
      add 
      { 
          CommandManager.RequerySuggested += value;
          this.canExecChangedRef += value;
      }
      remove 
      {
          this.canExecChangedRef -= value;
          CommandManager.RequerySuggested -= value; 
      }
  }
  </code></pre></li>
  </ol>