This is the online documentation for PostSharp 5.0.
Download PDF or CHM. Go to v4.3 or v5.0

Command

In the GUI applications built using MVVM pattern it's common for the view model class to define commands that can be performed by the user in the associated view. These commands are usually exposed as properties of the view model class and the underlying command logic is implemented in the methods of the view model class. PostSharp allows you to implement commands in your view model classes without adding boilerplate code.

This topic contains the following sections:

Creating a simple command

To add a command named "LoadContactsList" to your view model class

  1. Implement the command logic in the ExecuteLoadContactsList method in your view model class.

  2. Add LoadContactsListCommand property to your view model class and mark it with the CommandAttribute attribute. The command property must be of type ICommand and have both a getter and a setter.

C#
[Command]
public ICommand LoadContactsListCommand { get; private set; }

public void ExecuteLoadContactsList()
{
    // ...
}

After adding a new command to your view model class you can bind it to a UI control in your view's XAML code.

XAML
<Button x:Name="LoadButton" Content="Load contacts" Command="{Binding LoadContactsListCommand}"
  HorizontalAlignment="Left" Margin="361,10,0,0" VerticalAlignment="Top" Width="85" Height="20"/>
Checking whether your command can be executed

The UI controls in the GUI application can be enabled or disabled based on what actions are currently available to the user. The command pattern provides a clean way to check the availability of the actions. For each command in your view model class you can implement a boolean property or a method that validates whether this command can be currently executed.

To add the availability check for the "CreateContact" command to your view model class add the CanExecuteCreateContact property to the same class. Implement the availability check in the property getter. Return true if the command can be executed or false otherwise.

C#
[Command]
public ICommand CreateContactCommand { get; private set; }

public bool CanExecuteCreateContact
{
    get
    {
        bool canCreateContact = true;
        // ...
        return canCreateContact;
    }
}

public void ExecuteCreateContact()
{
    // ...
}

The "CanExecute" properties associated with the commands also support the NotifyPropertyChanged pattern. Thus their values can be automatically recalculated and propagated to the UI controls using data binding. However, properties are not appropriate when the availability of the command depends on the input argument. For example, with the command "DeleteContact" you may want to validate whether the current user has the permission to delete the currently selected contact.

The command availability check that depends on the input argument can be implemented as a method. To add the availability check for the "DeleteContact" command to your view model class add the CanExecuteDeleteContact method to the same class. The method must have one parameter and return a bool value. The parameter type must be assignable from the type of the command's input argument.

C#
[Command]
public ICommand DeleteContactCommand { get; private set; }

public bool CanExecuteDeleteContact(int contactId)
{
    bool canDeleteContact = true;
    // ...
    return canDeleteContact;
}

public void ExecuteDeleteContact(int contactId)
{
    // ...
}
Customizing the names of the command's methods and properties

PostSharp follows a predefined naming convention when looking for methods and properties associated with the command in your view model class. You can override the naming convention and choose your own member names by setting properties on the CommandAttribute. The following tables shows the default naming convention and the properties used to override member names.

Command pattern's naming conventions

Member kind

Default name

Example

Set this property to override

Command property

CommandName -or- CommandNameCommand

UpdateContact -or- UpdateContactCommand

Execute method

ExecuteCommandName

ExecuteUpdateContact

ExecuteMethod

CanExecute method

CanExecuteCommandName

CanExecuteUpdateContact

CanExecuteMethod

CanExecute property

CanExecuteCommandName

CanExecuteUpdateContact

CanExecuteProperty

C#
[Command(ExecuteMethod = nameof(UpdateContact), CanExecuteMethod = nameof(CanUpdate))]
public ICommand UpdateContactCommand { get; private set; }

public bool CanUpdate(object newData)
{
    bool canUpdate = true;
    // ...
    return canUpdate;
}

public void UpdateContact(object newData)
{
    // ...
}
See Also