Click here to Skip to main content
Click here to Skip to main content

A Generic Implementation for ICommand

, 24 Dec 2013
Rate this:
Please Sign up or sign in to vote.
This tip provides a simple implementation for the ICommand interface and enables to write quick code leaving the nuances of fetching the return types and asynchrony

Introduction

When I started my first project in WPF, I was faced with a lot of decisions to make. One such decision that I made was to use the Command pattern provided by System.Windows.Input.ICommand to handle the user interactions. As I got deeper, I found that I had to:

  1. write lots of boilerplate code
  2. cannot get the result of the execution. ICommand.Execute returns a void.

The MVVM frameworks would have presented a solution but I did not want to include a new framework just for the sake of this and implemented a few classes that would handle the returned data from the command execution and also do asynchronous execution. So I went ahead and wrote few classes.

  • UICommand - A plain vanilla implementation of the ICommand interface
  • UICommand<T> - A UICommand that can return a value of type T
  • AsyncUICommand - Very similar to UICommand but executes the user function in a threadpool thread
  • AsyncUICommand<T> - A AsyncUICommand that returns a type T

Using the Code  

All you need to do is derive a class from one of the above mentioned classes and override the OnExecute method.

The following snippet shows a sample implementation for creating a folder on the file system.

 class CreateFolderCommand : AsyncUICommand
    {      
        override protected void OnExecute(object parameter)
        {
            string path = parameter as string;
            System.IO.Directory.CreateDirectory(path);                         
        }
    } 

On the invoking side, create an object for this class:

CreateFolderCommand cmdCreateFolder = new CreateFolderCommand (); 

If you need a callback after the execution, register for the event. The event will notify you with an exception object if an exception has occurred. If not, it will be null:

cmdCreateFolder.ExecutionComplete += ((ex) =>
{
if(ex != null){//There is some error, handle the exception here
}}); 

That's it.

When you use the UICommand<T>, the ExecutionComplete event will also give you the result of the execution.

The Async versions also behave in the similar way. The event handlers will be called in the context of the UI thread and so you really need not worry about any context switching and update UI elements directly in the handler.

History

  • 25th Dec, 2013: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Senthilvel Samatharman
Software Developer (Senior)
India India
I have been associated with software in different domains and positions. I wrote most of my code in C++, basically for windows applications. For sometime, I managed teams that built websites and services using Java and related technologies. Now, I am back into development spree and am wetting my hands in .Net technologies.
Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 24 Dec 2013
Article Copyright 2013 by Senthilvel Samatharman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid