CQRS Event Sourcing: Validate UserName uniqueness

前端 未结 8 1244
醉话见心
醉话见心 2021-01-29 18:08

Let\'s take a simple \"Account Registration\" example, here is the flow:

  • User visit website
  • Click \"Register\" button and fill form, click \"Save\" button
8条回答
  •  清歌不尽
    2021-01-29 18:38

    About uniqueness, I implemented the following:

    • A first command like "StartUserRegistration". UserAggregate would be created no matter if user is unique or not, but with a status of RegistrationRequested.

    • On "UserRegistrationStarted" an asynchronous message would be sent to a stateless service "UsernamesRegistry". would be something like "RegisterName".

    • Service would try to update (no queries, "tell don't ask") table which would include a unique constraint.

    • If successful, service would reply with another message (asynchronously), with a sort of authorization "UsernameRegistration", stating that username was successfully registered. You can include some requestId to keep track in case of concurrent competence (unlikely).

    • The issuer of the above message has now an authorization that the name was registered by itself so now can safely mark the UserRegistration aggregate as successful. Otherwise, mark as discarded.

    Wrapping up:

    • This approach involves no queries.

    • User registration would be always created with no validation.

    • Process for confirmation would involve two asynchronous messages and one db insertion. The table is not part of a read model, but of a service.

    • Finally, one asynchronous command to confirm that User is valid.

    • At this point, a denormaliser could react to a UserRegistrationConfirmed event and create a read model for the user.

提交回复
热议问题