Implementing Commands in DDD

Commands are one of the most fundamental building blocks of DDD. Commands represent the action that users try to perform on the domain model, they are the verbs in the domain model.

public class Order : IAggregateRoot
{
 public void PlaceOrder(PlaceOrder command) {}
 public void CancelOrder(CancelOrder command) {}
}

 

Since the command name is self-explanatory and sufficiently intention-revealing, we could just over-load the method with different commands.

public class Order : IAggregateRoot
{
 public void Handle(PlaceOrder command) { }
 public void Handle(CancelOrder command) { }
}

 

If you are a fan of Udi Dahan’s use of interfaces to make roles explicit in your code like me, then you could model each command as a role,

public class Order : IAggregateRoot, IHandle<PlaceOrder>, IHandle<CancelOrder>
{
 public void Handle(PlaceOrder command) { }
 public void Handle(CancelOrder command) { }
}
public interface IHandle<T>
{
 void Handle(T command);
}

One of the more popular implementation of command-handlers uses publish-subscribe patterns and it has some serious benefits. I would discuss that future post. However, the above implementation is a good elegant start to modelling commands in your DDD implementations.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s