Jan
20
2012
DVCS // TDD // BDD

Improved my development workflow

Fleur d'hibiscus à la villa cannelleFor some time now I slightly modified my development workflow and I have seen a great improvement in my developers life:

  • it started by using Git Svn in front of our central Svn
  • then I introduced NCrunch in my TDD/BDD

NCrunch is an automated parallel continuous testing tool for Visual Studio .NET. It intelligently takes responsibility for running automated tests so that you don't have to, and it gives you a huge amount of useful information about your tests (such as code coverage) inline in your IDE while you work.

 I am a fan of Test Driven Development and Behavior Driven Development, I think there are great tools in a developer toolset in quite some circumstances.

I am also a great fan of JetBrains ReSharper which I use every development day and I couldn’t work as efficiently without it. But…

So what’s the point? NCrunch propose only an automated parallel continuous run of your test. Big deal, it saves you only some keystrokes! Yeah I believed that too, but the difference was quite impacting.

I don’t need to think anymore about running the tests I just run through my TDD thinking only of my tests and code, no distraction just seeing the result of my changes directly aligned with my code.

What I really liked about NCrunch is:

  • the possibility to run only a part of the tests
  • the feedback given in the code window
  • let me focus on the code
  • the risk/progress bar
  • the impact it had on my productivity

I really encourage you to try NCrunch which is free during the beta period.

Supported testing frameworks are:

  • NUnit
  • MS Test
  • Xunit
  • MbUnit
  • MSpec

The Xunit and MbUnit test runners are provided with thanks to The Gallio Project.

Supported languages and .NET versions are:

  • C#
  • VB.NET
  • F#
  • .NET Framework v2.0 and above
  • Visual Studio 2008
  • Visual Studio 2010
  • Visual Studio 11 Developer Preview
Jan
5
2012

Extending existing .NET API to support asynchronous operations

Palmier sur la plage de la grande anse du diamant

The other day I needed a way in a project I am working on to turn a .NET API, RestSharp to name it, so that I could use it in an asynchronous way.

The goal was to have the API returning a Task<TResult> so that I could use it if I want with the Async CTP. I also wanted to have something running both on .NET 4.0 and on Windows Phone 7.5. 

To achieve this goal I used

TaskCompletionSource<TResult> Class

.NET Framework 4

and defined two helper extension methods on the RestClient class of RestSharp as this:

#region using

using System;
using System.Threading;
using System.Threading.Tasks;
using RestSharp;

#endregion

namespace Sharper
{
    public static class RestClientExtensions
    {
        public static Task<TResult> ExecuteTask<TResult>(this RestClient client,
                                                         RestRequest request) where TResult : new()
        {
            var tcs = new TaskCompletionSource<TResult>();

            WaitCallback
                asyncWork = _ =>
                                {
                                    try
                                    {
                                        client.ExecuteAsync<TResult>(request,
                                                                     response => tcs.SetResult(response.Data));
                                    }
                                    catch (Exception exc)
                                    {
                                        tcs.SetException(exc);
                                    }
                                };

            return ExecuteTask(asyncWork, tcs);
        }

        public static Task<TResult> ExecuteTask<T, TResult>(this RestClient client,
                                                            RestRequest request,
                                                            Func<T, TResult> adapter) where T : new()
        {
            var tcs = new TaskCompletionSource<TResult>();

            WaitCallback
                asyncWork = _ =>
                                {
                                    try
                                    {
                                        client.ExecuteAsync<T>(request,
                                                               response =>
                                                               tcs.SetResult(adapter.Invoke(response.Data)));
                                    }
                                    catch (Exception exc)
                                    {
                                        tcs.SetException(exc);
                                    }
                                };

            return ExecuteTask(asyncWork, tcs);
        }

        private static Task<TResult> ExecuteTask<TResult>(WaitCallback asyncWork,
                                                          TaskCompletionSource<TResult> tcs)
        {
            ThreadPool.QueueUserWorkItem(asyncWork);

            return tcs.Task;
        }
    }
}

Now I can write asynchronous code like this:

public Task<List<Project>> GetAllProjects()
{
    var request = new RestRequest { Resource = "/httpAuth/app/rest/projects", RootElement = "project" };
    request.AddHeader("Accept", "application/json");

    return _restClient.ExecuteTask<List<Project>>(request);
}

So TaskCompletionSource<T> is a nice class to know about!

You might also watch this short video by Phil Pennington to get a better idea about it:

About Laurent

Laurent Kempé

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect and certified Scrum Master.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent was awarded Most Valuable Professional (MVP) by Microsoft from April 2002 to April 2012.

JetBrains Academy Member
Certified ScrumMaster
My status

Twitter

Flickr

www.flickr.com
This is a Flickr badge showing public photos and videos from Laurent Kempé. Make your own badge here.

Month List

Page List