Raba - Defend your code RSS 2.0
# Sunday, June 07, 2009

Background:

We are writing a new task for the ArcGis Explorer, this task needs to place images on the globe using its X,Y coordinates.
While for each instance on the globe we need to render its details as html.

Mission:

Rendering the objects to html, we are still didn't find the right place (on the server or on the client), but let assume that they both need to do the same job.

Ideas:

  1. Render in code:
    This is the naive way of thinking. while you are first thinking about this  problem you might think to add the object an ToHtml() method.
    Always think about the: Single Responsibility Principle (SRP).
    You can still think about Writing an HtmlWriter or HtmlRenderer class that gets object and render the object using reflection or something else, but than you will find yourself indenting you JavaScript\html\CSS in C#, for example you can watch this code:

       1: var items = new List<string> {"Shani", "Doron", "Nati", "Yossi"};
       2: var sb = new StringBuilder();
       3: items.ForEach(currentItem => 
       4:         sb.AppendFormat("<li>{0}</li>", currentItem)
       5:         );
       6: string data = sb.ToString();
    Let's think about the time we will need to change the UI from <li> to one with style or maybe even change the list to be styled as table...
     
  2. XSL - is pretty more dynamic than code, isn't it?
    So we are now looking for something more dynamic, something that we can read from a file so we can give it to our designer to play with the UI.
    This lead us to the second option: at my past all our reports were rendered using XSL files, personally I don't like this option.
    But the main reason is that we will need to serialize our objects to XML, while in most cases all you need is to render only few fields.
  3. Let's found another Template Engine:
    The first one I can think of is the MVC Engine or Brail. after some searching reading and Testing I found StringTemplate.
    Here is a simple code for using the API:

       1: var templateFolder = new StringTemplateGroup("SimpleLoopTemplate", @"\TemplateEngineTestCase\WinApp");
       2: var template = templateFolder.GetInstanceOf("3_simpleLoop");
       3: //here we are binding our list to the template
       4: template.SetAttribute("items", new List<string> {"Shani", "Doron", "Nati", "Yossi"});
       5: var data = template.ToString();

    and here is the template (file name: 3_simpleLoop.st):
       1: $items: {num1| 
       2:   <li>$num1$</li>
       3:   }$
       4: $end$
     

Conclusion:

The StringTemplate give us an API to solve our problem - and using such code and template we can render our Models to different Views.
But is this API is easy or even stable?

In later posts we will see more samples using the StringTemplate.
Hopefully samples for using Brail too.
Sunday, June 07, 2009 3:48:07 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
.Net 3.5 | Software Development | Design Patterns
# Saturday, August 18, 2007

I thought about the time we all spend on reading some well known patterns. I found this strange reading again and again the same logic. We all know the pattern but we doing it to be sure we use all the best practices.

Inspired by the book: Head First - Design Patterns, I rewrite the two basic patterns of Strategy and Observer. and create a snippets for your own use. so next time you won't need to get to Google it up.

Here are the snippets, for your own use: (right click & download)
Strategy.snippet
Observer.snippet

More to come on the next posts.


Remark: There could be a lot of variation for those patterns, those I wrote here were taken as a combination of the books samples and a few best practices I found out.
Saturday, August 18, 2007 5:33:24 PM (GMT Daylight Time, UTC+01:00)  #    Comments [3] - Trackback
.Net 2.0 | Design Patterns
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Shani Raba
Sign In
Statistics
Total Posts: 134
This Year: 1
This Month: 0
This Week: 0
Comments: 97
Cool Stuff
Add to Technorati Favorites
Themes
Pick a theme:
All Content © 2010, Shani Raba
DasBlog theme 'Business' created by Christoph De Baene (delarou)