April 16, 2017

Building microservices with pipes & filters

Couple month ago I made a short report on .NET meetup in Minsk about how we build microservices in our company. Report is on Russian, but if will be requests, I can make an English subtitles.

January 17, 2017

Writing Web API controller tests

I am fan of TDD principle, and always trying to write tests first. Today I want to show, how I usually test Web API controller. There are two approaches:

1. Unit test
Consider UserController class, which has POST method that registers users.
public class UserController : ApiController
    private readonly IRegistrationService registrationService;

    public UserController(IRegistrationService registrationService)
        this.registrationService = registrationService;

    public HttpResponseMessage Post([FromBody]string json)
        var model = new JavaScriptSerializer().Deserialize<RegistrationModel>(json);

        bool success = this.registrationService.Register(model);

        return new HttpResponseMessage(success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError);

January 31, 2016

Unreachable code affects the result

"Can unreachable code affects the result?" - it's very tricky question. I sure your first answer will be "No". Because it is too natural. Unreachable code means, that this code never be executed. But let's  have a look on such example:

August 30, 2015

C# 6: what's new

C# 6 was officially released about month ago, but it was in "preview" mode since the last year. So I am sure that everyone who wanted, can touch it and see all the new features. If not -  here's the list of updates in the latest version of C#.

Along with C# Microsoft released VB 14. They are both part of the Roslyn compiler, who is available on github.

String Interpolation
var str = $"Hello, {name}. You clicked {clickCount} time{{s}}";
It's good alternative to String.Format function. The same piece of code on C# 5 will looks like this
var str = String.Format("Hello, {0}. You clicked {1} time{s}", name, clickCount);

May 15, 2015

Far 3 Manager CheatSheet

From all file managers I love Far the most. And idea of creating a cheat sheet for it was long time in my head. But it was a problem for me to make a nice view for this table of shortcuts, until I accidentally found a http://www.cheatography.com/ site.
Вespite the many flaws and bugs this tool really cool! It helps me to realize a long-standing idea of creating cheat sheet for Far3. And here it is.

November 26, 2014

Befriending ASP.NET Web.API2, OWIN and Ninject

There are many articles where people writing about how to use WebAPI + Owin, much less talking about Owin + Ninject and very little gathering all together. Also in these rare articles I couldn't find working solution, so I decided to put all eggs in one basket and write how to use WebAPI + Owin + Ninject. It's very strange that most of the posts talking about using Unity, not Ninject, which I love the most (think not only I).

First, let's prepare our environment, I'm using Visual Studio 2013, but if you are still VS2012 user, you can download ASP.NET and Web Tools 2013.1 for Visual Studio 2012 from Microsoft site. Now we are ready to rock!

1. Create new solution. Choose Empty ASP.NET Web application

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!