Living in the Tech Avalanche Generation

A practitioner’s introspective on technology

SOA and Messages - What’s in a name

So the question is what’s in a name, when it comes to considering messages of the SOA variety, or to be more precise in a messaging system? Well I guess it depends on who you ask doesn’t it and let’s for argument sake extend the idea of the ‘who’ to include the implementing technologies of the day and any of their accompanying GBE (guidance by example). Consider this fragment of WCF code that declares the signature of a service. The example comes from the MSDN stock trader example application.

Listing 1.0

[ServiceContract(Name = "OrderProcessorService",
    Namespace = "http://Trade.TraderOrderHost")]
public interface IOrderProcessor
{
    [OperationContract(Action = "SubmitOrderTransactedQueue",
        IsOneWay = true)]
    void SubmitOrderTransactedQueue(OrderDataModel order);

    [OperationContract(Action = "SubmitOrder",
        IsOneWay = true)]
    void SubmitOrder(OrderDataModel order);
}

Self Evident?

The first thing I would like to highlight about this example is the name of the service itself, it has a very generic sounding name that does not say a lot about the domain in which we are processing orders. True enough there is some clue in the WCF ServiceContract attribute property assignments but is that sufficient? The next thing I want to point out is that when an order is submitted with the intent that it should be stored in a transactional queue, the service operation responsible has been named specifically to speak to that intent (of storing in a queue). The fact that the message is stored in a queue is an implementation detail and on the surface it appears  has absolutely no association to the business context of the operation, which is to “submit an order”. If on the other hand there is a business requirement to have *certain* messages stored in a durable transactional queue, we should probably be taking those business considerations into account with respect to naming the message handler (operation). Making some assumptions, perhaps this would a better naming scheme for that scenario:

Listing 2.0

[OperationContract(Action = "SubmitStrategicCustomerOrder", IsOneWay = true)]
void SubmitStrategicCustomerOrder(OrderDataModel order);

Next up lets focus on the name of the operations single argument OrderDataModel, which is significant for a couple of reasons. Firstly, is this operator really a model – its name says it is? Certainly not if we want to hold it up to the DDD definition, it carries no behaviour with it – it’s just data, information about the order that is being “submitted”. So is OrderDataModel a DTO? If feels like a DTO doesn’t it, thus we should ask what is the message? I am not going to get too heavily into a discussion of what constitutes the ‘message’ when it comes to SOAP services, only to reflect that many consider it to be the combination of the operations and their operators, whilst some consider it to be just the operators alone and others don’t speak of messages at all and consider an operations arguments as nothing more than DTO’s.

Let’s take a different approach to this problem, what if we were to name the contract for this service IProcessOrders and include handlers (methods) for cancelling the order amongst other things.

Listing 3.0

public class StockTradeOrderService : IProcessStockTradeOrders
{
    public void Submit(OrderMessage order)
    {
        //….etc
    }

    public void Cancel(OrderMessage order)
    {
        //….etc
    }
}

I cant say it feels right to me yet but gradually I do feel as though the names of this service and the intent expressed by it’s message handlers are becoming somewhat more reflective of the business problem it attempting to solve, making the code easier to reason about.

Lets have a look at how I might go about specifying the contract and implementation for such a service using NServiceBus.

Listing 4.0

public class SubmitOrderMessageHandler : IMessageHandler<SubmitOrderMessage>
{
    public IBus Bus { get; set; }

    public void Handle(SubmitOrderMessage message)
    {
        OrderSubmittedResponseMessage response =
            new OrderSubmittedResponseMessage ();

        //more processing to act on the order message

        this.Bus.Reply(response);
    }
}

What about cancelling the order then?

Listing 5.0

public class CancelOrderMessageHandler : IMessageHandler<CancelOrderMessage>
{
    public IBus Bus { get; set; }

    public void Handle(CancelOrderMessage)
    {
        //processing
    }
}

Structural differences aside, I still feel strange about the whole SubmitOrder aspect to the naming as it stands, it still feels a tad too generic for my liking. To be fair, an order service might not be something that would be modelled with singular discrete message handlers like this, perhaps it would better be modelled as a Saga (think persisted workflow), just as it is in the NServiceBus samples, but that’s not the focus here so we wont dwell on that either.

The SubmitOrderMessageHandler demonstrated above, handles messages of the type SubmitOrderMessage, where the IMessageHandler provides us (via the strategy pattern) a Handle<T> method, where T is the message type and the Handle methods body has the express purpose of implementing the logic for handling the message.

image By now you can probably see where this all going? The “data” contained in the SubmitOrderMessage is certainly different to the data contained in the CancelOrderMessage and if we shift our focus now back to the WCF stock trader example to consider the OrderDataModel (the argument to the SubmitOrder method) then its evident that the message carries some data used by the generic handler to examine more closely exactly what the intent is (beyond the ‘submission’ of the order). Again this is not plainly clear from the name of the message alone. The OrderDataModel data contract has a property (of string) called ‘orderType’ and looking at the component that has the responsibility of processing orders, we can see that it checks the value of the ‘orderType’ to discern whether or not to process the order to BUY or SELL.

Listing 6.0

if (order.orderType == StockTraderUtility.ORDER_TYPE_BUY)
{
    //…do some work for a buy order
}
else if (order.orderType == StockTraderUtility.ORDER_TYPE_SELL)
{
    //…do some work for a sell order
}
So up to this point we still don’t really have a good naming approach tied down, it’s clear that the intent to ‘BUY’ or ‘SELL’ is not represented in the name of the message at all. However what if the contents of a ‘BUY’ and ‘SELL’ message are semantically equivalent? Is that kind of generality enough to encourage us to become more lax and collapse these very different types of business events into one single message? Personally I don’t believe so. Let’s put on our versioning hat for just a minute and try and imagine what will start to happen when requirements for the system reveal cases that  require new fields (data). For example, a ‘BUY’ order message requires a new field and suddenly we have lost our semantic equivalence and our message starts to exhibit some of the frailty it posses by attempting to be so generic. It’s therefore important to put a solid abstraction on the message contract so that with variation over time the system doesn’t suffer under the weight of brittle message contract design.
image This small snippet of decisional code in listing 6.0 underlines how the naming of the Service Contract and Data Contracts in the example do not really make a good job in delivering code that is self describing from a business context and is therefore harder to read and reason about it. In the NServiceBus example however it’s plainly evident what to expect from each message and its handler. I should point out that it is possible to borrow the style encouraged in NServiceBus and implement it in WCF (to some degree); once such approach is using un-typed messages where the body of the message contains a serialised message <T> and Services receiving messages of type <T> can have registered message handlers that can be matched to deserialized messages of <T>, finally having their Handled method called. I covered this somewhat in another post.

Generic in the name of Reuse?

imageIn an article by Thomas Erl, he speaks of more ‘generic’ naming schemes when it comes to ‘utility services’ but I don’t have any genericbusiness’ operations because the domain in which I work is not typically generic. Therefore when I put on my AOP hat and start thinking about some of the cross cutting concerns that crop up in any number of business domains, such as the ubiquitous logging for example, shouldn’t I reasonably be expected to relax my naming sensibility? But that’s not really the problem under discussion here is it.

Lets take the code now from listing’s 4.0 and 5.0 and propose a new approach to the model suggested by the Stock Trader for the submission of buying and selling trades:

public class SubmitBuyOrderMessageHandler : IMessageHandler<BuyOrderMessage>
{
    public IBus Bus { get; set; }

    public void Handle(BuyOrderMessage)
    {
        BuyOrderResponseMessage response =
            new BuyOrderResponseMessage ();

        //more processing to act on the order message

        this.Bus.Reply(response);
    }
}

public class SubmitSellOrderMessageHandler : IMessageHandler<SellOrderMessage>
{
    public IBus Bus { get; set; }

    public void Handle(SellOrderMessage)
    {
        SellOrderResponseMessage response =
            new SellOrderResponseMessage();

        //more processing to act on the order message

        this.Bus.Reply(response);
    }
}

Messages and their names should map closely to business events modelled by the system; business events as such should have their names represented in non generic terms with more focus on their explicit context. I want to build self evident, self describing business systems where things like implementation details should certainly not figure in the naming of messages or their handlers, that’s something for ‘other’ documentation.

Share/Save/Bookmark

1 Comment so far

  1. [...] Segal, a fellow .NET and SOA junkie posted back in November about how he names operations and messages within WCF and the nServiceBus framework (an open-source [...]

Leave a reply

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia