views:

849

answers:

3

Consider the reference article, specifically the example implementation of a RelayCommand (In Figure 3). (No need to read through the entire article for this question.)

In general, I think the implementation is excellent, but I have a question about the delegation of CanExecuteChanged subscriptions to the CommandManager's RequerySuggested event. The documentation for RequerySuggested states:

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.

Yet the sample implementation of RelayCommand does not maintain any such to the subscribed handler:

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}
  1. Does this leak the weak reference up to the RelayCommand's client, requiring that the user of the RelayCommand understand the implementation of CanExecuteChanged and maintain a live reference themselves?
  2. If so, does it make sense to, e.g., modify the implementation of RelayCommand to be something like the following to mitigate the potential premature GC of the CanExecuteChanged subscriber:

    // 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; 
        }
    }
    
A: 

I may be missing the point here but doesn't the following constitute the strong reference to the event handler in the contructor?

    _canExecute = canExecute;           
Lazarus
`CanExecuteChanged` is not `canExecute`.
Greg D
No. `_canExecute` is the delegate that evaluates whether the command can be executed. `CanExecuteChanged` is an event that occurs when `_canExecute` is reevaluated. There's no relation between `_canExecute` and the handlers subscribed to the `CanExecuteChanged` event
Thomas Levesque
Of course. My answer was pre-today's Red Bull equivalent! Is RouteCommand actually the object listening for the event? My understanding here is limited but what I'm seeing is an object that's going to create a listener for the CanExecuteChanged event and then in the example given it'll execute _saveCommand.CanExecuteChanged += myHandler. To my (very) caffeine addled brain, it would seem that the responsibility for the strong reference lies with SaveCommand, not RelayCommand as RelayCommand isn't providing the listener for CanExecuteChanged.
Lazarus
+2  A: 

Well, according to Reflector it's implemented the same way in the RoutedCommand class, so I guess it must be OK... unless someone in the WPF team made a mistake ;)

Thomas Levesque
I admit that it's unlikely, especially given that WPF is, I suspect, the common consumer of the CanExecuteChanged event. I'm trying to follow the docs as-written, though, so while the actual behavior may work, I can't help but suspect we're depending on an implementation detail.
Greg D
A: 

I believe it is flawed.

By rerouting the events to the CommandManager, you do get the following behavior

This ensures that the WPF commanding infrastructure asks all RelayCommand objects if they can execute whenever it asks the built-in commands.

However, what happens when you wish to inform all controls bound to a single command to re-evaluate CanExecute status? In his implementation, you must go to the CommandManager, meaning

Every single command binding in your application is reevaluated

That includes all the ones that don't matter a hill of beans, the ones where evaluating CanExecute has side effects (such as database access or long running tasks), the ones that are waiting to be collected... Its like using a sledgehammer to drive a friggen nail.

You have to seriously consider the ramifications of doing this.

Will
In principle I agree with you. But on the other hand, CanExecute shouldn't really do anything heavy anyway since it can be called quite frequently by WPF
Isak Savo
@Isak yep. Of course, that's not always reality. And in those cases blindly redirecting the event is a bad choice.
Will