Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Do item counts belong in domain model?

Say you are modeling a forum and you are doing your best to make use of DDD and CQRS (just the separate read model part). You have:

Category {
    int id;
    string name;
}

Post {
    int id;
    int categoryId;
    string content;
}

Every time that a new post has been created a domain event PostCreated is raised.

Now, our view wants to project count of posts for each category. My domain doesn't care about count. I think I have two options:

  1. Listen for PostCreated on the read model side and increment the count using something like CategoryQueryHandler.incrimentCount(categoryId).
  2. Listen for PostCreated on domain side and increment the count using something like CategoryRepo.incrimentCount(categoryId).

The same question goes for all the other counts like number of posts by user, number of comments in a post, etc. If I don't use these counts anywhere except my views should I just have my query handlers take care of persisting them?

And finally if one of my domain services will ever want to have a count of posts in category do I have to implement the count property onto the category domain model or can that service simply use read model query to get that count or alternatively a repository query such as CategoryRepo.getPostCount(categoryId).

like image 854
Denis Pshenov Avatar asked Feb 08 '23 15:02

Denis Pshenov


1 Answers

My domain doesn't care about count.

This is equivalent to saying that you don't have any invariant that requires or manages the count. Which means that there isn't an aggregate where count makes sense, so the count shouldn't be in your domain model.

Implement it as a count of PostCreated events, as you suggest, or by running a query against the Post store, or.... whatever works for you.

If I don't use these counts anywhere except my views should I just have my query handlers take care of persisting them?

That, or anything else in the read model -- but you don't even need that much if your read model supports something like select categoryId, count(*) from posts...

domain services will ever want to have a count of posts in category

That's a pretty strange thing for a domain service to want to do. Domain services are generally stateless query support - typically they are used by an aggregate to answer some question during command processing. They don't actually enforce any business invariant themselves, they just support an aggregate in doing so.

Querying the read model for counts to be used by the write model doesn't make sense, on two levels. First, that the data in the read model is stale - any answer you get from that query can change between the moment that you complete the query and the moment when you attempt to commit the current transaction. Second, once you've determined that stale data is useful, there's no particular reason to prefer the stale data observed during the transaction to stale data prior. Which is to say, if the data is stale anyway, you might as well pass it to the aggregate as a command argument, rather than hiding it in a domain service.

OTOH, if your domain needs it -- if there is some business invariant that constraints count, or one that uses the count to constrain something else -- then that invariant needs to be captured in some aggregate that controls the count state.

Edit

Consider two transactions running concurrently. In transaction A, Aggregate id:1 running a command that requires the count of objects, but the aggregate doesn't control that count. In transaction B, Aggregate id:2 is being created, which changes the count.

Simple case, the two transactions happen by luck to occur in contiguous blocks

A: beginTransaction
A: aggregate(id:1).validate(repository.readCount())
A: repository.save(aggregate(id:1))
A: commit
// aggregate(id:1) is currently valid

B: beginTransaction
B: aggregate(id:2) = aggregate.new
B: repository.save(aggregate(id:2))
B: commit
// Is aggregate(id:1) still in a valid state?

I represent that, if aggregate(id:1) is still in a valid state, then its validity doesn't depend on the timeliness of the repository.readCount() -- using the count prior to the beginning of the transaction would have been just as good.

If aggregate(id:1) is not in a valid state, then its validity depends on data outside its own boundary, which means that the domain model is wrong.

In the more complicated case, the two transactions can be running concurrently, which means that we might see the save of aggregate(id:2) happen between the read of the count and the save of aggregate(id:1), like so

A: beginTransaction
A: aggregate(id:1).validate(repository.readCount())
// aggregate(id:1) is valid

B: beginTransaction
B: aggregate(id:2) = aggregate.new
B: repository.save(aggregate(id:2))
B: commit

A: repository.save(aggregate(id:1))
A: commit

It may be useful to consider also why having a single aggregate that controls the state fixes the problem. Let's change this example up, so that we have a single aggregate with two entities....

A: beginTransaction
A: aggregate(version:0).entity(id:1).validate(aggregate(version:0).readCount())
// entity(id:1) is valid

B: beginTransaction
B: entity(id:2) = entity.new
B: aggregate(version:0).add(entity(id:2))
B: repository.save(aggregate(version:0))
B: commit

A: repository.save(aggregate(version:0))
A: commit
// throws VersionConflictException

Edit

The notion that the commit (or the save, if you prefer) can throw is an important one. It highlights that the model is a separate entity from the system of record. In the easy cases, the model prevents invalid writes and the system of record prevents conflicting writes.

The pragmatic answer may be to allow this distinction to blur. Trying to apply a constraint to the count is an example of Set Validation. The domain model is going to have trouble with that unless a representation of the set lies within an aggregate boundary. But relational databases tend to be good at sets - if your system of record happens to be a relational store, you may be able to maintain the integrity of the set by using database constraints/triggers.

  • Greg Young on Set Validation and Eventual Consistency

How you approach any problem like this should be based on an understanding of the business impact of the particular failure. Mitigation, rather than prevention, may be more appropriate.

like image 66
VoiceOfUnreason Avatar answered Feb 19 '23 06:02

VoiceOfUnreason