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, April 11, 2009

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...

Saturday, April 11, 2009 5:56:02 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
Life | Software Development
# Friday, April 10, 2009

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:

  1. You are coding a GIS (Geographic Information Systems) applications
  2. You want to use a known Convention for Point, Polygon, Polyline etc.
  3. 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.

Friday, April 10, 2009 10:15:45 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
.Net 3.5 | C#3.0 | GIS

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.

Friday, April 10, 2009 10:03:14 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
.Net 3.5 | C#3.0 | LINQ | GIS | AGX
# Thursday, March 26, 2009

Next Friday (3.4.09) will be the second alt.net conference
You can read more here about alt.net 

Registration to the event at facebook

I'll be there.

Thursday, March 26, 2009 11:50:24 AM (GMT Standard Time, UTC+00:00)  #    Comments [2] - Trackback
alt-net-israel
# Sunday, December 21, 2008

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...
Sunday, December 21, 2008 10:57:27 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
C#3.0 | Software Development | TDD
# Friday, December 19, 2008

Dev Academy 3 just finished, but if you feel you missed a lecture or maybe missed the whole day, you can sit back download the code and full length lectures.

You can find everything in here http://www.microsoft.com/israel/msdn/devacademy3/2.aspx.
Pay attention: while the presentation were written in English, the session all of them were in Hebrew but you can also enjoy the code.

I  recommend on this lectures:

  • Dev301, Eyal Vardi lectures about Http Web Services: Great performer, good subject, lots of code
  • Dev 402, Sasha Goldshtein lectures about Concurrent Programming: I didn't like his style, but still some great samples.
  • Arc 301, Yair Siwek lectures about "Velocity": Great performer, funny guy. You must watch this session.

Enjoy.

Friday, December 19, 2008 4:04:19 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
.Net 3.5 | Life | WCF

For the last 3 months we suffer from many problems on our ArcGis-Servers.
The main problem is our architecture which planned on 4-cores (dual) + 12 GB RAM, but the main problem is that our server holds Win2003-32bit, which can handle only 4GB RAM.

This lack of 8GB causes us many page-faults. our SOC.exe causes our server to hold the server on 4 GB RAM permanently. (which means 100%)

 

Of course That we first need to extend our architecture to have more Spatial-Servers to deal the pressure but it will take time till buying and installing new servers, in the meanwhile we are trying to figure out creative ways to decrease the RAM Usages.


Yesterday Mody (from Systematics, ESRI's representative in Israel) sent me this link:

http://webhelp.esri.com/arcgisserver/9.3/dotNet/index.htm#tuning_services.htm (read the recycle section)

The article is talking about the 9.3 version (while we are using the 9.2 sp1) but it still help me understand something about how the people at Redland think.

 

Quote from the Recycle section: "Recycling destroys and re-creates all running instances of a service, whether or not those instances are above the minimum specified. To periodically return the number of running instances to the minimum specified, the service must be stopped and restarted."

It says that the recycle:

1) Will re-create all instances - which means that the "in use" number will be set to 0 - on every recycle.

2) It won't Change the number of "available instances" to the minimum (for example if we set minimum=4 and maximum=8, and before recycle the available=5, after recycle it will still be on 5)

 

Pros

  • ESRI's idea says that If your machine sometime had 5 users instead of the 4 you planned - so the machine needed to create the new instance. Such scenario can happen again, so They won't kill the instance at your recycle. and the next time you will need the 5th instance all the machine will have to do - is take it (without paying the creation time)

Cons

  • Machines that holds many types of Server-Objects and each of them will grow up above its minimum, it will cause the machine to work very slow, cause each instance take more memory, While the idea that the 5th user will return it more obvious that not all server-objects instances will be back to their high level usages.
    More instance -> Needed more CPU, even if they are not in use -> machine will work slower even with no less users.

 

My New Feature request:

I would like to have  recycle-preferences\options, For example: I would like to choose between two types of recycle:
The first type -  recycle and re-init the available instance to minimum.
The second type - recycle only. (like the one exist today)
Moreover, I would like to set more than one recycle item per server-object.
For Example:
I can set two recycle items: the first recycle will be twice a day and won't change the available instances(the second type), while the second type will be once a week, and will set the available instances to the minimum.

 

Enjoy.

Friday, December 19, 2008 12:08:42 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
GIS | ArcGis Server | Software Development
# Friday, November 07, 2008

After installing the TeamCity it creates the Data Directory in the user profile (c:\document and settings\[username]\.buildserver).
The .buildserver directory holds each of your project's configurations and project's output (artifacts). After a month of using the TeamCity we found out that the relevant user (the one that installed the TeamCity) profile become very huge which caused him almost 15 minutes for each logon (Loading the profile).

I tried changing this path using the TeamCity Web Site, but there you can find the parameter TeamCity.Data.Path as read-only.
After reading a little bit, I found the Tomcat6w.exe command.
Here are the actions for changing the path:

  • run this command: 
   1: C:\TeamCity\Bin\Tomcat6w.exe //ES//TeamCity
  • a configuration window will be opened, now you can find there some parameters that can be changed only on starting the service
  • at the "Java Options:" text box, you will find the TeamCity.Data.Path parameter, change it to your your wanted directory, for example: C:\BuildServer

Pay attention that you need to restart the service for causing the new parameters to be initiated.

Friday, November 07, 2008 7:21:10 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
Build Server | Agile
# Saturday, August 02, 2008

After almost two years on Pasha's shared host (ServerGrid) I've finally got my blog to have its own home, I am hosting on GoDaddy.com.
{Pasha - thank you for everything}

What's new:

  • new host server
  • new theme
  • new dasblog version (2.1)

Things to be done (or: still to fix):

  • broken links - Images are not located at the right directory
  • blogroll - I had the default blogroll which is not my real reading list

I am looking forward to hear you comments.

Saturday, August 02, 2008 4:13:25 PM (GMT Daylight Time, UTC+01:00)  #    Comments [4] - Trackback
Life | My Site
Archive
<June 2009>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
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: 139
This Year: 6
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)