Strongly typed access to EPiServer’s Page Objects with Page Type Builder

While Page Type Builder doesn’t have any specific support for Page Objects at the moment it does have a neat little extension method for PageData that you can use to avoid using magic strings when working with Page Objects.

To illustrate this let’s look at an example that I’ve blatantly stolen from a post by Paul Smith:

PageData pageData = // Obtain the PageData instance to work with

PageObjectManager pom = new PageObjectManager(pageData);

Rating rating = pom.Load<Rating>("Rating");

if (rating != null)
{
    // Do something with the rating
}

In the example above an object of type Rating is retrieved for a page (the pageData variable). To do so we need to specify the name of the Page Object with a string (“Rating”).

If we are using Page Type Builder we can add a code property to those page types that should have a rating, like this:

[PageType]
public class Article : TypedPageData
{
    public Rating Rating { get; set; }
}

Assuming that we know that the type of the page we can then utilize Page Type Builders GetPropertyName method to replace the magic string:

Article pageData = (Article) // Obtain the PageData instance to work with

PageObjectManager pom = new PageObjectManager(pageData);

Rating rating = 
    pom.Load<Rating>(pageData.GetPropertyName(p => p.Rating));

if (rating != null)
{
    // Do something with the rating
}

Of course this means that we have a property in our page type class that doesn’t do anything, we only use it to retrieve it’s name. To fix that we can move the code for retrieving the Page Object into the property.

[PageType]
public class Article : TypedPageData
{
    public Rating Rating 
    {
        get
        {
             var manager = new PageObjectManager(this);

             return manager.Load<Rating>(
                 this.GetPropertyName(p => p.Rating));
        }
    }
}
With the Page Object retrieval stuff extracted our original code looks like this:
Article pageData = (Article) // Obtain the PageData instance to work with

if (pageData.Rating != null)
{
    // Do something with the rating
}

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Comments

  1. Tom Stenius's avatar

    Tom Stenius 1 years ago

    Sweet! This is really nice. Never been fund of magic strings ;)

  2. Niklas Melinder's avatar

    Niklas Melinder 1 years ago

    Neat solution!

  3. Daniel Berg's avatar

    Daniel Berg 1 years ago

    Nice!

  4. Oskar Zetterberg's avatar

    Oskar Zetterberg 1 years ago

    Excellent, a real neat solution.

Follow me on Twitter

  1. @tim_abell The gigantic ones are for customers who specifically ask for them only :) 1 months ago
  2. Looking to buy a gigantic easter egg filled with candy for delivery in Stockholm. Any recommendations? 1 months ago
  3. @strandberg_m Du måste skriva om resultatet efteråt! 1 months ago
follow me

Latest comments

  1. Joel Abrahamsson wrote "Hi Jonas! The fluent API is really geared towards working..." on Building a search page for an EPiServer site using Truffler
  2. Jonas wrote "Thank you for one more great write up! If you're not lucky ..." on Building a search page for an EPiServer site using Truffler
  3. David Knipe wrote "The CategoriesFacet method will save me a load of headaches ..." on Cool new features in the Truffler .NET API

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more