Stefan Forsberg
May 30, 2010
(1 votes)

Design principles and testing

While I really enjoyed reading Fredrik Vigs latest megablog about EPiServer developer resources I felt slightly depressed that the area of testing and design principles was more or less  missing. This isn’t Fredriks fault of course since there aren’t (m)any articles on the subject here or on other episerver related pages. I’m in no way an expert on the field but I’m going to go out  on a limb here and write a few posts about these subjects. Maybe I’ll wake up a some test fanatics, design dorks and a few regular hecklers from their beauty sleep in the process.



To serve as a sort of base for the posts I’m going to have the following scenario:

Our page is going to display the weather for the current day. For this we’re going to be using

The page also contains a text area where a visitor can write some text and then save it. Upon save some formatting of the text is done before saving the text as the page MainBody.

The code we’re going to be working with looks like this

   1: protected override void OnLoad(System.EventArgs e)
   2: {
   3:     base.OnLoad(e);
   5:     var weatherXmlData = XElement.Load("");
   7:     var today = weatherXmlData
   8:         .Descendants("time")
   9:         .FirstOrDefault();
  11:     if(today != null)
  12:     {
  13:         Temperature.Text = today.Element("temperature").Attribute("value").Value;
  14:         WeatherSymbol.ImageUrl = string.Concat("~/", today.Element("symbol").Attribute("number").Value, ".png");
  15:     }
  17: }
  19: protected void SaveClick(object sender, EventArgs e)
  20: {
  21:     var newMainBody = NewMainBody.Text;
  23:     newMainBody = newMainBody
  24:         .Replace(Environment.NewLine, "<br />");
  26:     var writablePage = CurrentPage.CreateWritableClone();
  28:     writablePage.Property["MainBody"].Value = newMainBody;
  30:     DataFactory.Instance.Save(writablePage, SaveAction.Publish);
  31: }

In the next few posts we’re going to refactor this code and write some tests for it. What I’m aiming for is to sort of give an explanation of some basic design principles, how they relate to testing and why it should matter to you. I also hope to have created world peace in the process.


May 30, 2010


Sep 21, 2010 10:33 AM

This looks promising. When do I get to read the rest?

Sep 21, 2010 10:33 AM

I hope to have the first part up in the next two days.
/ Stefan Forsberg

Sep 21, 2010 10:33 AM

This is a really nice idea. I'm very much looking forward to world peace ;)
/ Mark Everard

Sep 21, 2010 10:33 AM

The replace-with-line-break functionality is just what I need in my first proper EPiServer do I hook up the SaveClick method in my control?

Sep 21, 2010 10:33 AM

Hi again, what I'm really asking here is whether or not there's a way to hook-up this replace-function to the on-page editing publish action. I suspect the only way to do this is to create a custom property + control - i'll do it if it is the only way, but i'm a bit short of time...thanks!

Sep 21, 2010 10:33 AM

Hi Stefan,
Thanks for the tip! I've created a custom PlugInAttribute that replaces newlines with br, that process works nicely - I actually used datafactory's publishing event for this. However, now my control renders the
literally. I'm using an episerver long string property to display the text. Now maybe I misunderstood the sdk but I thought if UISafeHtmlTags defines br then it should render as html. What am I missing here? Can I not use the long string property in this way? Any ideas?
/ Alan Allard

Please login to comment.
Latest blogs
Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog

Dynamic Route in ASP.NET Core When MapDynamicControllerRoute Does Not Work

Background Creating one of the add-on for Optimizely I had to deal with challenge to register dynamically route for the API controller. Dynamic rou...

valdis | Sep 25, 2022 | Syndicated blog

404 Error on Static Assets Within an Optimizely plugin

Background With the move to CMS 12 and .NET 5/6, developers are now able to build Plugins and Extensions using Razor Class Libraries (RCL).  These...

Mark Stott | Sep 23, 2022