• Publisert
  • 4 min

Working with PageObjects

In my current EPiServer project we needed some sort of comment functionality on an article. After some thought of how to do this, we decided that PageObjects probably was the best solution for our needs.

I will not describe all the steps to implement this solution, only a few things that I ran into when making this. If you want to see a complete recipe on how to implement a comment solution on your pages you should check out http://world.episerver.com/Blogs/Niklas-Melinder/Dates/2010/11/EPiSocial—Using-PageObjects/

Adding new properties to a class that has already been saved once as a PageObject.
Say you have a class Comment that looks like this:
public class Comment
    {
        public DateTime Created { get; set; }
        public string Title { get; set; }        
        public string Text { get; set; }
        public Author Author { get; set; }
    }
And a class Author that looks like this:
public class Author
    {
        public string UserName { get; set; }
        public string Email { get; set; }
    }

Adding new properties to a class that has already been saved once as a PageObject.

Say you have a class Comment that looks like this:

public class Comment    {        
      public DateTime Created { get; set; }
      public string Title { get; set; }
      public string Text { get; set; }
      public Author Author { get; set; }
}

And a class Author that looks like this:

public class Author    {
      public string UserName { get; set; }
      public string Email { get; set; }
}

If you during development save a few comments on the page, and later discovers that you want more properties in a class you have to take a few steps to accomplish that. Say you want a QuotedTitle and a QuotedText to allow the user to answer a comment and quote the comment he was answering. If you then just use the same save-functionality you have used so far, you will discover that the values of the new properties will not get saved to the database. Since PageObjects are using Dynamic Data Store in the background you have to, just as with DDS, remap your store such that it contains the new properties that you want to save. To do that you can simply use this code:

//Your savemethod
public static void Save(PageComment comment, PageData page)
{
UpdateStoreDefenition<PageComment>();
//Your logic to save to the DDS here
}

//The method for remapping the store
public static void UpdateStoreDefenition()
{
DynamicDataStore store DynamicDataStoreFactory.Instance.GetStore(typeof(T)) ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(T));
StoreDefinition storeDef = StoreDefinition.Get(store.Name);
storeDef.Remap(typeof(T));
storeDef.CommitChanges();
}

 

If you want to add a new property to Author, you have to remap the Author class in the store in the same way. It is not enough to remap the Comments class even if the Comment class contains an Author property.

To do that you can use:

UpdateStoreDefenition<Author>();

 

The Name parameter in PageObjectManager

One thing that confused me a bit when first starting to work with PageObjects was the “Name” parameter in both the PageObjectManager.Save method and in the PageObjectManager.Delete method. In my solution this Name parameter is not the name of one single comment, but rather the name of my collection of comments. 

So if you for instance do this:

PageObjectManager pom = new PageObjectManager(page);
pom.Delete(“Comments”);

You will delete all the comments on that page. 

To delete just one single comment, you have to retrieve the comments on the page, filter away the one you want to delete and save the collection again.

 

Remember to clean up

Remember to clean up the database when you delete a page. One way to do this is to implement an event handler, listening on the DeletingPage event. I implemented it like this:

First, wire up the event in Application_Start in global.asax.cs.

DataFactory.Instance.DeletingPage += new PageEventHandler(CleanUpPageObjectsWhenPageIsDeletedHandler); 

Then add the event handler in the same file:

static void CleanUpPageObjectsWhenPageIsDeletedHandler(object sender, PageEventArgs e)
        {
            PageData page = DataFactory.Instance.GetPage(e.PageLink);           
            ICommentService commentService = new CommentService();           
            commentService.DeleteAll(page);
        }

 

This will delete all the PageObjects stored for that particular page (not just PageObjects of type Comment or Author).

 

Happy Coding!