State Parameters

Jul 1, 2010 at 3:27 PM
Edited Jul 1, 2010 at 3:35 PM

Hey Orktane

I have a small suggestion for an improvement to the framework - I found extracting the state parameters using the current implementation (retrieve as objects -> cast) a little tedious and so created the following extension method, which allows me to do so in a typed fashion:

    public static class ParameterExtensions
    {
        public static bool TryGetParameter<T>(this ParametersCollection parameters, string key, out T value)
        {
            value = default(T);

            object parameter = default(T);

            if (parameters.TryGetValue(key, out parameter))
            {
                if (parameter is T)
                {
                    value = (T)parameter;

                    return true;
                }
            }

            return false;
        }
    }

This allows me to run the following code in the ViewModel:

        protected override void OnIntialize(ParametersCollection state)
        {
            Item[] items = null;

            if (state.TryGetParameter("Items", out items))
            {
                // ... do something with the items here
            }
        }

Instead of the existing functionality where the parameter needs to be retrieved as an object and casted/checked for null.  Perhaps you could include something similar in the framework in the future?

Coordinator
Jul 1, 2010 at 4:11 PM

I think I manage that :) but I was thinking rather than having a solitary extension method we might just as well put the generic overload of TryGetValue in the parameters collection class itself.

Rishi

Jul 2, 2010 at 1:42 AM

even better!