Command and query at the same time

158 views
Skip to first unread message

David

unread,
Nov 19, 2016, 10:54:52 AM11/19/16
to DDD/CQRS
I've a bounded context which offers weekly downloads. It's downstream to another bounded context which deals with media (file uploads/downloads).

When a user wants to download a file I must first check if the download is still available (using its expiry date), if it's available I must send a POST request (media.site.com/files/{file-id}/dynamic) to the media bounded context to create a dynamic download link for the file which expires in 30 minutes.
The POST request will return the URI where the file can be downloaded from.

I'm creating a dynamic download link for the file so it's a command. A command does not return anything so how am I supposed to get the URI of the new dynamic file? The URI is something like meda.site.com/files/dynamic/{random-string} where the generation of the random string is the media bounded contexts responsibility.

I was thinking of treating this as a query instead of a command so I get a return value but that seems wrong because it's causing state to change somewhere.

Any thoughts?

Alexander Langer

unread,
Nov 19, 2016, 12:36:22 PM11/19/16
to ddd...@googlegroups.com

> I'm creating a dynamic download link for the file so it's a command. A
> command does not return anything so how am I supposed to get the URI of
> the new dynamic file? The URI is something like

Generate the random string in the POST handler and include the result in
both, the command and the HTTP return value.

David

unread,
Nov 19, 2016, 12:45:12 PM11/19/16
to DDD/CQRS
You mean have a "result" field in my command which the handler can set a value in when it's complete?
Message has been deleted

Mauricio Burgos Herrera

unread,
Nov 20, 2016, 1:43:41 AM11/20/16
to DDD/CQRS
Media HTTP endpoint handler will generate the string. This string would be put inside the command, and the HTTP handler can return this string in the HTTP response

Peter Hageus

unread,
Nov 20, 2016, 3:17:06 AM11/20/16
to ddd...@googlegroups.com
This is a good use for HTTP Location header btw. 

/Peter
--
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.

David

unread,
Nov 20, 2016, 8:57:29 AM11/20/16
to DDD/CQRS
I think you are misunderstanding.

  1. Bounded context A executes command.
  2. Some checks are done to ensure the file is allowed to be downloaded.
  3. If all is good it then sends the request to the media bounded context to generate the dynamic download link.
  4. Since I originally executed a command and commands are not supposed to return anything I can't get the generated link back to where I dispatched the first command.

Greg Young

unread,
Nov 20, 2016, 9:27:02 AM11/20/16
to ddd...@googlegroups.com
http location header?
> --
> 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.



--
Studying for the Turing test

Mauricio Burgos Herrera

unread,
Nov 20, 2016, 12:15:45 PM11/20/16
to ddd...@googlegroups.com, Greg Young
The receiver of the request in the media bounded context would generate the random string and use it as an argument when creating the command to generate the dynamic link. After the command is dispatched and successful, the request handler will return HTTP response with the link created.

As for the location header comment that would be to use the location response header to tell the caller where the resource is https://en.wikipedia.org/wiki/HTTP_location

-- 
Mauricio Burgos Herrera
You received this message because you are subscribed to a topic in the Google Groups "DDD/CQRS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dddcqrs/wbvDqV5mRPQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dddcqrs+u...@googlegroups.com.

Alexander Langer

unread,
Nov 20, 2016, 12:15:57 PM11/20/16
to ddd...@googlegroups.com
Then change 3. such that it generates the dynamic download link in a
predictable way (e.g., just take the {random-string} generated in the
REST layer and included in the command).

If file is not allowed to be downloaded, let
meda.site.com/files/dynamic/{random-string} return 403.

You might also want to consider HTTP long polling for enhanced UX.


On 20/11/2016 14:57, David wrote:
> I think you are misunderstanding.
>
> 1. Bounded context A executes command.
> 2. Some checks are done to ensure the file is allowed to be downloaded.
> 3. If all is good it then sends the request to the media bounded
> context to generate the dynamic download link.
> 4. Since I originally executed a command and commands are not supposed
> to return anything I can't get the generated link back to where I
> dispatched the first command.
>
>
> On Sunday, 20 November 2016 01:43:41 UTC-5, Mauricio Burgos Herrera wrote:
>
> Media HTTP endpoint handler will generate the string. This string
> would be put inside the command, and the HTTP handler can return
> this string in the HTTP response
>
> On Saturday, November 19, 2016 at 6:45:12 PM UTC+1, David wrote:
>
> You mean have a "result" field in my command which the handler
> can set a value in when it's complete?
>
> On Saturday, 19 November 2016 12:36:22 UTC-5, Alexander Langer
> wrote:
>
>
> > I'm creating a dynamic download link for the file so it's a command. A
> > command does not return anything so how am I supposed to get the URI of
> > the new dynamic file? The URI is something like
>
> Generate the random string in the POST handler and include
> the result in
> both, the command and the HTTP return value.
>
> --
> 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
> <mailto:dddcqrs+u...@googlegroups.com>.

Mauricio Burgos Herrera

unread,
Nov 20, 2016, 12:16:07 PM11/20/16
to ddd...@googlegroups.com, dypsok
Yes that is exactly what I meant. But yes as Peter said, you probably want to use the location header

-- 
Mauricio Burgos Herrera

On 20 November 2016 at 07:43:41, dypsok (dyp...@gmail.com) wrote:

Why not post to a facade service that execute the command and return a http response containing the uri with random string ?
I thinkthat's the same idea as David,  ie in pseudo code :
At client level make a Get to  http://GiveMeUrlForMedia/{file-id}
At HttpHandler level do :
  var RandomString = generateNewRandomString()
   var IsExecuted =  CommandHandler.Handle( new GenerateTempLinkCommand(RandomString)  )
  if (IsExecuted) 
  {
    return "http://dynamicLink/" + RandomString
--
You received this message because you are subscribed to a topic in the Google Groups "DDD/CQRS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dddcqrs/wbvDqV5mRPQ/unsubscribe.

To unsubscribe from this group and all its topics, send an email to dddcqrs+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages