alacrity in action
| Tags: , ,

I was writing some WCF services recently (driving development with unit tests, naturally) and eventually bumped into the problem I guess all authors of WCF services eventually bump into. I had to use the OperationContext.Current.

A bit of googling revealed, unsurprisingly, I’m not the only person struggling to mock the OperationContext. It turns out there is also a solution out there. It’s called WCFMock with a nice description is this blog post: “WCFMock, a mocking framework for WCF services“.

While it looked promising I have two reservations about it. The first one hides in this line:

using WebOperationContext = System.ServiceModel.Web.MockedWebOperationContext;

When doing TDD one of the rules I try to follow is never to bend the production code for the requirements of the test. Sure, I design the code so that it is testable. However, the production code is there to fulfil the functional and non-functional requirements (or to deliver value through working software if you like) and I want to make sure I test the same code that will run live.

The other reservation I have with the proposed approach is that it doesn’t really express the dependencies of the piece of code in question explicitly.

Look at the GetProducts operation on the ProductCatalog. The following line is where we need the context:

WebOperationContext.Current.OutgoingResponse.ContentType = "application/atom+xml";

But we only need the context because we want to set the correct ContentType on the response. Using the context just happens to be the way to achieve it.

Perhaps we could replace this line with a more explicit one like this (and there’s probably still more to be improved in terms of naming)


We replaced a reference to OperationContext with a reference to operationResponse. The operationResponse should be defined as an interface and can be injected in the constructor just like the repository is in the example given. Our test method now becomes slightly shorter and more readbale

public void ShouldGetProductsFeed()
        var respository = new InMemoryProductRepository(
                new List<Product>{
            new Product { Id = "1", Category = "foo", Name = "Foo1", UnitPrice = 1 },
            new Product { Id = "2", Category = "bar", Name = "bar2", UnitPrice = 2 }
        var operationResponseHandler = new Mock<OperationResponseHandler>();
        var catalog = new ProductCatalog(repository, operationResponseHandler.Object());
        var formatter = catalog.GetProducts("foo");
        var items = formatter.Feed.Items;
        operationResponseHandler.Verify(x => x.SetContentType("application/atom+xml"));
        Assert.AreEqual(1, items.Count());
        Assert.IsTrue(items.Any(i => i.Id == "http://products/1" && i.Title.Text == "Foo1"));

I personally prefer this approach as we don’t need to go into the details of the OperationContext. Naturally, we’ll have to implement our new interface for production and it will eventually have to reference the context, but we can keep that bit separately and test it in an integration test. I would probably split and name the above unit test as well, but that’s a completely different story.

| Tags: , , ,

Ever since I started writing windows applications, even though most of them were not in C or C++, almost every time I would refer back to Charles Petzold’s Programming Windows. Whether it was Visual Basic, Delphi, .NET or even Java, there would eventually come a point, where the only way to achieve something would be to dig into the Win32 API and hook into the message loop directly.

I don’t know why I thought writing a WPF application in .NET 4 for Windows 7 would be any different.

If you start with a default empty window it is rendered nicely with a drop shadow around it (like all other windows).

However, I wanted to achieve an alternative layout, without the frame but with the shadow still on. Unfortunately setting WindowStyle="None" creates a rather blunt rectangle.

There is always the option to do something inside the client window to pretend your window actually does have a shadow but it is somewhat messy and just not as pretty (or perhaps I wasn’t ready to tinker with the Canvas and Effects enough). So will say it also comes with a performance penalty (since you would require transparent background).

Finally, with a little bit of Interop spice by calling Desktop Window Manager APIs I got the desired effect:

Mind you it only works if DWM is available (that is in Vista and Windows7) but I’m fine with that.

Here is the required code:

[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);

void ShellWindow_SourceInitialized(object sender, EventArgs e)
var helper = new WindowInteropHelper(this);
int val = 2;
DwmSetWindowAttribute(helper.Handle, 2, ref val, 4);
var m = new Margins
bottomHeight = -1,
leftWidth = -1,
rightWidth = -1,
topHeight = -1

DwmExtendFrameIntoClientArea(helper.Handle, ref m);

If you want to dig deeper there is WPF Shell Integration Library that does some of this heavy lifting for you.

P.S. Seems like Java folks have similar challenges.

| Tags: , ,

I found myself today needing some simple DTO classes for unit tests to extend existing services and controllers. We’re using .NET 3.5 so Object and Collection Initializers come in really handy for mocking out services and providing data. If the object graph is somewhat bigger crafting the data by hand is quick but somewhat cumbersome.

Yet we usually have that data somewhere and getting a hand on an instance that might just need a bit of tweaking later is easy. I really wanted something that would serialise an instance of an object (perhaps obtained while debugging) into object initialiser syntax that I can paste back into the code.

I was disappointed I couldn’t google anything useful, but then perhaps my googling skills are not as good as my coding skills so I wrote something quickly instead. A bit of reflection and recursion and it’s there.

You can download it from CodePlex

It is extremely sketchy but does the job for me.

Is anything better out there? Ideally, this could now be hooked into Visual Studio somehow to make the job even easier. Unfortunately Code Visualisers won’t do the job as they can’t be registered for “object”.