Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Stefan Forsberg
May 30, 2010
  5451
(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
Level Up with Optimizely's Newly Relaunched Certifications!

We're thrilled to announce the relaunch of our Optimizely Certifications—designed to help partners, customers, and developers redefine what it mean...

Satata Satez | Jan 14, 2025

Introducing AI Assistance for DBLocalizationProvider

The LocalizationProvider for Optimizely has long been a powerful tool for enhancing the localization capabilities of Optimizely CMS. Designed to ma...

Luc Gosso (MVP) | Jan 14, 2025 | Syndicated blog

Order tabs with drag and drop - Blazor

I have started to play around a little with Blazor and the best way to learn is to reimplement some old stuff for CMS12. So I took a look at my old...

Per Nergård | Jan 14, 2025

Product Recommendations - Common Pitfalls

With the added freedom and flexibility that the release of the self-service widgets feature for Product Recommendations provides you as...

Dylan Walker | Jan 14, 2025

My blog is now running using Optimizely CMS!

It's official! You are currently reading this post on my shiny new Optimizely CMS website.  In the past weeks, I have been quite busy crunching eve...

David Drouin-Prince | Jan 12, 2025 | Syndicated blog

Developer meetup - Manchester, 23rd January

Yes, it's that time of year again where tradition dictates that people reflect on the year gone by and brace themselves for the year ahead, and wha...

Paul Gruffydd | Jan 9, 2025