Event sourced unique validation technique

On our project a number of aggregates have the requirement that they
have a unique name across all instances of the aggregates of a certain
type. To achieve this we created a UniqueNameSet that has the method

boolean doesNameExist(Class aggregateType, String aggregateName)

Other implementations I have seen internally we store the names for an
aggregate in a database table. Our implementation is Event Sourced. We
implement the EventVisitor interface so that when the set loads i.e.
the bean is initialised we visit each event and build up the set of
names for each aggregate type.

What do you think about this approach?

One issue at the moment I see is that the EventVisitor doesnt take
into account the Snapshot mechanism so all domain events need to be
visited. Another point here is that it removes the need to create new
domain querying services and tables and the supporting software
artifacts e.g. tests sql scripts. There are less representation of
same data in domain and there is a performance benefit keeping these
things in memory and not hitting database.

Is the UniqueNameSet an aggregate?

The idea is that you should -whatever solution you choose- have a single location where all unique attributes are located. That location should also be able to cope with concurrent access.

What value does event sourcing add here?

It shouldn’t be too hard to write a test to prove this. If it works, and you find it easy to maintain, then it’s a good solution :wink: