Faking Dynamic Properties with PageTypeBuilder in EPiServer

Posted on 21 July 2010 and tagged with , , . Short link:

When building websites in EPiServer you sometimes want a value of a property to be inherited from the parent page. Previously we used Dynamic Properties to have properties inherited, but when using PageTypeBuilder we are spolied with being able to code all our page type properties programactially in classes. However PageTypeBuilder doesn’t support creating Dynamic Properties, but that doesn’t stop us from creating the same functionality with ordinary properties.

After reading Joel’s blog post about dynamic properties with PageTypeBuilder and trying out the snippets of code he wrote, I decided to convert it into an extension method to make it easier to implement in more properties and projects.

Extension Method
public static TProperty GetFakeDynamicProperty<TPageData, TProperty>(this TPageData pageData, Expression<Func<TPageData, TProperty>> expression) where TPageData : PageData where TProperty : class
{
    if (pageData == null) return null;
    if (expression == null) throw new ArgumentException("Missing expression parameter", "expression");

    TProperty value = pageData.GetPropertyValue(expression);

    if (value == null && pageData.ParentLink != null && pageData.PageLink != PageReference.StartPage) {
        var page = DataFactory.Instance.GetPage(pageData.ParentLink) as TPageData;
        value = page != null ? expression.Compile()(page) : null;
    }

    return value;
}
Sample Usage
[PageTypeProperty(Type = typeof(PropertyString), Tab = typeof(Information))]
public string MyInheirtedProperty
{
    get { return this.GetFakeDynamicProperty(page => page.MyInheirtedProperty); }
}

If you know any improvements that can be made to this method – please do leave a comment.

 

Comments

No comments yet.

Write a comment

Required: Login with OpenID to post a comment. It is quick, easy and safe

Name: *
Email:
(never displayed - used for Gravatar)
Website:
Message: * Allowed tags: b, i, a, code, pre, blockquote, strike