ILearnable .Net

May 13, 2009

EPiServer convenience Extensions

Filed under: Uncategorized — andreakn @ 08:36

We have had a great time extending the API in EPiServer to provide conveniences that are easy to use.

extensions on pagedata relating to property values:

 public static bool HasProperty(this PageData pageData, string propertyName)
{
    if (pageData == null)
        throw new ArgumentNullException("pageData");
    
    if (string.IsNullOrEmpty(propertyName))
        throw new ArgumentNullException("propertyName", "propertyName is null or empty");
   
    var propertyData = pageData.Property[propertyName];

    return propertyData != null;
}

public static bool HasValue(this PageData pageData, string propertyName)
{
    if (!HasProperty(pageData, propertyName))
        return false;

    var propertyData = pageData.Property[propertyName];

    return !propertyData.IsNull;
}

public static T  GetValue<T>(this PageData pageData, string propertyName)
{
  return GetValue<T>(pageData, propertyName, default(t) );
}

public static T GetValue<T>(this PageData pageData, string propertyName, T defaultValue)
{
   if(!HasValue(pageData, propertyName))
       return defaultValue

    var propertyData = pageData.Property[propertyName];

    try
    {
        return (T)propertyData.Value;
    }
    catch (InvalidCastException)
    {
        throw new InvalidCastException(string.Format(
                                           "Property '{0}' on page {1} is not compatible with {2}. Property type is {3} ({4}).",
                                           propertyName,
                                           pageData.DebugString(), typeof(T).Name,
                                           propertyData.Value.GetType().Name, propertyData.Type));
    }
}

Extensions relating to childpages


public static PageDataCollection GetChildren(this PageData pageData)
{
    if (pageData == null)
    {
        throw new ArgumentNullException("pageData");
    }
    return pageData.PageLink.GetChildren();
}

public static PageDataCollection GetChildren(this PageReference pageReference)
{
    if (pageReference == null)
    {
        throw new ArgumentNullException("pageReference");
    }
    return DataFactory.Instance.GetChildren(pageReference);
}

public static IList<PageReference> GetDescendents(this PageReference pageReference)
{
    if (pageReference == null)
    {
        throw new ArgumentNullException("pageReference");
    }

    return DataFactory.Instance.GetDescendents(pageReference);
}
public static PageData GetPage(this PageReference pageReference)
{
    if (pageReference == null)
    {
        throw new ArgumentNullException("pageReference");
    }
    return DataFactory.Instance.GetPage(pageReference);
}


naturallty, (for me at least) all these extension method reside in the same namespace as the classes they are extending. It makes the API super-easy to use, and we don’t have to remeber special namespaces

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: