views:

60

answers:

1

i am using the SampleModel project from MVVM Foundation.Why is ICommand returned when _decrementCommand is declared as RelayCommand. I know RelayCommand inherits ICommand but won't is be clearer to just return a RelayCommand?

public ICommand DecrementCommand {
    get { return _decrementCommand ?? (_decrementCommand = new RelayCommand(() => --this.Value)); }
}
RelayCommand _decrementCommand;
+4  A: 

This is the principle of using the least specific type you can get away with. Concrete or specific types are a liability because they are more likely to expose things that callers don't need to know about and they are more likely to need to be changed (making them an implementation detail).

In this case, if you always return ICommand, then you can change the underlying command type without breaking callers. You can also expect that the callers won't have to know more about what your function does than they actually should know, meaning that they will be less likely to break OO principles.

siride