When and how to assign a unique identifier for an object in DDD?

A better example would be an object Userthat needs to be saved. I have the following candidates for assigning a unique identifier to a user:

  • Assign keys provided by the back-end (NDB, MySQL, etc.).
  • Manual work with a unique identifier using some service (for example, a system clock).
  • Properties such as emailId.

Taking a simple example of a detailed view, we often have a detailed view of a user of the type some/path/users/{user_id}, if we store emailId as a unique identifier, then there is a chance that the user will one day be able to change his email identifier and it will break.

Which approach is better for identifying the same object?

+4
source share
2 answers

Named UUID.

UUID, because it gives the identifier a good predictable structure without introducing semantic consequences (for example, an example of your email identifier). Think of a surrogate key .

Named UUID because you want the generated identifier to be deterministic. Deterministic tools play: you can move your system to a test environment and run replay commands to check the results.

- , - (: POST - ). , (, ) - . "" , persistence , .

, idempotent, .

UUID ; , uuid ( , ).

(, ) uuid, , - . , vivek@stackoverflow.com ? .

, - - .

, . , . , , .

+3

@VoiceOfUnreason, . , UUID . UUID .

, . UUID - , . , Twitter Snowflake (), .

, , .

, , . . , , .

0

Source: https://habr.com/ru/post/1629492/


All Articles