Pragmatic CQRS: Verifying username uniqueness when registering a new user

by Tom 15. November 2011 03:19

Introduction

Most people who get started with CQRS have issues with this classical example: "how do we verify username uniqueness when a new user registers ?".

While user authentication is something that has been implemented numerous amounts of times before, and one should usually not reinvent the wheel but use an existing software library for this particular case, the problem in itself is rather interesting, and happens quite a lot in a domain.

In this small article I will show you a simple proper way to resolve the issue respecting CQRS/DDD principles.


Is that really what we want ?

Let us think about another way to solve the issue. I will rewrite the scenario a little bit:


Great, now we have a pending registration ! How is that helpful ?

Not so quick! I have a rule of thumb: whenever I have to communicate between different AR's (even the ones of the same AR type), I use a saga. What is uniqueness validation when you think about it ? Exactly !! A question to all the other AR's whether an entity with the same properties exists. So let us have a second scenario for the saga:

Please note that scenario's for sagas are a little different:


Example code


Conclusion

There you have it, a simple and elegant solution to a problem everybody struggles with in the beginning.

Bookmark and Share

About Tom

Tom Janssens op LinkedIn

Tom Janssens op twitter

Core bvba RSS

 

Tom Janssens is an independent freelance ICT consultant that has been "into computers" ever since the age of 7.

Typing source code from a book evolved into exploring the limits of coding in procedural, assembly and object-oriented languages.
As he matured in software coding, he started focussing on the problems surrounding software development, and learned that software development is usually about people and interactions first, and about technology second.

Due to his diverse track record he gained insights in a lot of aspects of the software development process. Currently his main focus is on strategic ICT advice, lean product/project development and improving the software development process and architecture.

He avoids ivory-tower-approaches by applying and verifying the applicability of the latest tech buzz in software experiments.

He is also the founder of the following LinkedIn groups:

CQRS Professional
BDD Professional
Asp.Net MVC professional

More info about Tom and his company...


Advertisement

Forget all your SCRUM -, Kanban - and other Agile and Lean certificates

Here is the only true AGILE and LEAN certificate you will ever need:

The Creative Recursive Analysis Process Certificate
(CRAP Certificate for short)

More info can be found at the official CRAP certificate website:
http://bit.ly/CRAPCertificate