Every distributed system eventually requires messages to be written on the wire to be transmitted from one machine to another. In many cases these messages are hidden magic. Using WCF web services, or Thrift RPC, code-generated proxies make remote calls look like function calls.
That command travels over the wire as a message made of simple scalars like strings and numbers. There is value in exposing the command itself. When you work extensively with queuing, you have no choice but to make the message explicit. Some communication frameworks, like the lovely ServiceStack, embrace this message wholeheartedly. (That last link is actually worth a read, if a bit zealous.) Now instead you have:
Voila! An explicit message object you can put in lists, send through generic pipelines (say, to add authentication or logging concerns) and generally have fun with. This is all well and good and obvious at the application service level.
Hang on. If these are nice, why not embrace this pattern for our in memory domain objects? Should our aggregate root interfaces take these commands? Should we prefer option (1) or (2) below?
I don’t think there’s an obvious correct answer here. At a project in 2012 we tried both. And we ended up preferring option (1) for the actual domain objects. Here’s why.
What happens when you go implement AddEligibility on your Person domain object?
Did you notice the difference between the command object and the interface? The interface used DateTime, and the command object just used long because it’s more safely serializable.
DateTime is a useful value object with interesting operations like asking how many days are between two times. And it knows about daylight savings time. And leap days. And all the useful things that value objects do. You know, encoding important domain behavior.
We found when programmers had access to the message object inside our domain object, they’d take cruel advantage of it. As silly as it looks to not take advantage of the DateTime in the above example, that’s exactly what would happen. Domain logic that should been safely ensconced in value objects, shared cleanly within a bounded context, was instead re-implemented all over our domain objects. Really, who divides by 86,400 in a method meant to calculate eligibility windows?
The temptation to do that rarely arises inside a raw MVC controller, or ServiceStack service handler, since you’re so clearly at the boundaries of the system, and so clearly unpacking network messages. Inside those application services you better bet people quickly just did this
And the person object’s implementation was more clearly free of translation concerns and leaked logic (in this artificial case, the logic of date manipulation):
Now the domain object can fluently use a nice value object from its domain (say, TimeSpan), and the application service can worry about message unpacking.
For some reason, this separation of concerns led to clearer code in the domain, and clearer flexibility in the application service layer. There was less temptation to treat these command objects as anemic domain objects.
So at least based on an actual programming team in the wild, my instinct is to convert these command objects to domain objects as quickly as possible in the well-known boundaries of the system. Your mileage may vary.
Tell us what you need and one of our experts will get back to you.