Just a daily time saver, for reuse at another time.

Any old time windows developer, will remember the fun of using the ini files with GetPrivateProfileString. As much as ini files sucked there is one nice aspect of that call – you can setup a default value it is to return in case a value is not specified in the file. Similarly in a daily episerver programming you usually want to read a value but if one is not epcified you will usually want to use another value and just move along with the progress, and not really care to have an if there to do the filling in. Not to limit the property to any specific type – the task can be nicely solved with generics to handle pretty much any type of property.

/// <summary> /// Gets a property from a page by its name - if a property does not exist or is not set - returns a default value. /// </summary> /// <typeparam name="T">The type of the property</typeparam> /// <param name="page">The page to pull the data from</param> /// <param name="propertyName">The name of the property</param> /// <param name="defaultValue">The value to be returned if the property does not exist or is not set.</param> /// <returns>The value of the property.</returns> public static T GetPropertyDefault<T>(PageData page, string propertyName, T defaultValue) { PropertyData property = page.Property[propertyName]; if (property == null || property.Value == null) { return defaultValue; } else { return (T) property.Value; } }

It’s hard not to appreciate how clean that resolve is (with strong typing being forced with use of generics):

string link = PageUtility.GetPropertyDefault(CurrentPage, "ALink", "#top");

Another speedup coming from the TryParse background – when I want to deviate from the normal execution if a property is not defined but still have a clean code and strong typing…

/// <summary> /// Gets a property from a page by its name - if a property does not exist or is not set, /// returns the presence status of the property existence as a boolean value. /// </summary> /// <typeparam name="T">The type of the property.</typeparam> /// <param name="page">The page to pull the data from</param> /// <param name="propertyName">The name of the property</param> /// <param name="value">The value to be returned if the property exist and has a value, /// if the proeprty does not exist or is not set the value witll remain unchanged.</param> /// <returns>True if the property was retrieved succesfully, false otherwise.</returns> public static bool TryGetProperty<T>(PageData page, string propertyName, ref T value) { PropertyData property = page.Property[propertyName]; if (property == null || property.Value == null) { return false; } value = (T)property.Value; return true; }
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...



This entry (Permalink) was posted on Friday, November 9th, 2007 at 2:09 pm and is filed under .Net Framework, ASP.NET, EPiServer, Software Development, Web applications. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.