DotNetRussell
DotNetRussell

Reputation: 9857

What should go in CanExecute?

I think that there is a specific answer to this.

If I have a command binding

private bool CanExecute(Object args){
  // Should this just be null checks?
  // Should it also contain logic? 
  // example:

  return this.SelectedObject != null;

  // or 

  return this.SelectedObject != null && this.SelectedObject.Status == 1;
}

private void Executed(Object args){

  //Or should logic be reserved for the Executed command

  if(this.SelectedObject.Status == 1)
     //Do stuff
  else
     //Don't do stuff
}

It seems redundant to have a can execute method if we do additional data validation within the executed method.

Upvotes: 0

Views: 102

Answers (2)

Dennis
Dennis

Reputation: 37770

if the logic of your command assumes, that it must not be executed, when some conditions have met, then CanExecute have to check these conditions.

Otherwise, CanExecute must return true.

It doesn't matter, what is the nature of conditions, but you should note, that long running checks may hit UI thread performance.

Upvotes: 2

David Pilkington
David Pilkington

Reputation: 13630

The way that I see it is that there is a distinction of whether something CAN happen and if something SHOULD happen.

An example of this can be a save button or something. The user may not have rights to save an entity so the action CAN'T happen.

If the user does have rights, all of the required fields may not be filled in so it SHOULDN'T happen.

Its in the semantics.

Upvotes: 2

Related Questions