Converting event based async calls to Task

Working on a new WinPhone app the other day I encountered an interesting problem which made me scratch my head quite a bit. I needed to read the list of appointments from the phone’s calendar. No big deal, right? Using the MVVM pattern I created a separate view model for my page and a service where I wanted to put all my phone specific stuff. The initial signature of my method was

public IEnumerable<Appointment> GetAppointments();

When trying to implement the method I found out that the calendar API uses the old style event based async pattern (EAP). I tried couple of different methods to wrap the async call and the result from the event in a single method call and failed miserably. After that I tried changing the method to the new async/await style. I knew I had to return a Task<T> so the signature of my method became

public Task<IEnumerable<Appointment>> GetAppointments();

But I still did not know how to make it work. And then I finally came across the TaskCompletionSource class that already solves my problem:

public Task<IEnumerable<Appointment>> GetAppointments()
    return Task.Run(() =>
        var tcs = new TaskCompletionSource<IEnumerable<Appointment>>();

        var appts = new Appointments();
        appts.SearchCompleted += (o, e) =>
        appts.SearchAsync(DateTime.Today, DateTime.Today.AddDays(1), "appointments");

        return tcs.Task;

Beautiful, isn’t it?


In the seventies, just before I was born, Xerox (where I spent half of my career) invented MVC (model-view-controller). There are many variations of the pattern one of which is MVVM (model-view-viewmodel). While the differences between the various flavors of MVC, MVP, MVVM, etc. are in my opinion purely academic, there is absolutely no doubt in my mind that following a pattern like that can make the dev’s life a lot easier. There is somewhat of a high price for entry and a bit of a learning curve, but once you commit to it, there is no going back.

The essence of MVVM is that you separate your data (the model) from you presentation (the view) and you put all your UI logic in the view-model, where it can be developed and tested separately from the view itself. When targeting multiple platforms, in the ideal case you can reuse your models and view models across the different platforms and only code separate views.

There are several different MVVM libraries (MVVM Light, Caliburn, etc.) but it is important to understand that you do not need to bring a library in order to do MVVM, it just makes things easier. I’ve never used mvvmcross before but it promises to deliver databinding on windows, android and ios with reusable view models. It also contains a simple IoC container as well as few other tools and plugins.

mvvmcross is available as a nuget package. you add it to your projects, follow the provided (short) to-do list and you are in business. I was able to wire up a win store app with two views, view models, a service, etc. in maybe 15 or 20 minutes. Tomorrow – droids for breakfast :)

Multi-platform development with c#

The life of the mobile developer today is not easy at all. The market is segmented with at least 3 major players (counting Microsoft, not counting Blackberry). Clients/users want native look and feel and native performance on each platform. Tools like PhoneGap promising write-once-run-everywhere with html5/css/javascript unfortunately just don’t deliver on that promise.

In the recent years Microsoft, knowing that it is not quite winning the post-PC battle is trying to lure the developers to write apps for winphone/win8 by providing the best dev tools and a relatively easy path (with the partnership with xamarin) to porting apps from android or ios to winphone/win8.

Besides code reuse, there is another also important aspect of mobile (or not) development: using a modern and powerful language. In the recent years Java is trailing behind c# (catching up but still not there) and objective-c is so not in the same league as the other two that apple had to introduce a whole new language to replace it few month ago. And c# is almost everywhere – on the client, on the server, on a device, in the cloud – devs can use the same skills and the same tools for any type or size project. (in all honesty that mostly applies to javascript too – with the exception of the nice tools part).

So in the following days I’ll create a small demo of a LOB application on all mobile platforms using visual studio, c#, xamarin, an mvvm library, etc. My goal is to have a form and call a service with the data from the form writing as little code as reasonable and reusing it as much as possible.

Pipes for Windows 8

The Windows 8 version of Pipes is in the store and got a 1000 downloads in the first week. Working on the first update already with a “big orange button”™ for rate and review as well as few smaller improvements.

Download from Windows Store

#dvlupday in San Diego

I attended a DVLUP day in San Diego this weekend. The event started pretty rough with an ex-military security guard yelling at poor unwoken devs to order in lines before 8:00 AM. Besides that it was a lot of fun. I quite miss the energy of those events. Over a hundred devs sitting in a room in the Nokia office in San Diego, laptops open, coding… Several good presentations most notably on appstudio, unity and maps.

I worked on a video poker app for windows phone. I started with a code from a poker kata we did at work few weeks ago. I also had a control for a playing card that flips when tapped with a nice animation. And I build a working video poker game in less than 8h and it turned out pretty nice. I’m going to submit it to the app store in the following days for a DVLUP challenge.

I’m also planning to create an universal app based on that code base trying to reuse as much code as possible between winphone and win8 apps.

Update (5/5): the app just got published, download from the link below.


unit testing TypeScript

Few days ago TypeScript 0.97 was promoted to version 1.0RC which means that there will be no more changes for version 1.0, just bug fixes. At the same time Visual Studio 2013 Update 2 RC was announced with full TypeScript support out of the box, without having to separately download the tools. It is obvious that Microsoft are pushing TypeScript to be a first class citizen in the Visual Studio eco-system. What’s missing? Unit testing! Did they not notice? How could they call it 1.0 without a viable unit testing framework out of the box?

Here comes ReSharper 8.2 (which is not yet released, beta bits available on the JetBrains website). Best new feature – TypeScript unit testing of course!

Here is my setup:

  • Visual Studio 2013 Update 2 (RC)
  • ReSharper 8.2 (Beta)
  • PhantomJS (from installed and configured
  • QUnit definition file – qunit.d.ts (from DefinitelyTyped)

Create a new HTML TypeScript Application project in Visual Studio and delete index.html, app.ts and app.css files (like to start clean).

Create a folder “libs” and put qunit.d.ts there.

Create a folder “tests” and put a file helloWorld.ts like this:

///<reference path="../libs/qunit.d.ts"/>

test("hello world test", ()=>
    ok(true, "hello world test");

The test icon appears in front of the test method. Run the test to make sure all is configured correctly.

Now create a folder “app” and put a file MyClass.ts in it:

module MyModule
    export class MyClass
        private x: number;

        constructor(x: number)
            this.x = x;

        getX(): number
            return this.x;

Finally create a test class MyClass.Tests for that class in folder “tests”:

///<reference path="../libs/qunit.d.ts"/>
///<reference path="../app/MyClass.ts"/>

module MyModule.Tests
    test("my test", ()=>
        // arrange
        var myClass = new MyModule.MyClass(5);

        // act
        var result = myClass.getX();

        // assert 
        equal(result, 5, "result is 5");

Magic! Love it or hate it ReSharper saves the day (again)…