Stefan Forsberg
May 30, 2010
  5343
(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
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024

Create custom folder type in Edit Mode

Content Folders, which are located in assets pane gadget or multichannel content gadget, allow you to add any type of block or folders. But...

Grzegorz Wiecheć | Aug 28, 2024 | Syndicated blog

Creating an Optimizely AddOn - Getting Started

When Optimizely CMS 12 was launched in the summer of 2021, I created my first AddOn for Optimizely CMS and talked about some of the lessons I learn...

Mark Stott | Aug 28, 2024