当前位置: 动力学知识库 > 问答 > 编程问答 >

Domain Driven Design - Value Object or Entity

问题描述:

I got a question about identifying a value object in the case below and how I must implement this.

The case:

In a online community users can make there own private/public page(s) (something like Facebook for example). Within this page they can make posts and such. These posts can be rated by other users. Not only the posts can be rated but also the entire page can be rated by other users.

So if I try to model this I end up with 3 entities (Page, User, Post) who all have a unique identity in within this content.

But what about the ratings? I'm leaning towards a Value Object because a rating does not have enough identity within this content (a post or page can exist without it) and it can't exists without a user.

The question: is it a Value Object or is it a Entity :)

Thanks!

网友答案:

It seems to me that your rating system is highly concurrent, as you expect ratings on a single AR from many concurrent users. This will definitely cause big problems with stale ARs if two concurrent ratings come in. You are way better of making your Ratings their own AR to allow for concurrent creation of ratings of a single Post/Page, see e.g., Vaughn Vernon's IDDD book where he talks about concurrent users.

网友答案:

Given your description of the problem, Rating is a value object.

Value objects indeed are something that can be used by domain expert and users to describe qualities of an entity but not identified.

The Rating class could hold a reference to the user entities, via shared identifiers, and the Post class could have a command like void Register(Rating rating).

网友答案:

when you trying to make an object as value object don't consider the identity of it only. value object should be immutable and not too much in an application, for example a brand. all of the brands in the world are not more than users of or pages of a system. or money and address in some cases as not every object has identity, value object is for reducing the complexity not making the existing problem more complex by sitting in two way about choosing an an object as entity or value object! in your case: page , user and post could be entity of an aggregate or maybe aggregate root(based on your application) but rates of the system could be entity or value object. if it has not identity for you to retrieve it via an aggregate root , you assign it as a value object but in other case it could be an entity of an aggregate. take a look at this short blog post about DDD: http://www.ehsanghanbari.com/Post/15/domain-driven-design-alphabet

分享给朋友:
您可能感兴趣的文章:
随机阅读: