tags:

views:

40

answers:

1

Hi,

I have a sub-class of DelegateCommand which fails with an InvalidProgramException, "The Command Language Runtime detected an invalid program." when I call Execute.

My sub-class looks like this:

    public class SaveOrderCommand : DelegateCommand<IOrder>
{
    [Dependency]
    public IOrderService OrderService { get; set; }

    public SaveOrderCommand()
        : base(p => this.ExecuteSaveOrder(p), p => this.CanExecuteSaveOrder(p))
    { }

    public bool CanExecuteSaveOrder(IOrder order)
    {
        return true;
    }

    public void ExecuteSaveOrder(IOrder order)
    {
        if (order == null)
            return;

        IProductOrderService productSpecificOrderService = this.OrderService.RegisteredServices[order.GetType()] as IProductOrderService;
        order = productSpecificOrderService.Save(order);
    }
}

I can see that giving the base class references to methods in the sub-class might be an issue but if that's the issue, can anyone suggest a better implementation?

+1  A: 

Perhaps you should just implement ICommand yourself? Your class would become:

public class SaveOrderCommand : ICommand
{
    [Dependency]
    public IOrderService OrderService { get; set; }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return parameter is IOrder;
    }

    public void Execute(object parameter)
    {
        var order = parameter as IOrder;
        if (order == null)
            return;

        IProductOrderService productSpecificOrderService = this.OrderService.RegisteredServices[order.GetType()] as IProductOrderService;
        order = productSpecificOrderService.Save(order);
    }
}
Abe Heidebrecht