Raj Aththanayake's Blog Raj Aththanayake's Blog | C#

Refactoring Windows8 style app for Testability

4. September 2012

Couple of months ago, I wrote an article to the 2nd / September issue of the DNC Magazine on improving the testability of Win8 metro app. It is a pleasure to see that this article has come out nicely in the Magazine.

This article explains how we refactor an existing metro app and improving its testability while using the MVVM (Model View ViewModel) pattern. I also introduce the MoqRT framework and how to overcome challengers of testing metro style apps. This also includes some of the cool features in VS2012 Unit Testing support.

There are some other great articles and including a very interesting interview with Jon Skeet.

Go ahead and subscribe (it is FREE) and you will love it.

http://www.dotnetcurry.com/magazine/dnc-magazine-issue2.aspx

C#, Unit Testing

Dynamic columns via DynamicObject

7. July 2012

This might be obvious to some of you, but I thought of writing a small post, since it has a nice example of the usage of  .NET 4. DynamicObject.

Recently I had a requirement where I had to dynamically generate a tabular data (rows/cols) and use that data for Json serialization.

There are few ways of implementing this. The below example uses the .NET 4. DynamicObject to generate custom columns. I found this is quite easier and cleaner to implement than the tradition reflection based approach.

          static Table GenerateData()
           {
               var columnsSet1 = new List<Col>() {new Col("row1Col1"), new Col("row1Col2"), new Col("row1Col3")};
               var columnsSet2 = new List<Col>() {new Col("row2Col1"), new Col("row2Col2"), new Col("row2Col3")};
              
               var rows = new List<Row> {  new Row() { Cols = columnsSet1 }, 
                                           new Row() { Cols = columnsSet2 },
                                    };
               var rowList = new List<dynamic>();
               foreach (var row in rows)
               {
                   dynamic dynamicCol = new ColExpando(); //ColExpando derived from DynamicObject
                   var cols = row.Cols;
                   foreach (var col in cols)
                   {
                       dynamicCol.Column = col.Val;
                   }
                   rowList.Add(dynamicCol);
               }
               var tb = new Table { Data = rowList };
               return tb;            
           }
        public class ColExpando: DynamicObject
        {
            private readonly Dictionary<string, object> members = new Dictionary<string, object>();
            private int colAppendIndex = 0;           
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                   colAppendIndex++;
                    string key = binder.Name + colAppendIndex;
                    if (!members.ContainsKey(key))
                    {
                        members.Add(key, value);
                    }
                    else
                    {
                        members[key] = value;
                    }
                    return true;
              }
            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                    if (members.ContainsKey(binder.Name))
                    {
                        result = members[binder.Name];
                        return true;
                    }
                    return base.TryGetMember(binder, out result);
              }
            public override IEnumerable<string> GetDynamicMemberNames()
            {
                return members.Keys;
            }
        }

Note that colAppendIndex is an incremental number which appends a number in front of the key of the property name. This gives us the uniqueness of the keys in the dictionary.

If you call this method in console app and binds to json as below

 

static void Main(string[] args)
           {
               var json = JsonConvert.SerializeObject(GenerateData(), Formatting.Indented); //using Newtonsoft.Json lib
               Console.WriteLine(json);
               Console.Read();
           }
Output:
{
  "Data": [
    {
      "Column1": "row1Col1",
      "Column2": "row1Col2",
      "Column3": "row1Col3"
    },
    {
      "Column1": "row2Col1",
      "Column2": "row2Col2",
      "Column3": "row2Col3"
    }
  ]
}

C#