Yoav sent me this web site. I like the idea of sharing knowledge, I also like to read and learn from open sources. This is even better: daily -public- code review...
For more background about when we use this API you can read my last post about Rendering Objects to Html Simple Rendering of query: 1: var query = new StringTemplate("SELECT $column$ FROM $table$;"); 2: query.SetAttribute("column", "name"); 3: query.SetAttribute("table", "User"); 4: var data = query.ToString();
We will refactor this sample to handle the queries from an outside source:
1: var group = new StringTemplateGroup("SimpleTemplates",@"C:\Templates\TemplateEngineTestCase"); 2: var query = group.GetInstanceOf("1_simple"); 3: query.SetAttribute("column", "name"); 4: query.SetAttribute("table", "User"); 5: var data = query.ToString();
Rows 1,2 - loading the template data from a source file.
Here is the Template (1_simple.st):
//1_simple.st
SELECT $column$ FROM $table$;
Template Anonymous Type:
In the last template usage (1_simple.st) We bind the parameters using strings only. but we also have a better options: let's assume we wrote down such template:
//2_simpleObjects.st
Full Name: $Person.FirstName$ - $Person.LastName$
But let's assume that we don't have the Person class\instance, we can simply write it using the anonymous types:
1: var user = new {FirstName = "Shani", LastName = "Raba"}; 2: var group = new StringTemplateGroup("OOTempaltes", @"C:\Templates\TemplateEngineTestCase"); 3: var query = group.GetInstanceOf("2_simpleObjects"); 4: 5: query.SetAttribute("Person", user);
You can also use this kind of bindings for setting template-attributes from each one of the Person sub-classes.
Template list:
Till now it is all pretty simple to implement by yourself, but the real issue here is the List binding feature. You can write such template:
//3_simpleLoop.st
$items: {num1|
<li>$num1$</li>
}$
$end$
and bind the list using this code:
1: var templateFolder = new StringTemplateGroup("SimpleLoopTemplates", @"C:\Tempaltes\TemplateEngineTestCase"); 2: var template = templateFolder.GetInstanceOf("3_simpleLoop"); 3: 4: template.SetAttribute("items", new List<string> {"Shani", "Doron", "Nati", "Yossi"});
Conclusion:
- The code is pretty simple, the templates engine implements good functionality and is well documented.
- The StringTemplate syntax is easy to learn and well documented.
- I still don't like it when I need to learn a new language\syntax - it will sharpen the newbie's curve.
- StringTemplate is missing good Object2Template Designer.
Dr. Alistair Cockburn will be in Israel. I recommend you this user-group: Scrum Event. come and learn from the best. See you there.
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: - 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...
- 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.
- 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.
I've been looking around for samples and some good advices for writing resumes and found this site: VisualCV Mostly, I like this Engineer profile: http://www.visualcv.com/alex1 The really good part is the "code sample" section on the top-right corner... Have fun...
Last month I've presented a simple newbie's tutorial about NTS - Net Topology Suite. Here you can download the code sample & presentation. What is it NTS (Net Topology Suite)? - implements geometry model defined in OpenGis Consortium (OGC)
- implements simple features inside the OpenGis Consortium (OGC) as Geographic Data access
- implements Some great OGC writers as: GmlWriter (Geographic markup language), WktWriter (Well Known Text)
How do I know whether I need NTS: - You are coding a GIS (Geographic Information Systems) applications
- You want to use a known Convention for Point, Polygon, Polyline etc.
- You want simple implementation for OGC
3 times No - you don't need this 3 times Yes - you must read this article and probably use it. You can read more about Kml & C#3.0 - at my last post. I will keep posting more samples in the near future.
This post inspired by Gal Ochana. Gal is a new gifted programmer at my team, she presented a great presentation about Linq to XML - you can download code here. The great bonus at her presentation was the Linq to Kml Sample, I would like to share this with you. The kml itself is an XML file at our sample it looks like this: 1: <?xml version="1.0" encoding="UTF-8"?> 2: <kml xmlns="http://www.opengis.net/kml/2.2"> 3: <Placemark> 4: <name continent="America">America</name> 5: <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description> 6: <Point> 7: <coordinates>-100.0822035425683,37.42228990140251,0</coordinates> 8: </Point> 9: </Placemark> 10: <Placemark> 11: <name continent="Africa">Africa</name> 12: <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description> 13: <Point> 14: <coordinates>20.0822035425683,20.42228990140251,0</coordinates> 15: </Point> 16: </Placemark> 17: <Placemark> 18: <name continent="Asia">Asia</name> 19: <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description> 20: <Point> 21: <coordinates>42.0822035425683,33.42228990140251,0</coordinates> 22: </Point> 23: </Placemark> 24: <Placemark> 25: <name continent="Europe">Europe</name> 26: <description>Europe</description> 27: <Point> 28: <coordinates>100.0822035425683,62.42228990140251,0</coordinates> 29: </Point> 30: </Placemark> 31: </kml>
You can read more about Kml here.
Screen-shot of the above kml in the ArcGis-Explorer:
You can see in the above Kml file sample the continents represented as a simple points, of course you can extend it to include polygons, poly-lines or anything else that valid within KML standard. The only things that differs Kml from the known Xml file is the namespace that we will need to handle in our code, let's see the query itself:
1: XNamespace ns = "http://www.opengis.net/kml/2.2"; 2: 3: XDocument kmlDoc = XDocument.Load(InputKml); 4: 5: var result = from placemark in kmlDoc.Descendants(ns + "Placemark") 6: where (placemark.Element(ns + "name") 7: .Attribute("continent").Value == 8: Continents.SelectedItem.ToString()) 9: select placemark;
line 1: declare the full namespace that will be used for the attributes and Tags line 3: loading the kml input file line 5-9: run a simple query to select a single continent, pay attention to the namespace that we concatenate for selecting the right node.
Here you can see a screen-shot with the result of the above query using the "Asia" continent as the selected item.
Enjoy.
While coding and refactoring my current project we found ourselves mocking such type of code: 1: public class MapPrints 2: { 3: protected string ImagesToReadPath = @"C:\ShaniData\Projects2008\TddSamples\data\"; 4: protected string ImagesToWritePath = @"C:\ShaniData\Projects2008\TddSamples\output\"; 5: protected Font DefaultFont = new Font("Arial", 100.0f); 6: 7: public void Print() 8: { 9: Bitmap bitmapImage = new Bitmap(3000,3000, PixelFormat.Format32bppArgb); 10: 11: using (Graphics g = Graphics.FromImage(bitmapImage)) 12: { 13: g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height)); 14: g.DrawString("Gello Graphics World", DefaultFont, Brushes.White, 100, 100); 15: SaveToFile(bitmapImage); 16: } 17: } 18: 19: private void SaveToFile(Bitmap bitmapImage) 20: { 21: string outputImage = Path.Combine(ImagesToWritePath, "RhinoDiagram.png"); 22: //Bitmap b = new Bitmap(3000,3000, g); 23: bitmapImage.Save(outputImage); 24: } 25: }
The MapPrints class is a sample I just wrote at home to demonstrate the one of the usages for Graphics class in the Print method in our applications. In this sample we save the image to an output file, but there are places that we just render this to the screen or printer etc.
I might add at this point that I prefer Asserting than mocking as a general idea. I think that mocking such code is useless. Moreover, we are using a geographic-API which produce use and produce images everywhere, so I decided to extend the images testing capabilities.
I thought how would I like to test this and spike a little bit last weekend and got this bunch of tests:
1: [TestMethod] 2: public void CompareImagesSize() 3: { 4: Image expected = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\ExpectedImage.png"); 5: Image actual = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\RhinoDiagram.png"); 6: 7: Bitmap expectedBitmap = new Bitmap(expected); 8: Bitmap actualBitmap = new Bitmap(actual); 9: 10: ImageAssert.HasTheSameSize(expectedBitmap, actualBitmap); 11: } 12: 13: [TestMethod] 14: public void CompareTwoSameImagesButWithDifferenExtension() 15: { 16: Image expected = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\Image2.png"); 17: Image actual = Bitmap.FromFile(@"C:\ShaniData\Projects2008\TddSamples\Output\Image1.jpg"); 18: 19: Bitmap expectedBitmap = new Bitmap(expected); 20: Bitmap actualBitmap = new Bitmap(actual); 21: 22: ImageAssert.AreEqual(expectedBitmap, actualBitmap); 23: }
Lines 10, 22: are using the new API: ImageAssert with its new methods: HasTheSameSize and AreEqual. In such way I can take any two images (from some print code I just wrote in my application) and test it against the expected one. You can also think about methods returning Images or bitmaps. For me it is just what I've needed
After finish spiking I start implementing my dreams, and it is also pretty simple:
1: public class ImageAssert 2: { 3: //public static void MoreMethods(Bitmap expected, Bitmap actual) 4: //{ 5: // //Compare image extensions 6: // //Compare Thumbnail... 7: //} 8: 9: public static void HasTheSameSize(Bitmap expected, Bitmap actual) 10: { 11: if ((expected.Height != actual.Height) 12: || (expected.Width != actual.Width)) 13: HandleFail("ImageAssert.HasTheSameSize", String.Empty); 14: } 15: 16: public static void AreEqual(Bitmap expected, Bitmap actual) 17: { 18: for (int i = 0; i < expected.Width; i++) 19: { 20: for (int j = 0; j < expected.Height; j++) 21: { 22: Color expectedBit = expected.GetPixel(i, j); 23: Color actualBit = actual.GetPixel(i, j); 24: if (!expectedBit.Equals(actualBit)) 25: { 26: HandleFail("ImageAssert.AreEqual", String.Empty, i, j); 27: return; 28: } 29: } 30: } 31: } 32: 33: internal static void HandleFail(string assertionName, string message, params object[] parameters) 34: { 35: throw new AssertFailedException(String.Format(assertionName)); 36: } 37: }
Lines 9-14: HasTheSameSize method: is only comparing between two images and checking that they are the same size Lines 16-31: AreEqual method: is running over all pixels of both bitmaps and comparing between them. Lines 33-37: HandleFail method: we must understand the pattern of Assert - while valid asserts do nothing, on the other side, failures should throw new AssertFailedException() such exception will be published to the relevant framework. as a red bar.
What next?
- I would be glad to get some feedback about the new ImageAssert class
- Let me know what ways do you think we can extend this class
- Any other thoughts will be appreciated too...
|