Tom 05:54, 31 October 2009

At my current project we are using a dispatcher class to batch requests to our WCF layer based on the Request/Response Service Layer created by Davy Brion. Once you call the Get<TResponseType>() on the dispatcher the WCF layer starts working on the requests one by one and return them in one roundtrip.

Adding requests to the dispatcher is easy:

var saveDocumentRequest = new SaveDocumentRequest();
var getRemainingDocuments = new GetRemainingDocumentsRequest();
dispatcher.Add(saveDocumentRequest, getRemainingDocuments);

 

Where the signature of the Add method in Dispatcher is like;

public void Add(params Request[] requests)

Although this syntax is not bad, it feels like so much like code from last month: not very sexy. Wouldn't be more fun if we could spice it up a bit using generics and a bit of Func?

I'd love to get rid of instantiating a variable just for the sake of adding it to my dispatcher. After a bit of fiddling around with my unit tests I came up with the following solution.

What I want is to call the Add method in a generic way and set the parameters on my request object inline, using a Func. This is best illustrated with a test:

[Test]
public void can_assign_value_using_a_func_in_a_generic_method_call()
{
    Guid id = Guid.NewGuid();
    var dispatcher = new Dispatcher();
    
    dispatcher.Add<GetPersonsRequest>(s => s.ID = id);
    
    
    //disregard ugly cast; this is only for test purposes
    var request = (GetPersonsRequest)dispatcher.Requests.First();
    request.ID.ShouldEqual(id);
}
Where the Request and dispatcher have these signatures:
public class GetPersonsRequest:Request
{
    public Guid ID { get; set; }
}

public class Dispatcher
{
    public Dispatcher()
    {
        Requests = new List<Request>();
    }

    public void Add<TRequestType>(params Func<TRequestType, object>[] funcs)
where TRequestType : Request, new() { var request = new TRequestType(); foreach (var func in funcs) { func(request); } Requests.Add(request); } public IList<Request> Requests { get; set; } }

I can now set only property in my Request type, what if it has more properties that need to be set? Just introduce the infamous params to the method signature like so:

public void Add<TRequestType>(params Func<TRequestType, object>[] funcs) 
where TRequestType:Request, new() { var request = new TRequestType(); foreach (var func in funcs) { func(request); } Requests.Add(request); }

And again the test:

[Test]
public void can_assign_two_values_using_a_func_in_a_generic_method_call()
{
    var dispatcher = new Dispatcher();
    Guid id = Guid.NewGuid();

    dispatcher.Add<GetPersonsRequest>(s => s.ID = id, t=> t.GetAllDetails = true);

    //disregard ugly cast; this is only for test purposes
    var request = (GetPersonsRequest)dispatcher.Requests.First();
    request.ID.ShouldEqual(id);
    request.GetAllDetails.ShouldBeTrue();
}

When we change the return type of the Add method to the dispatcher itself, we can then use a fluent interface to get a very friendly syntax  for making calls:

 

//input parameters for first request
var document = new object();
var userID = Guid.NewGuid();

//input parameters for second request
var dossierID = Guid.NewGuid();

dispatcher
    .Add<SaveDocumentRequest>(s => s.Document = document, t => t.UserID = userID)
    .Add<GetZaakRequest>(s => s.DossierId = dossierID);

I really like the fact that I don't have to initialize a variable and still have access to all the properties in the request object..

Tom 06:15, 20 October 2009

I'm always trying to improve on my speed in development. Yesterday I noticed Udi Dahan using the In-proc test feature of TestDriven.Net in one of his presentations at the SDC - Netherlands, like so:

 

image

 

 

I had seen this option before but as it was hidden in some submenu and the regular test method worked just fine, there seemed no benefit in using it.

 

However.

Running your tests using In-proc makes for about 25% speed difference on average! To me, this is very noticeable but the function is hidden in some context submenu, hence still slow to use. After a bit of emailing with TestDriven.Net, it appears that you can run In-proc using the DTE runner.

To map it to a shortcut key, use the following screenshot:

 

image

Tom 02:23, 20 October 2009

I have converted quite a few to Resharper recently. I'm quite the addict myself and I can't see myself using Visual Studio productively without it anymore.

Resharper does have a learning curve however since there are just because of the sheer amount of enhancements it brings to working with VS.

 

To get you up to speed using Resharper, perform these settings in Visual studio after installing:
Within Visual studio: ALT - R - O => intellisense, completion behavior =>
Automatically show completion list in 0 milliseconds
"Check" all options except Case-sensitive prefix matching
Finally, "Only those that are at least": set this to 1

 

You can watch the following screencasts:
- James Kovacs screencasts on Resharper
- My own MVP coding demo using Resharper and Rhino mocks
- Ayende demoing Rhino mocks

(pls note that you'll need the Camtasia TSCC codec to view the last two)

 

You can use keyjedi to display the shortcuts used when someone else is looking over your shoulder. This will greatly enhance the introduction within your team. This only works on x86 systems, I have compiled the code into an x64 assembly that works on my win7 machine, let me know if you need it.

Lastly, have the developers print out the Resharper keymapping (in plastic cover preferably, in case of any coffee, tea and all that :-) )

 

If you have any further questions, please let me know.

Tijmen 09:53, 15 October 2009

One of my machines refused to install updates to the .NET Framework 3.5 sp1, both through auto-update and when installing manually. I finally fixed it by using Aaron Stebner's .NET framework cleanup tool and re-installing .NET. This tool radically removes all versions of the .NET framework, including IIS artefacts, registry entries, the works. I found this tool through a post on raymond.cc.

Note that this tool should probably not be the first thing to try; uninstalling and re-installing the .NET framework versions would probably a safer bet to try first.

Tijmen 07:36, 1 October 2009
Just a quick note: there is an update out for the latest .NET framework version, that addresses issues with ASP.NET dynamic data. Amongst others, the site will not display foreign key fields in a one-to-one relationship. This KB article contains all the download links to the various versions.
Not a new issue by any means (the fix is dated January 2009), but good to know nonetheless.