Just spotted this in a project I’m working on:

public static string GetExceptionAsString(this Exception exception)
{
	return string.Format("Exception message: {0}. " + Environment.NewLine +
 		"StackTrace: {1}. {2}", exception.Message, exception.StackTrace,
		GetAnyInnerExceptionsAsString(exception));
}

Writing code like this should be a shooting offense.

But wait, there’s more! Check out its usage:

try
{
	...
}
catch (Exception ex)
{
	log.InfoFormat("Error occured:{0}.", ex.GetExceptionAsString());
}

Agh!

Code like this demonstrates a complete misunderstanding of CLR exception basics — inner exceptions and formatting — and also log4net. All that hand-rolled formatting could simply be replaced with:

  • GetExceptionAsString() -> Exception.ToString()
  • ILog.InfoFormat(format, args) -> ILog.Info(message, exception)
June 25th, 2010 | 6 Comments

Prism’s event aggregator is great for decoupling UI state changes when UI events occur, but sometimes you need to perform some larger, long-running task on a background thread — uploading a file, for example.

Here’s a quick example of an encapsulated event handler listening off the Prism event bus, and using Windsor’s IStartable facility to handle event subscription:

public class TradeCancelledEventHandler : ICompositePresentationEventHandler, IStartable
{
    private readonly IEventAggregator eventAggregator;

    protected TradeCancelledEventHandler(IEventAggregator eventAggregator)
    {
        if (eventAggregator == null)
            throw new ArgumentNullException("eventAggregator");

        this.eventAggregator = eventAggregator;
    }

    public void Start()
    {
        // Register to receive events on the background thread
        eventAggregator
            .GetEvent<TradeCancelledEvent>()
            .Subscribe(Handle, ThreadOption.BackgroundThread);
    }

    public void Stop()
    {
        eventAggregator
            .GetEvent<TradeCancelledEvent>()
            .Unsubscribe(Handle);
    }

    void Handle(TradeCancelledEventArgs eventArgs)
    {
        // ... do stuff with the event
    }
}

Each event handler is effectively a little service running in the container. Note ICompositePresentationEventHandler is a simple role interface that allows us to register them all at once in the IoC container:

public interface ICompositePresentationEventHandler {}

...

container.AddFacility<StartableFacility>();

// Register event handlers in container
container.Register(
    AllTypes
        .Of<ICompositePresentationEventHandler>()
        .FromAssembly(Assembly.GetExecutingAssembly()));
June 24th, 2010 | No Comments Yet

In the first two posts I talked about commands and command handlers. Now we need to wire them up to invoke them from your service endpoint.

Command Dispatcher

When a command arrives, you simply look up the corresponding handler from your IoC container and invoke it. This responsibility is delegated to a command dispatcher object:

public interface ICommandDispatcher
{
    void Dispatch<T>(T command) where T : ICommand;
}

public class CommandDispatcher : ICommandDispatcher
{
    private readonly IWindsorContainer container;

    public CommandDispatcher(IWindsorContainer container)
    {
        if (container == null) throw new ArgumentNullException("container");
        this.container = container;
    }

    public void Dispatch<T>(T command) where T : ICommand
    {
        if (command == null) throw new ArgumentNullException("command");

        var handler = container.Resolve<ICommandHandler<T>>();
        ErrorIfNoHandlerForCommandFound(handler);

        handler.Handle(command);
    }

    private static void ErrorIfNoHandlerForCommandFound<T>(
        ICommandHandler<T> handler) where T : ICommand
    {
        if (handler == null)
            throw new NoHandlerForCommandException(typeof(T));
    }
}

Then we simply inject the command dispatcher into the WCF service and invoke it whenever a command is received:

[ServiceBehavior]
public class BookingService : IBookingService
{
    private readonly ICommandDispatcher commands;

    [OperationContract]
    public void BookTable(BookTableCommand command)
    {
        if (command == null) throw new ArgumentNullException("command");
        commands.Dispatch(command);
    }
}

Many of you will note that this is very similar to Udi Dahan’s Domain Events aggregator — the only major difference is CQRS commands are only ever handled by one handler, where domain events are broadcast to anyone who’s listening.

Scaling out

Note this is a synchronous command dispatcher — commands are handled as soon as they arrive. An asynchronous/high-volume system may simply put them in a queue to be executed later by some other component.

Final thoughts

This really is a very introductory series to refactoring an existing application to move towards CQRS. We haven’t even touched on the main goal of CQRS yet — all we’ve done is put clear command/query contracts between our client and server.

It may not sound like much, but doing so allows us to mask non-CQRS components in our system — an anti-corruption layer of sorts — and allows us to proceed refactoring them internally to use different models and storage for commands and queries.

June 17th, 2010 | 10 Comments

In the previous two posts, I showed some simple patterns for commands and command handlers. Now let’s talk about the other half of the story: queries!

On our WCF service, each query method:

  • Returns one or more QueryResult objects — a DTO created exclusively for this query.
  • Takes a QueryParameter argument — if required, a DTO containing search criteria, paging options etc.

For example, to query for bookings:

[ServiceContract]
public interface IBookingService
{
    [OperationContract]
    IEnumerable<BookingQueryResult> SearchBookings(
        BookingSearchParameters parameters);
}

Query Parameters

Queries take simple DTO parameter objects just like commands. They carry both search criteria (what to look for) and things like paging options (how to return results). They can also define defaults. For example:

[DataContract]
public class BookingSearchParameters
{
    public BookingSearchParameters()
    {
        // default values
        NumberOfResultsPerPage = 10;
        Page = 1;
    }

    [DataMember]
    public Tuple<DateTime, DateTime> Period { get; set; }

    [DataMember]
    public int NumberOfResultsPerPage { get; set; }

    [DataMember]
    public int PageNumber { get; set; }
}

Query Object

Queries are then executed by a query object — an application service that queries your ORM, reporting store, or domain + automapper (if you’re still using a single model internally for commands and queries).

public interface IQuery<TParameters, TResult>
{
    public TResult Execute(TParameters parameters);
}

Query Results

Queries can return a single result (e.g. to look up the details of a specify item), or a sequence (searching):

public class BookingSearchQuery :
    IQuery<BookingSearchParameters, IEnumerable<BookingSearchResult>>
{
    public IEnumerable<BookingSearchResult> Execute(
        BookingSearchParameters parameters)
    {
        ...
    }
}

Query results are simple DTOs that provide all the information the client needs.

[DataContract]
public class BookingSearchResult
{
    [DataMember]
    public string PartyName { get; set; }

    [DataMember]
    public int PartySize { get; set; }

    [DataMember]
    public DateTime TimeAndDay { get; set; }

    [DataMember]
    public string SpecialRequests { get; set; }
}

Query Results should be able to be rendered directly on the UI, in one query. If the they require further mapping, or multiple calls (e.g. to get different aspects of an object) before you can use it on a view model, then they are most likely:

  • Too granular — query results should be big flattened/denormalized objects which contain everything you need in one hit.
  • Based on the wrong model (the domain or persistence model) — they should be based on the UI’s needs, and present a screenful of information per call.

As with commands, having a one-to-one mapping between query objects and queries makes it easy to add/remove functionality to a system.

Can’t I use the same object for sending commands and returning results?

That BookingSearchResult looks more or less identical to BookTableCommand we sent before — it has all the same properties. That doesn’t seem very DRY! Can’t I just create a generic DTO and use that in both cases?

Using the same class for commands and queries is called CRUD, and it leads to exactly the sort of situations we are trying to avoid — where commands needs to use different representations of objects than when querying, but can’t because they are tightly coupled to share the same object. As I said in part 1, commands are driven by business transactions, but queries are driven by UX needs, and often include projections, flattening and aggregation — more than just 1:1 mapping.

Next: Part 4 – Command Dispatcher

June 16th, 2010 | 7 Comments

In my previous post, I described command DTOs and service methods for booking a table at a restaurant. Now, we just need something to interpret this command, and do something useful with it.

To do this, we create a corresponding command handler for each command:

public interface ICommandHandler : IHandler<T> where T : ICommand
{
    void Handle(T command);
}

Command handlers are responsible for:

  • Performing any required validation on the command.
  • Invoking the domain — coordinating objects in the domain, and invoking the appropriate behaviour on them.

Command handlers are application services, and each execution represents a separate unit of work (e.g. a database transaction). There is only one command handler per command, because commands can only be handled once — they are not broadcast out to all interested listeners like event handlers.

Here’s an example for handling our BookTableCommand. A one-to-one handler/command mapping makes it easy to add/remove features from our service.

public class BookTableCommandHandler : ICommandHandler<BookTableCommand>
{
    IDinnerServiceRepository nights;

    public void Handle(BookTableCommand command)
    {
        var dinnerService = nights[command.TimeAndDay];
        var party = new Party(command.PartySize, command.PartyName);
        night.TakeBooking(party);
    }
}

Note each command implements ICommand — a simple explicit role marker interface that also allows us to use constraints on generic types and automate IoC registration of command handlers.

public interface ICommand { }

Command validation and errors

Aside from transient technical faults, there are two business reasons a command might fail:

  • The command was not valid — e.g. you tried to book a table for zero people.
  • The command could not succeed — e.g. the restaurant is fully booked that night.

Ideally, the client will have pre-checked these to save time, but if the command handler detects a problem, how do we report it back to the user, given commands are not allowed to have return values? How would we report success even?

Actually, this is not a problem at all — commands have no return value, but they can throw a detailed validation/command failed exception back to the client. If they didn’t throw anything, it is assumed to have succeeded.

What if you execute commands asynchronously — e.g. queued and executed at some later time? We can’t throw an exception back to the client in this case. But that’s fine — the client must always assume their asynchronous command will succeed. If it does fail, it will be reported back through some alternate channel (e.g. via e-mail or the query side). This is why it is important to pre-validate commands on the client as much as possible.

Next: Part 3 – Queries, Parameters and Results

June 16th, 2010 | 7 Comments