Patch Submission to fix Bug in CAL

Topics: Prism v2 - Silverlight 2
Jun 19, 2009 at 8:12 PM
Edited Jun 19, 2009 at 8:13 PM

Is there anyone I can submit a patch to the CAL for?

Basically, in the Silverlight implementation of DelegateCommand<T>.OnCanExecuteChanged, there is a call to Deployment.Current.

In a unit test environment (specifically, NUnit), this code throws an exception because there is no current deployment.  I believe it is safe to wrap the block in an try/catch and set it to null on exception.

I hope this gets to the right people, in case the DelegateCommand makes it into Silverlight 3 in its current form.  Without this change, we can't use the CAL implementation of DelegateCommand.

Note: The same is true of CompositeCommand.

The Class is: Microsoft.Practices.Composite.Presentation.Commands.DelegateCommand<T>.

The code currently is:

        protected virtual void OnCanExecuteChanged()
        {
            Dispatcher dispatcher = null;

            if (Deployment.Current != null)
            {
                dispatcher = Deployment.Current.Dispatcher;
            }

The code can be changed to:

 

        protected virtual void OnCanExecuteChanged()
        {
            Dispatcher dispatcher = null;

            try
            {
                if (Deployment.Current != null)
                {
                    dispatcher = Deployment.Current.Dispatcher;
                }
            }
            catch (Exception)
            {
                dispatcher = null;
            }

 

Thank you,

Brian Genisio

Jun 19, 2009 at 10:16 PM

Hi Brian,

 

If you believe your suggestion could be useful for the community, you could create a work item in the Issue Tracker, so people can vote for it. It might be taken into account for future releases.

On the other hand, if you need to use a delegate command in a test, you might consider using a mocking the DelegateCommand class.

 

The following thread deals with a  similar situation and explains how could this be achieved:

·         DelegateCommand.RaiseCanExecuteChanged -- Can't test it :(

 

Please let me know if this helps.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman

Jun 20, 2009 at 2:15 AM

Unfortunately, mocking out the DelegateCommand doesn't make a lot of sense in my case.  I am testing my ViewModel behavior, which is composed of several DelegateCommands.  The logic that the DelegateCommand wraps is what I want to test, so mocking it out would muddy the code significantly if I want to test the two delegates it wraps.  For example, here is a VERY simple ViewModel that I might want to test:

public class ViewModel
{
    public ViewModel()
    {
        _searchCommand = new DelegateCommand<object>(ExecuteSearch, SearchText.Length > 0);
    }

    private string _searchText;
    public string SearchText
    {
        public get { return _searchText; }
        public set
        {
            _searchText = value;
            _searchCommand.NotifyCanExecuteChanged();
        }
    }

    private readonly DelegateCommand<object> _searchCommand;
    public ICommand SearchCommand { get { return _searchCommand; } }
}

To test this, it really requires that the ViewModel relies on the concrete implementation of DelegateCommand, not a mock... since all the behavior is delegated by the DelegateCommand itself.

Because of this, simply setting the SearchText in a test will cause an exception.  Plus, I might want t test that validates the "CanExecuteChanged" event gets fired when SearchText is set.  In either case, I don't want to mock out the DelegateCommand.

Unfortunately, in the current state of the code, I cannot write tests against the CAL DelegateCommand.  Of course, it is VERY easy to implement my own... but I am hoping to not re-invent the wheel.

I will do as you suggested... submit it to the "Issue Tracker".

Thanks,

Brian