Wednesday, February 25, 2009

While writing some tests for the following method I had a strange problem. When the test ran it threw a NullReferenceExceptions for my stubbed User object.

The method under test is simple enough

as is the test case

The mocking Framework is Rhino Mocks and this was the solution to the problem.

With Rhino a call to a stub (or mock) is expected only once while my method is checking the user object a number of times. Eventually the test will just check that the cookie has been set so I can change the User stub to:

Tuesday, February 24, 2009

Back when I wrote a lot of ASP my best debugging tool was Response.Write and like most people I had functions to help me handle all the information. Back to the present day and the .Net framework moved on from echoing information to the response stream but every now this method still has its uses. Take my recent example of writing a HttpModule, this code runs inside the .Net pipepline and fires various events. When I deployed it to a test server I wanted to find out when events fired and what isntances were initiated. To help me .Net has the System.Diagnostics namespace so I can write:

Debug.WriteLine("Post Authorize event has fired");

Great, but how do I view all this debugging? I could have logged it, but I wanted a nice simple solution so I used DebugView, part of the SysInternals suite.

This clever program will display anything written to the DefaultTraceListener, plus it can connect to a remote computer.

Friday, February 20, 2009

I watched Glen Blocks PDC demo for the Managed Extensibility Framework (MEF) last year and thought it would be a great tool to use if I ever created a client application that had to provide a plug-in mechanism. As I write web based business applications I didn’t think this would be any time soon. Added to this I am using the Castle Windsor Inversion of Control container so I didn’t think I had a need for another container.

Background

I’m currently working on a project that has a central application for user authentication. Each client application calls a webservice to check the user’s credentials and, if all is well, a principal is added to the HttpContext. This process is all wrapped up in a HttpModule. My task is to create a variety of cookies to authenticate users with 3rd party or legacy systems once they are authenticated. The best place to do this is within the HttpModule but this is used by a variety of applications. To solve this I used MEF to create a pluggable PostAuthorization mechanism.

MEF and the HttpModule

My goal is to be able to write a component that can set a client cookie. As it is dealing with user information is will also have to access the user stored in the HttpContext. I created this interface to start with

MEF creates dependencies at runtime whereas an IoC usually has a configuration process which defines the concrete instance to be created for the abstract services. In my httpModule I created a list of CookieSetters that MEF could populate:

By attributing the collection with [Import] I'm telling MEF to wire up all assemblies it discovers that implement ISetCookies. Now this is in place it is time to start up MEF, and this being a HttpModule the place to do the work is in the Init method

The creation of the container, which I lifted from the CodePlex site, creates:

A catalog of all the assemblies in the bin folder, anything attributed with an Export will be added to the catalog.

A composition batch that will consume the exported assemblies, anything attributed with Import will be added to the batch.

A container to hold it all and orchestrate the magic.

Now I can create my PostAuthroization event to kick off any CookieSetters MEF has found:

Here I first check that the list has been created and then call the SetCookie method on each one. Below is a trivial example of a CookieSetter

Summary

When working with different teams being able to provide simple extension points is great and I can see MEF being the tool for the job. It will also be a good tool to have alongside an IoC as the two are complimentry.

As for the code above it solves a problem for me but still needs some work as the container will be built everytime the module is loaded causing a nasty performance hit. Putting in the HttApplicationState as described by Michael Puleio will be the best solution.

About Me

I am a lead developer with over 20 years experience of creating business class enterprise
software. Most recently I have been working in the global broking sector building front
office eCommerce platforms for the Swaps Derivatives markets. I focus on producing fast,
error free financial software by creating safe multi-threaded processes and building
independent components that communicate by message passing. I use proven software
development practices, including Test Driven Development, the SOLID principles and SOA.
My love of computing started when I was 8 years old when I wrote an adventure game on
my ZX81. I loved that I could program a computer and create a game. Creating software is
still my passion. My current interest is in functional programming, particularly Haskell, F#
and Scala, and how this applies to the Finance sector.