Try our conversational search powered by Generative AI!

Stefan Forsberg
May 30, 2010
  5217
(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 alt.net hecklers from their beauty sleep in the process.

 

Setup

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 yr.no.

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);
   4:  
   5:     var weatherXmlData = XElement.Load("http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml");
   6:  
   7:     var today = weatherXmlData
   8:         .Descendants("time")
   9:         .FirstOrDefault();
  10:     
  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:     }
  16:         
  17: }
  18:  
  19: protected void SaveClick(object sender, EventArgs e)
  20: {
  21:     var newMainBody = NewMainBody.Text;
  22:  
  23:     newMainBody = newMainBody
  24:         .Replace(Environment.NewLine, "<br />");
  25:  
  26:     var writablePage = CurrentPage.CreateWritableClone();
  27:  
  28:     writablePage.Property["MainBody"].Value = newMainBody;
  29:  
  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.

xoxo

May 30, 2010

Comments

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

Hi,
The replace-with-line-break functionality is just what I need in my first proper EPiServer project...how do I hook up the SaveClick method in my control?
Thanks!
/Alan

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?
Thanks!
/Alan
/ Alan Allard

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog