May 23, 2010
visibility 3954
star star star star star
(2 votes)

Building a fake structure

This is sort of a follow up to my last post (How do you setup different states that depends on EpiServer page structure?). While I like the concept the code you have to write to create the structure is slightly boring to do so i wanted to try and create something that felt a bit more fluent. I spoke a bit with Joel about this and felt inspired enough to give it a go.

The basic setup remains the same from the last post, using StructureMap to inject a IPageSource implementation to your TypedPageData class. What this post is about is how to build the structure that’s returned from the GetChildren call. Is this useful? Not sure. Was it semi-fun to code? Yes.

 

Building the basic structure

I really like how you build xml-files with XElement. For those of you who has not worked with that it can look something like this

   1: new XElement("root",
   2:     new XElement("Page 1 level 1"),
   3:     new XElement("Page 2 level 1",
   4:         new XElement("Page 1 level 2")
   5:      )
   6: );

This reads so much nicer to me than all those AddChildren (or whatever the old way of doing xml-files was. I think I’ve suppressed the memories). It’s nice that you can infer what the parent to the node is by just placing it “below” it’s parent.

Being slightly inspired by this I created a Node class that works in a similar way. The class takes a PageData object, an id of the object and an optional params array of nodes. I can now define my structure like this

   1: new Node(new PageData(), 1,
   2:     new Node(new PageData(), 2),
   3:     new Node(new PageData(), 3,
   4:         new Node(new PageData(), 4)
   5:         )
   6:     );

 

Setting PageData values

Simply newing up PageData objects won’t really do us much good and I wanted to create a sort of fluent API for initializing both the default values and any other values you’d want to set on the pagedata objects.

PageDataBuilder (which is a name I came up with on my own and is in no way inspired by any other fictional / not fictional EPiServer related open source projects) is a generic class (where the generic type TTypedPageData has to be TypedPageData) with, among others,  the following methods

WithDefault – returns GetDefaultPageData for the page type

WithParameters – Takes an Action<TTypedPageData> so you can can do whatever you please to your page data object

WithCallback – If your TypedPageData class implements the interface ISetDefaultValues it’s called by this method.

Usages of this class can look like this

   1: new PageDataBuilder<PageType.ArticleContainer>().WithDefault().Get()
   2:  
   3: new PageDataBuilder<PageType.Article>().WithDefault().And().WithParameters(a => a.Heading = "Article 1").Get()
   4:  
   5: new PageDataBuilder<PageType.Article>().WithDefault().And().WithCallback().Get()
   6:  

 

You can use this in your node building like this

   1: var nodeCreator =
   2:     new Node(new PageDataBuilder<PageType.ArticleContainer>().WithDefault().Get(), 0,
   3:         new Node(new PageDataBuilder<PageType.ArticleContainer>().WithDefault().Get(), 26,
   4:             new Node(new PageDataBuilder<PageType.Article>().WithDefault().And().WithParameters(a => a.Heading = "Article 1").Get(), 5000),
   5:             new Node(new PageDataBuilder<PageType.Article>().WithDefault().And().WithCallback().Get(), 5001)
   6:         )  
   7:     );

 

If anyone is interested in this and wants to take a look at the code or anything else feel free to drop me a mail at stefan.forsberg (AT) cloudnine . se (not sure how to attach files here).

xoxo

May 23, 2010

Comments

Sep 21, 2010 10:33 AM

Great post Stefan!

I think this shows great promise and I think it would be really interesting to combine it with a fluent API for setting up fake DataFactory instances.

If you e-mail the code to me (mail@joelabrahamsson.com) I'd be happy to host it on my site so people can download it.

markus.ljung@cloudnine.se
markus.ljung@cloudnine.se Sep 21, 2010 10:33 AM

This is so cool!
It would be great to expand it with functionality to generate larger tree structures with fake data.

error Please login to comment.
Latest blogs
Finding Thomas Part 3 - The Moment of Recognition

Remember Thomas? In digital landscape, Thomas is the returning visitor who reads everything, opens every email, converts on nothing. In standard...

Ritu Madan | Jun 26, 2026

Add more scheduled job settings from the Optimizely CMS 12 admin UI -- with OptiScheduledJob.ExtraParameters

  Optimizely (EPiServer) CMS 12 ships a great scheduled-jobs framework, but it has one frustrating gap: a job has nowhere to store its own...

Binh Nguyen Thi | Jun 25, 2026

Automated Search & Navigation to Graph Migration with Claude Code

A Claude Code plugin that scans your S&N codebase, applies Graph SDK transformations, and validates the result. Install once, run one command. CMS ...

Connor Fortin | Jun 24, 2026

Migrating from Find to Graph: Lessons Learned from a Real CMS 13 Project

While migrating a search solution from Optimizely Search & Navigation (Find) to Optimizely Graph in CMS 13, I encountered several issues that were...

Binh Nguyen Thi | Jun 24, 2026

Optimizely: Upgrade Opti-ID and .NET 10 in CMS 12

Many Optimizely customers are planning their roadmap around a future migration to Optimizely CMS 13. As a result, upgrades such as Opti ID adoption...

Madhu | Jun 23, 2026 |

Understanding Optimizely Graph: Caching, Webhooks & Avoiding Stale Content (Optimizely SaaS CMS)

📌 Scope: This post covers Optimizely CMS (SaaS) only — using the official @optimizely/cms-sdk and @optimizely/cms-cli packages with Next.js 15. If...

Kiran Patil | Jun 23, 2026 |