TopMenu

Wednesday, 25 September 2013

I’ve been a big fan of JSON.Net since past few years. Always helping me with less code and high performance conversion of JSON to C# objects. Thanks to James who made it possible as a fastest framework for manipulating JSON with a great documentation. In this post we will talk about a json format where a list of json objects presents with no root elements. This would be easy if you have root element you can use JObject of json.net. But in case you don’t then this would throw an exception.

You can use simple Jsonconvert.Deserialize<yourType>(jsonString) to get the typed object from a JSON. But his can throw an error if the items in Json have field with incorrect value. Let’s an int field have some alphabetic character. The exception will thrown for parsing and you’ll have very little details on error.

Now if you need to parse it as list and let’s say you want to find elements who have fields which are not of correct type as it should be then we can filter them out and log them somewhere. And so you will get a complete list of correct items and corrupted data.

We’ll use a JArray class from namespace Newtonsoft.Json.Linq to parse the data as list of array of object and then we’ll convert one by one each item to typed object and add it to list. Here’s the sample generic deserializer that can parse Json and get the valid and invalid elements.

Wednesday, 18 September 2013

There were new features introduced in .Net 4.0 and one of the awesome feature was addition of System.Dynamic in framework library. With this dynamic objects C# can provide dynamic language features like Python.

A short brief of Dynamic objects is, Dynamic objects expose members such as properties and methods at run time, instead of in at compile time. This enables you to create objects to work with structures that do not match a static type or format. For example, you can use a dynamic object to reference the HTML Document Object Model (DOM), which can contain any combination of valid HTML markup elements and attributes.

People still gets confused with Dynamic vs Anonymous types. Just to clarify Anonymous types cause the compile time errors. While Dynamic types work upon run time late binding for object properties. Let’s take an example:

var anonymousObject = new { Name=”Anonymous”, Value=”Foo” } ;

if you use anonymouseObject.Index then It’ll throw an compile time exception because it only know the Name and Value as it’s property. But using Dynamic you can use object.UnknownProperty will not cause compile time error.Let suppose you have requirement to create a object with properties which could be in any no. and of any type you don’t know. So how will you do this operation? I’ll introduce you with an exciting type of class called ExpandoObject with in System.Dynamic.

This will automatically append these properties to the object. Now if you ask for runTimeObject.OtherUnknownProperty then it’ll not give any error during compilation but this will be check when you run the program. So you can also check if the object have this kind of property before using any runtime property. Behind the scene it’s storing these property as a Dictionary. So you can cast the runTimeObject to IDictionary<string,object> to get the list of late bound properties on the object.

Isn’t is cool? Yes it is. But beware before using it in your ordinary scenario can cost you some CPU time. These are runtime late binding operations which requires its own time to resolve the symbols and so are less optimized than normal early binding cases.

So choose wisely with your scenario. In some cases this is really an awesome feature provided in recent version of CSharp. I would say that CSharp is not just a Object Oriented language but it’s adapting features of Functional, Dynamic language. That’s why it’ll will long last than any language in the programming world.

If you enjoyed reading this then please leave a comment or suggestions.

Friday, 6 September 2013

This post is about a question that people often asks when writing Unit tests and What troubles a developer is:

When should I stop writing Unit tests of something(Let say a simple Function)?

Lets talk about something known as Code coverage feature in visual studio(Premium/Ultimate editions). Most of the people don’t even know how it can help identifying unused code. Code coverage actually do the instrumenting of the assembly with special symbols putting over each statement and then analyse if that is being called when the code was getting executed. In visual studio this feature is added with Test manager. So you can run your unit tests via coverage tool and analyse the report how much production code is being covered by your unit tests.

Lets see how to run tests with coverage in simple steps:

1. In visual studio 2012 (Premium/Ultimate) you’ll find the option Test –> Analyze code coverage. Now there’s an option to run a single test coverage or all tests coverage.

Note: This option will be enabled only when your solution have UnitTests available. For unit tests written with Nunit you will need an Nuget package for Nunit Test Adapter. If you want then you can also download the Nunit VS Extension by searching into the VS Extension online.

2. Now select Run All Tests option and the magic will start. The coverage tool will be automatically launched and will run all Unit tests.

3. Once all the unit tests are done with running. You’ll see a coverage report in Code coverage Results window which will give you coverage in %.

In this report you’ll see in the Covered column the percentage of every function. And if this report says 100% then

Writing Unit tests at the moment.

Though as a standard testing strategy you can stop when coverage is >95%.