Jonathan George's Blog

Introducing Who Can Help Me? – A S#arp Architecture Demo Site

Posted in Technical Stuff by Jonathan on December 21, 2009

A couple of months ago, myself and some colleagues had the pleasure of pushing the metaphorical big red button and seeing www.fancydressoutfitters.co.uk go live. We all agreed it was one of the best projects we’d worked on in a long time, for a number of reasons. Those reasons can be split into two categories: the team and the technology.

The core dev team was mostly made up of people who’d worked with one another before. For example I first worked with James back in 2002, well before either of us joined Conchango (as it then was.) James had spent a lot of time working with Howard on Tesco Digital, and although I’d never worked with Howard directly I’d had a lot of contact with him on my previous project. We’d all previously worked with Ling (our data architect), Justin (developer, yet to create his own online presence for reasons unknown) and Naz (tester, ditto) on other projects over the previous 3 years.

On most projects, the team spends a fair amount of time up front becoming effective, and a big part of that is getting to know one another – strengths, weaknesses, the way we all communicate and so on. For us, there were only a couple of people that none of us had worked with on previous projects, and that made it simple for the team as a whole to gel really quickly. The process was simplified even further by a great project manager who knew how to make things happen without needing to control the dev team, and a business analyst who managed to take a huge amount of grief from us all about his gratuitous use of clip art in his Powerpoint presentations whilst at the same time winning the respect of the client for the speed with which he understood how they worked internally.

Also, because the team was well resourced up front, Howard and James were able to spend the time laying the appropriate foundations. James has talked about this previously, so I won’t cover it again, but suffice it to say that the time invested up front paid massive dividends later in the project.

The foundations they laid included the core software stack we would use and the tools and techniques we would adopt. They decided that the core of the site would be based on ASP.NET MVC and S#arp Architecture, which is a best practice framwork joining MVC with NHibernate. They also picked some other bits and pieces to reduce friction – things like Spark, AutoMapper and xVal – and James decided that the project would use Behaviour Driven Development as a testing approach.

When all of these things came together, one thing that stood out was how clean the resulting solution became. Core design concepts such as separation of concerns, encapsulation and dependency injection were baked in, making the code clean and easy to test. The layers of the solution were well defined and understood, meaning it was always obvious where a particular piece of code should live and it was easy to discuss the codebase between us because we were all talking the same language. The adoption of BDD finally made using a test-driven approach make sense for those like me who always found it a challenge.

As the project neared it’s end, myself, James and Howard wrote some blog posts talking about various aspects of the solution, but it became hard for all of us to pull out specific bits of code to talk about because removing them from the wider context of the solution caused them to lose their meaning. Howard suggested that we could address this – as well as giving something back to the community that gave us such great software – by putting together an app based on the same architecture and publishing the source code. We could then use it to talk around specific features or areas of the architecture, and at the same time demonstrate to the community the approach we took for the FDO site.

A couple of years back, Howard spent a few hours one evening creating an app called “Who can help me?” Originally intended to capture information about the skills and experience available within Conchango, it took him about 3 hours to put together using ASP.NET WebForms and Linq to SQL. It was the ideal application for our purpose; it has a practical application (we still use it inside EMCC) and it’s simple so the focus can be on the solution design rather than the business logic.

So, here it is: Who Can Help  Me, built using S#arp Architecture, NHibernate, Spark View Engine, xVal, AutoMapper, Castle Windsor, MSpec, RhinoMocks and PostSharp. We’ve pulled in some new bits, such as MEF and DotNetOpenAuth, hooked it up to Twitter using TweetSharp to demonstrate the pattern for external service integration, and published the whole lot on Codeplex.

The result is definitely more complex than the problem requires, but that’s ok – as I mentioned above, we specifically chose a simple application to demonstrate an enterprise level architectural approach with the hope that the focus could be on the latter. So please have a play with the live site, download the code, give it the once over and let us know what you think. We’ll be blogging about the bits we find interesting (links below), as well as tidying up the bits we’re not so keen on and we’d love to hear any feedback you have.

We’ll also be using the hashtag #WCHM on Twitter when we talk about this, so keep an eye out.

@jon_george1

Add Timeouts to your methods with PostSharp

Posted in Technical Stuff by Jonathan on September 15, 2009

Note: This was originally posted on my old blog at the EMC Consulting Blogs site.

In my previous post, “Make methods fire and forget with PostSharp”, I showed how a PostSharp aspect could be used to stop non-critical methods from blocking execution. Staying on a similar theme, in this post I will show how you can use the AOP framework to add timeouts to methods.

In one of my current projects, we’re using Microsoft Velocity CTP3. Whilst this is proving to be pretty good, it has an annoying tendancy to take the entire site down with it if it blows up. We therefore added some code to allow the site to keep running if the cache stops working so that the problem could be rectified without causing more than a slowdown. This code worked perfectly, but I hit the minor snag that Velocity operations like DataCacheFactory.GetCache appear to have something like a 10 second timeout. Our app will typically make tens of calls to the cache to generate a single page so whilst I stopped the site crashing, it was still unusable.

I couldn’t find a way to lower the timeout in Velocity, so I had to implement it in my own methods. The standard code for adding a 1 second timeout in a method would look something like this:

  1. public class TimeoutWithoutPostsharp
  2. {
  3.     public object MyMethod(object param)
  4.     {
  5.         var methodDelegate = new Func<object, object>(this.MyMethodInner);
  6.         var result = methodDelegate.BeginInvoke(
  7.             param,
  8.             null,
  9.             null);
  10.         if (!result.AsyncWaitHandle.WaitOne(1000))
  11.         {
  12.             throw new TimeoutException();
  13.         }
  14.         return methodDelegate.EndInvoke(result);
  15.     }
  16.     private object MyMethodInner(object param)
  17.     {
  18.         // Do the work
  19.     }
  20. \

We create a delegate to the method proper, which we can then invoke using BeginInvoke() on the delegate. This returns an IAsyncResult, from which we can use the AsyncWaitHandle to wait for the method to complete. The WaitOne method allows us to specify the timeout interval we want, and returns true if the method completes within that period. If it doesn’t, we raise a TimeoutException, otherwise we call EndInvoke() on the delegate and return the correct value.

If you read my previous post you’ll know what to expect with PostSharp in play…

  1. public class TimeoutWithPostSharp
  2. {
  3.     [Timeout(1000)]
  4.     public object MyMethod(object param)
  5.     {
  6.         // Do the work
  7.     }
  8. }

Again, all the plumbing is eliminated and you end up with clean code in which it’s obvious what’s going on. As with the Asynchronous attribute, this is implemented by inheriting the PostSharp.Laos.OnMethodInvocationAspect base class, which replaces the original method with a generated one that calls into your attribute. The code is straightforward, following the pattern shown in the first code snippet:

  1. [Serializable]
  2. public class TimeoutAttribute : OnMethodInvocationAspect
  3. {
  4.     /// <summary>
  5.     /// Initializes a new instance of the <see cref=”TimeoutAttribute”/> class.
  6.     /// </summary>
  7.     /// <param name=”timeoutMs”>
  8.     /// The timeout period in ms.
  9.     /// </param>
  10.     public TimeoutAttribute(int timeoutMs)
  11.     {
  12.         this.TimeoutMs = timeoutMs;
  13.     }
  14.     /// <summary>
  15.     /// The timeout period in ms.
  16.     /// </summary>
  17.     public int TimeoutMs { get; set; }
  18.     public override void OnInvocation(MethodInvocationEventArgs eventArgs)
  19.     {
  20.         Action proceedAction = eventArgs.Proceed;
  21.         IAsyncResult result = proceedAction.BeginInvoke(
  22.             null,
  23.             null);
  24.         bool completed = result.AsyncWaitHandle.WaitOne(this.TimeoutMs);
  25.         if (!completed)
  26.         {
  27.             throw new TimeoutException();
  28.         }
  29.         proceedAction.EndInvoke(result);
  30.     }
  31. }

The code for this aspect, along with some unit tests to prove I’m not making all this up, can be downloaded from Codeplex.

@jon_george1

Tagged with: ,