Entities or value objects ?

182 views
Skip to first unread message

hector.T

unread,
May 3, 2013, 10:53:41 PM5/3/13
to ddd...@googlegroups.com
Hi, 

I am having problem to differentiate between entities and value objects.
This question is highly related to the question of sudarshan https://groups.google.com/forum/?fromgroups=#!topic/dddcqrs/OAE4LC-OSAU but I would like to give my own example on this particular problem.

I would like to represent a Share from the Stock market, the Share will be the Entity and will contain an object Pricing which contains 3 properties; the current price, the ask price and the bid price.

From my understanding, this Pricing object should be a value object because only his properties define it. If I have a Pricing of 1/1/1 and another one 1/1/1, for me there are the same.
But where I am confused is that those value can change. So this Pricing object wouldn't be immutable as the current price can change at any time without the ask price changing or the bid price changing.

Am I missing something?

Thank you very much,
Hector

Bryan Watts

unread,
May 4, 2013, 12:01:53 AM5/4/13
to ddd...@googlegroups.com
Hi Hector,

In this case, the Pricing object would change all at once. If the ask price became 5, you would replace the entire 1/1/1 instance with a 1/5/1 instance. You might even add some helpers such as Pricing.WithAsk() and Pricing.WithBid() to make it easier to write.

This is similar to how .NET's DateTime is a value object that allows you to create new instances by changing any of the individual values, such as with .AddMinutes().

- Bryan

hector.T

unread,
May 4, 2013, 2:40:21 AM5/4/13
to ddd...@googlegroups.com
Hi Bryan,

Thanks for the answer. That was exactly what I was looking for! 
But now It brings me to another question; what if my Share contains a collection of Pricing which are use as history and each pricing will contains the same properties plus a date.
And I should be able to change any property from any Pricing in the collection by navigating from the Share.

How would you do, will each pricing be a value object or an entity?

Thanks!

Bryan Watts

unread,
May 4, 2013, 7:14:51 PM5/4/13
to ddd...@googlegroups.com
It is odd that you would allow updates to something considered historical, so I would suggest rethinking that approach. To address your question, though, the Pricing object would be an entity because it refers to a particular date, and you have the need to refer to prices on arbitrary dates. The ability to refer to the same object at multiple points in time defines an entity, so that fits this case (see my answer to the question you referred to originally for more information).

hector.T

unread,
May 8, 2013, 10:15:20 AM5/8/13
to ddd...@googlegroups.com
I would like to add another question on entities/value object/aggregate; when must I decide to model an aggregate or just a value object?

If I take "Effective Aggregate Design by Vaughn Vernon", the example is on a Scrum system where the model is composed by Product, BacklogItem, Sprint and Release.
If I would have a business rule which state "A product will be created by a team member."

Would you represent a TeamMember as an aggregate in the current BC and this aggregate will have a factory method CreateProduct() which returns a new Product?
Or would you rather have a Product constructor which just take a TeamMemberId knowing that all TeamMembers are managed in another BC and the current BC will only hold a value object TeamMemberId reference?

@yreynhout

unread,
May 9, 2013, 6:17:03 AM5/9/13
to ddd...@googlegroups.com
An aggregate is a consistency boundary for a cluster of objects that - together - are involved in the lifecycle of the aggregate. You have to differentiate between referring to a piece of information at a certain point in time in that life-cycle vs protecting invariants surrounding that piece of information. The first would usually end up as a value object or primitive type in the aggregate that happens to track this as part of a behavior, while the latter would become either an entity part of an aggregate or an aggregate itself. Keeping pounding the whys ... modeling is not a one-shot thing, it requires lots of iteration, being wrong, getting insight, towards a more useful model (no such thing as perfect).

Tom Janssens

unread,
May 11, 2013, 2:49:12 AM5/11/13
to ddd...@googlegroups.com
Just a gentle reminder: does a share have any behavior? I would assume portfolios or stock exchanges have behavior, but I assume a share does not? Could you give me an example? 

Op zaterdag 4 mei 2013 04:53:41 UTC+2 schreef hector.T het volgende:

hector.T

unread,
May 13, 2013, 2:35:42 PM5/13/13
to ddd...@googlegroups.com
Hi,

Thanks for the replies!

Since I read your question, I changed the "share" to a "stock" which is represented by its ticker.
But in my example, the only behavior is that the pricing of the stock will change for each new price received from the market. 

Tom Janssens

unread,
May 14, 2013, 1:04:17 AM5/14/13
to ddd...@googlegroups.com
Hey Hector,

Based on your remark, I would say that this is an external interface/service that injects a StockPriceEmitted event? No need to have any AR there IMO.

Op maandag 13 mei 2013 20:35:42 UTC+2 schreef hector.T het volgende:
Reply all
Reply to author
Forward
0 new messages