The template method pattern: processing WCF requests

Im working on a project at the moment building a message bus using WCF. We have a submit data method that takes a number of different message types; create widget, update widget etc. We need to process these requests quite differently based on the type of message passed in but there is some commonality to the processing for all message types.

The template method pattern was really useful here, we have a base message processor that contains the common processing steps and provides a "hook" method that specific message processor sub classes can override. This base class takes care of common logic for us like storing the message payload for audit and saving any results from processing. The code looks like this:

public abstract MessageProcessorBase<TMessage>
{

public void ProcessMessage(TMessage message)
{
StoreMessage(message);
ProcessingResults results = ProcessMessageByType();
SaveProcessingResults(results);
}

protected abstract ProcessingResults ProcessMessageByType();

private void StoreMessage(TMessage)
{
...
{

private void SaveProcessingResults(ProcessingResults results)
{
...
}
}

All of these methods are private on the  MessageProcessorBase except the ProceessMessageByType which is abstract and overridden in the subtypes. Now for each message type we simply create a message processor and implement the specific processing logic.

Template message processors class diagram

The message processor base is generic meaning it can be strongly typed to each specific message type, while not being tied to any one message type. The nice thing about this is that we have a strongly typed message processor for each message type.

That's all there is to it, a tidy way of using inheritence to make our message processor class closed to change but open to extension.

Tags: , , ,

Comments

trackback
DotNetKicks.com
9/23/2010 9:08:59 AM Permalink

The template method pattern: processing WCF requests

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Daniel marbach
Daniel marbach Switzerland
9/24/2010 1:47:30 AM Permalink

Hy
We use nservicebus and nservicebus capability to have mesaage inheritance. For example you can have ICreateCommand and then Createwidgetcommand, createcheesroyalcommand. Now you can decide if you want to handle ICreateCommand by implementing IHandleMessages<ICreateCommand> or the subtypes. The cool thing is that nservicebus instantiates your handlers according to the message type, so soon as the handler for example for createcheesburgerroyalcommand is called You can have your concrete factories etc. Injected into the handler...

Daniel

trackback
DotNetShoutout
9/24/2010 3:47:58 PM Permalink

instantiate | The template method pattern: processing WCF requests

Thank you for submitting this cool story - Trackback from DotNetShoutout

pingback
topsy.com
9/25/2010 6:57:31 AM Permalink

Pingback from topsy.com

Twitter Trackbacks for
        
        instantiate | The template method pattern: processing WCF requests
        [instantiate.co.nz]
        on Topsy.com

Luke
Luke New Zealand
10/4/2010 3:00:39 AM Permalink

Thanks Daniel. It sounds like nServiceBus can do some cool stuff. I know it is good for message queuing, as an abstraction over MSMQ. It sounds like it does a lot more than that though, I'll have another look into it.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading