I was recently reading this article here: https://cuttingedge.it/blogs/steven/pivot/entry.php?id=100. It appears to talk about using commands (http://www.dofactory.com/net/command-design-pattern) instead of application services.
Please see the code below:
public sealed class ShipmentController
{
private readonly ICommandDispatcher dispatcher;
public void ShipOrder(ShipOrder cmd) => dispatcher.Dispatch(cmd);
}
sealed class CommandDispatcher : ICommandDispatcher
{
private readonly Container container;
public void Dispatch(dynamic cmd) => GetHandler(cmd.GetType()).Handle(cmd);
private dynamic GetHandler(Type type) =>
container.GetInstance(typeof(ICommandHandler<>).MakeGenericType(type));
}
which replaces code like this: http://www.zankavtaskin.com/2013/11/applied-domain-driven-design-ddd-part-6.html
I have three questions:
1) Is this saying that you should have one command per command request in the Application Service Layer? Wouldn't this result in class explosion e.g. if you have 100 commands?
2) What do you do with CQRS queries? Do you create regular application services for these?
3) What do you do with scenarios where you extract from the database (say an order); perform a command on the Order e.g. CalculateTax and then persist to the database? I assume the flow would be (is this right):
MVC
Application Service (to extract order from database)
Command (Application Service calls CalculateTaxCommand)
command request
are two completely different things. And yes, forcommand request
you would have at least 3 classes:command
,handler
andresponse
. 2) No. You have the same principle ascommand
s, except queries are idempotent (they can't modify anything). This means you might need to encapsulate your db access so that nobody can write a query that modifies something 3) You would: a) send a query b) execute a command tocalculatetax
c) execute a command to update the db – zaitsman