DEV Community

Discussion on: A Deconstruction of CQRS

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

Great article. It was a pleasure to read it. I like the translation of the Entity to the DDD context.

One question about the Pre-generated ID adventure paragraph. You wrote:

"An ID was generated (or requested from the server) when the form was loaded, before the user even started typing anything."

So my question is why don't generate this Id on the server side after the submission of a form? You've chosen the different strategy, and that is fine for me because solved your problem :). I'm just curious if you've considered ID generation before sending a command to the external server? Or that was only a think javascript client, and it could be overengineering to do that?

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Thanks for the comment. :)

Regarding ID generation, I might not be understanding your questions entirely, but I will try to answer.

We always generate UUIDs on the server currently. I have trust issues with some browser implementations of random in JS. However, for other kinds of apps I would be comfortable with generating UUIDs on the client.

We generate the ID when the form is loaded. For web clients, we normally request data from the server for creation forms anyway -- to populate drop-down lists for example. So in the query response from the server, it will include an newly generated UUID for convenience.

The client knowing the ID before the form is submitted is valuable for a couple of reasons. Retries without duplicates were mentioned in the article. But also when the operation succeeds, the client already has the ID to be able to provide access links to the user (or query a hypermedia endpoint with the ID, if that's your thing). This frees the client from having to interpret the success of the command -- it's basically just a "yes" or "no, and here's why". Generating on the server after submission does not have these benefits.

We have done ID generation (well, fetching an ID from server) just before submitting the creation command. That was our first try at it. We didn't like it because it was a little more awkward. For example, handling a retry is slightly different from a first try. Also if there was a network congestion problem near the time of submission, it is likely to affect fetching an ID as well, so an extra error step to handle. These problems would be easily tamed if we were generating the ID locally. But at that point, the difference between generating the ID when the form loads vs just before submission is negligible (with UUIDs anyway). For me it makes more sense to do it up front so the form submission process has less decision branches.

Let me know if you have any more questions or I missed something.

Edit: I meant to add that we also use secondary IDs. These are human-friendly (and in our particular app, usually human-entered) IDs. Items can be searched by secondary IDs.

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

Thanks a lot for the explanation. You answered my question in 100%. I'll do my best to make more clear question next time :)

I've heard that in DDD there could be a dedicated service which is responsible for ID creation.

Thanks a lot for your replay.

Thread Thread
 
kspeakman profile image
Kasey Speakman • Edited

No worries. It seems I did understand your questions after all. :)

There are many strategies for ID generation, including ones where an ID server is employed. With UUIDs, there is no extra infrastructure needed. However, (although it is extremely unlikely) it is still possible to have UUID collisions. For me the trade-off is well worth making, but it depends on the situation.

Thread Thread
 
rafalpienkowski profile image
Rafal Pienkowski

Yes, you did :)