开发者

DDD: Model local identity inside agregate root

after reading the blue book (Eric Evan's Domain Driven Design) and starting applying the DDD concepts in a simple blog like application I have the following question, how do you model a local identity of an entity inside an aggregate root??

Let's say for the sake of simplicity, I have a simple blog model that has the following entities and scenarios: A registered user can publish a post, a post can have one or more tags associated and a registered or unregistered user can publish a comment on a post.

In this scenario, the entities are User, Post, and Comment, the aggregate roots are User na Post being comment an aggre开发者_StackOverflow社区gate inside Post aggregate root.

So since the comment entity has a local identity inside Post how do I model its local identity?? I.E. I cannot differenciate a comment just by it's attributes since I can have twho different comments for the same post published by the same user with the same content...

I first thought of the order of the comment inside the post's comment list to identify the comment inside the post, but this becomes very complex in concurrent environments, like a web application where two clients can post comments on blogs and I would have collisions until I store them in the database. Besides that I need to keep the logic for restoring the comment list from repository in the same order the post was saved to the repository...

Then having a unique identifier counter inside the Post and autoincrement it with every comment published, but in concurrent environment becomes complex, so how do I model the local identity inside an aggregate root??

Thanks Pablo


Good question. From Eric Evan's book:

ENTITIES inside the boundary have local identity, unique only within the AGGREGATE.

...only AGGREGATE roots can be obtained directly with database queries. All other objects must be found by traversal of associations.

I think that the second part is what is important. You should be treating Comment Id as local identity. In other words you should not retrieve Comments bypassing its Aggregate Root (Post), reference Comments from the outside etc. Technically, Commend Id can be the same AUTOINCREMENT field generated by database, like you would have for User and Post (or any other id generator from hibernate). But conceptually Comment will have local identity. It is the job of the Aggregate Root to resolve it.


I'm no DDD expert, but I would argue that perhaps Comment should be a value object. A value object has no "conceptual identity," but it can certainly have a persistence identity. Usually, you should prefer using value objects over entities because the latter typically has more overhead.

Don't confuse persistence identity with conceptual identity. I had trouble understanding that in the beginning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜