1
\$\begingroup\$

Just a wrapper around IsolatedStorageSettings.

The only thing i'm not sure about is isoSettings.Save(); Should i call it there? Looking like it works without it, setting [] is enough.

class IsoSettingsManager
{
    private static readonly IsolatedStorageSettings isoSettings = IsolatedStorageSettings.ApplicationSettings;

    /// <summary>
    /// Set a property
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="content"></param>
    public static void SetProperty(string propertyName, object content)
    {
        isoSettings[propertyName] = content;
        isoSettings.Save();
    }

    /// <summary>
    /// Reads value from IsolatedStorageSettings. 
    /// WARNING: returns null, if property is not found.
    /// </summary>
    /// <param name="propertyName"></param>
    /// <returns></returns>
    public static object GetProperty(string propertyName)
    {
        if (isoSettings.Contains(propertyName) && isoSettings[propertyName] != null)
                return isoSettings[propertyName];

        return null; 
    }

    /// <summary>
    /// Reads reference from IsolatedStorageSettings. 
    /// WARNING: returns default(), if property is not found.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="propertyName"></param>
    /// <returns></returns>
    public static T GetProperty<T>(string propertyName)
    {
        if (isoSettings.Contains(propertyName) && isoSettings[propertyName] != null)            
            return (T) isoSettings[propertyName];            

        return default(T);
    }

    /// <summary>
    /// Removes property, if already exists
    /// </summary>
    /// <param name="propertyName"></param>
    public static void RemoveProperty(string propertyName)
    {
        if (isoSettings.Contains(propertyName))
            isoSettings.Remove(propertyName);
    }
}
\$\endgroup\$
1
\$\begingroup\$

When you use your GetProperty method there is no way to find out did you get a real property value or some default value.

It will be more clear if you'll use a standard TryGet pattern here:

public static bool TryGetProperty<T>(string propertyName, out T result)
{
    if (isoSettings.Contains(propertyName) && isoSettings[propertyName] != null)
    {
        result = (T) isoSettings[propertyName];
        return true;            
    }

    result = default(T);
    return false;
}
\$\endgroup\$
2
  • \$\begingroup\$ The IsolatedStorageSettings supports the TryGetValue method so there is no need to create it manually. \$\endgroup\$ – Peter Kiss Oct 24 '13 at 7:16
  • \$\begingroup\$ yep, my bad. So it can be simplified. \$\endgroup\$ – Anton Sizikov Oct 24 '13 at 8:50
1
\$\begingroup\$

There are few things which could be improved. Argument validation, better error handling, duplicate code removal and potentially better API for handling missing value for a given property name. Personally, I am not a big fan of TryGet pattern.

Here's how I would write it:

public static class IsoSettingsManager
{
    private static readonly IsolatedStorageSettings isoSettings = IsolatedStorageSettings.ApplicationSettings;

    // All arguments of public methods must be validated.
    public static void SetProperty(string propertyName, object propertyValue)
    {
        if(string.IsNullOrWhiteSpace(propertyName))
            throw new ArgumentNullException("propertyName");

        //I added this check only because in your original GetProperty method
        //you treat 'null' as a missing value for a given property name. 
        //In that case, it makes sense to not allow null content to be set
        //as property value.

        //You want to carefully think about whether a null content is a valid 
        //value or not for your end users and adjust the code accordingly.
        if(propertyValue == null)
            throw new ArgumentNullException("propertyValue");

        isoSettings[propertyName] = propertyValue;
        isoSettings.Save();
    }

    public static object GetProperty(string propertyName, object alternateValue)
    {
        if(string.IsNullOrWhiteSpace(propertyName))
            throw new ArgumentNullException("propertyName");

        //We don't validate alternateValue because user may want to return 'null'
        //as an alternate.      
        return isoSettings.Contains(propertyName) ? isoSettings[propertyName] : alternateValue;
    }

    //Instead of repeating the code of non generic GetProperty method, we will
    //use it here.
    public static T GetProperty<T>(string propertyName, T alternateValue)
    {
        var propertyValue = GetProperty(propertyName, alternateValue);      

        //Let's ensure that the property value is of the right type.

        //Below logic is strict about type casting. 
        //Potentailly, CAST can also invoke conversion and if you want to adapt to 
        //that you should use the function outlined in this answer:
        //http://stackoverflow.com/questions/1399273/test-if-convert-changetype-will-work-between-two-types/1399454#1399454
        if(propertyValue.GetType() != typeof(T))
            throw new InvalidOperationException("Property value for Property Name " + propertyName + " is of type " + propertyValue.GetType().Name + " and it can not be casted to type " + typeof(T).Name);

        return (T) propertyValue;
    }

    public static T GetProperty<T>(string propertyName)
    {
        return GetProperty<T>(propertyName, default(T));
    }

    public static void RemoveProperty(string propertyName)
    {
        if(string.IsNullOrWhiteSpace(propertyName))
            throw new ArgumentNullException("propertyName");

        if (isoSettings.Contains(propertyName))
            isoSettings.Remove(propertyName);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.