November 15, 2013

Deserialize JSON to Dictionary

Passing JSON is a very popular way to get data from the UI. For deserializing I am using Json.Net You can download this package from the site above or use NuGet.

It works fine for converting JSON string to simple Dictionary<string, string> or  Dictionary<string, object>

string json = @"{""key1"":""value1"",""key2"":""value2""}";
Dictionary<string, string> dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

But you can have a little problem with deserialization JSON string to Dictionary<string, List<string>> type. 
Explicit conversion like in an example failes. To solve this problem we should make conversion in two steps:
1. convert original string to Dictionary<string, object> as we have done before;
2. deserialize recieved object (which is also the JSON string) to the type List<string>.
string json = "{\"key1\":[\"value1\"],\"key2\":[\"value2\",\"value3\"]}";

Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
Dictionary<string, object> desirializedJsonObject = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

foreach (var obj in desirializedJsonObject)
 var value = JsonConvert.DeserializeObject<List<string>>(obj.Value.ToString());
 result.Add(obj.Key, value);

Looks pretty easy, but can take some time for a first. Happy coding!

October 13, 2013

Sticky notes always on top in Windows 7

Today I want to write about not programming thing but very useful feature, that I was needed. Windows 'Sticky notes' are very helpful for developers and not only for them. I often write on it some things that don't want to keep in memory. But there is a problem - they are not always on top of all other windows.

And that is very strange, because concept of a 'sticker' implies that it stays on top of things, before our eyes. There is no any option to do it, so you can write a program or use a tool like NIRCMD. I chose the last variant.

So, first you need to download it from the site And then just type the command:
C:\> nircmd.exe win settopmost class Sticky_Notes_Top_Window 1
To undo this action, type this:
C:\> nircmd.exe win settopmost class Sticky_Notes_Top_Window 0
This command changes the TOPMOST flag on all windows with the ‘Sticky_Notes_Top_Window’ class name. That's it. Hope it helps.

September 8, 2013

Dependency Injection using Ninject in MVC 4

Today let's talk how to implement DI using the most popular IoC container Ninject in MVC 4, because it differs from the previous versions of framework.

Small recall: the dependency injection is a software design pattern, which allows us easily change hard-coded dependencies between interfaces and their implementations.

Problem statement: for example we have HomeController and GuestController classes, which tightly coupled to the HomeService class as on the image below.

February 26, 2013

Singleton pattern

Singleton is using when you need a class that has only one instance, and you need to provide a global point of access to the instance. As you can notice, singleton is similar to the global variable. And as the global variable it can violates testability and scalability of your system. So you should use it carefully or not to use at all if you can. But if you need it, you can use this 'lazy' implementation. ('Lazy' means that the instantiation is not performed until an object asks for an instance, whereas global variables always consume resources when the application starts):
using System;

public class Singleton
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
         if (instance == null)
            instance = new Singleton();
         return instance;

February 24, 2013

Hello World!

Hello! My name is Alex. I've been working as a C# ASP.NET Developer for the last few years and to structure my knowledge I decided to write a blog. Also I will be glad if the information I will write here be useful for someone. As you can see at the top of the site I will be writing here mainly about C# and all that related to it. This is not my first experience of the writting a blog. But about such technical things on English I write at the first time. So if you see any mistake in my posts or English or just want to say something that can make this blog better, feel free to email me at or hit me on skype: alexzaitzev.

I want to start my blog from the post about Design Patterns. A bit later I will write about algorithms and some interesting tasks and hints in C#, but I think that Design Patters are one of the most important and useful thing, so let's start from them.
There are many articles about them, but when I need to find implementation some of them I had to visit many sites: no one has full and clear information about most of them. Not to mention their implementation. Thus next post I will devote to Singleton pattern. The easiest for understanding and using pattern.