Archive for November, 2009

Named Command Decorator

Here is the problem, we have a list of ‘commands’ we wish to show depending on what view model / entity is active. If we just had a collection of ICommands, we could show buttons but what would their content be?

My solution is to use the decorator pattern to decorate commands with a Name that can be used as the content for the buttons.

My decorator looks like :

public class NamedCommand : ICommand{    public NamedCommand(ICommand decoratedCommand, string name)    {        _DecoratedCommand = decoratedCommand;        Name = name;    }

    readonly ICommand _DecoratedCommand;    public string Name { get; set; }

    public void Execute(object parameter)    {        _DecoratedCommand.Execute(parameter);    }

    public bool CanExecute(object parameter)    {        return _DecoratedCommand.CanExecute(parameter);    }

    public event EventHandler CanExecuteChanged;}

I then created an extension method on ICommand :

public static ICommand Name(this ICommand command, string name){    return new NamedCommand(command, name);}

So in the code I can now do:

Commands.Add(new ActionCommand(OnOpen).Name("Open"));

For my WPF visual, I have a DataTemplate :

<DataTemplate DataType="{x:Type Commands:NamedCommand}" >    <Button Content="{Binding Name}"            Command="{Binding }" /></DataTemplate>

ANN: Houston Dynamic Languages User Group

.NET 4.0 is almost here, and with it, the DLR. What does that mean for you? If you are a .NET developer, you now have several new languages at your disposal in IronPython and IronRuby that will allow you to either write code much faster or embed scripting support into your projects. If you aren’t a .NET developer, you can now leverage the Silverlight full .NET framework using a language familiar to you. We’ll also be looking at other dynamic languages–such as Boo, JavaScript/JScript, and PHP–and their interoperability with various parts of the Microsoft ecosystem.

We’ll be starting up January 5th, 2010 and meeting the first Tuesday of every month. Please join us as we explore the possibilities with .NET and dynamic languages. More info to come!