Querying during a Command

313 views
Skip to first unread message

Mike Lazell

unread,
Jun 22, 2017, 4:33:57 AM6/22/17
to DDD/CQRS
Hi there,

I am in the process of scaffolding a new project and as part of it I have a Command which creates an entity (CreateQuote). As part of that process it needs to get a value from an external service, and then another value from another external service and finally persist a combined entity in the DB and then return a response.

As things stands I have CreateQuote in the Command layer and 2 services in the Query layer. 

My concern then is that in the Command layer I need to call the query layer to get these before then persisting the entity.

Does this sound correct? Or are the 2 external service calls commands also?

Thanks in advance.

Mike

João Bragança

unread,
Jun 22, 2017, 5:31:18 AM6/22/17
to ddd...@googlegroups.com
Ultimately they are just messages. Instead of dealing with the complexity of 'layers' why not wrap up these external services in an interface or delegate? Here's an example in c#

async Task<Result> Handle(CreateQuote command, CancellationToken ct) {
    var serviceATask = _callServiceA(command.Stuff, ct); // just delegates injected into your handler's constructor
    var serviceBTask = _callServiceB(command.OtherStuff, ct);

    await Task.WhenAll(serviceATask, serviceBTask); // assuming you don't need the result of one query to go into another

    var serviceAResult = await serviceATask;
    var serviceBResult = await serviceBTask;

    var quote = Quote.Create(serviceAResult, serviceBResult, command.StillOtherStuff);

    await _quotes.Save(quote, ct);

    return new Result(quote.Identifier);
}

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/dddcqrs.
For more options, visit https://groups.google.com/d/optout.



--

Mike Lazell

unread,
Jun 22, 2017, 9:47:10 AM6/22/17
to DDD/CQRS, joao...@braganca.name
Hi João,

Many thanks for the reply.

I've created a wrapper for each of them in the query layer (the external services are for retrieving values only) and abstracted to interfaces (again in the query layer). The reason for this is they can be used standalone within the query to retrieve the values.

So, as mentioned above, for reuse I would need to use query services within the command layer.

Should I perhaps refactor the wrapper servies to a command/query agnostic layer which can be consumed in both commands and queries?


On Thursday, 22 June 2017 10:31:18 UTC+1, João Bragança wrote:
Ultimately they are just messages. Instead of dealing with the complexity of 'layers' why not wrap up these external services in an interface or delegate? Here's an example in c#

async Task<Result> Handle(CreateQuote command, CancellationToken ct) {
    var serviceATask = _callServiceA(command.Stuff, ct); // just delegates injected into your handler's constructor
    var serviceBTask = _callServiceB(command.OtherStuff, ct);

    await Task.WhenAll(serviceATask, serviceBTask); // assuming you don't need the result of one query to go into another

    var serviceAResult = await serviceATask;
    var serviceBResult = await serviceBTask;

    var quote = Quote.Create(serviceAResult, serviceBResult, command.StillOtherStuff);

    await _quotes.Save(quote, ct);

    return new Result(quote.Identifier);
}
On Thu, Jun 22, 2017 at 10:33 AM, Mike Lazell <mikel...@gmail.com> wrote:
Hi there,

I am in the process of scaffolding a new project and as part of it I have a Command which creates an entity (CreateQuote). As part of that process it needs to get a value from an external service, and then another value from another external service and finally persist a combined entity in the DB and then return a response.

As things stands I have CreateQuote in the Command layer and 2 services in the Query layer. 

My concern then is that in the Command layer I need to call the query layer to get these before then persisting the entity.

Does this sound correct? Or are the 2 external service calls commands also?

Thanks in advance.

Mike

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.

Visit this group at https://groups.google.com/group/dddcqrs.
For more options, visit https://groups.google.com/d/optout.

Ben Kloosterman

unread,
Jun 22, 2017, 7:17:59 PM6/22/17
to ddd...@googlegroups.com
In theory should be done by the creator of the command...   but this goes against the principal of making things easy for the client. 

So for web I tend to do it in the controller which then creates the real command.

Ben

Rupesh Kumar Tiwari

unread,
Jun 22, 2017, 7:30:20 PM6/22/17
to ddd...@googlegroups.com
I agree with @Ben, I would also prefer to do all query in client / controller and create command object which has necessary data and pass it to the command handler.  

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+unsubscribe@googlegroups.com.

Mike Lazell

unread,
Jun 23, 2017, 5:58:17 AM6/23/17
to DDD/CQRS
Hmm OK, that also goes against my approach of not having 'fat' controllers. I view the services as being the actual application and the API being just a client for those services.

Vincent van Dijk

unread,
Jun 24, 2017, 5:01:23 AM6/24/17
to DDD/CQRS
Calling some services in your controller doesn't mean a fat controller. It's the controller's job to combine all the services.

If you have a command handler for the CreateQuote command, you can do the query magic there. I think this is the best place to do it.

Vincent.

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.

Ben Kloosterman

unread,
Jun 25, 2017, 5:06:08 AM6/25/17
to ddd...@googlegroups.com
Note the message on the wire IS a command and the controller IS a command handler.  Creating a separate command handler is purely for architectural reasons and provides little benefit.  That said i do like and use the pattern where the controller just does the http ( mainly for testing)  , though it can be arch gold plating and quickly get you lasagna architecture.

In this case you need to add to the command . If you don't want it in the controller just call a fill command service which then calls your command handler service .

Ben

Reply all
Reply to author
Forward
0 new messages