Stefan Forsberg
May 30, 2010
  5398
(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
Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog

ExcludeDeleted(): Prevent Trashed Content from Appearing in Search Results

Introduction In Optimizely CMS, content that is moved to the trash can still appear in search results if it’s not explicitly excluded using the...

Ashish Rasal | Nov 7, 2024

CMS + CMP + Graph integration

We have just released a new package https://nuget.optimizely.com/package/?id=EPiServer.Cms.WelcomeIntegration.Graph which changes the way CMS fetch...

Bartosz Sekula | Nov 5, 2024

Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog