views:

313

answers:

1

In domain driven design, it is common knowledge that an object with an identity is an entity. For example, any person will have several forms of identity (name, etc).

But value objects are those objects with no identity. One common value object is address, however address has no identity. But at the database layer, we can have a composite key. Does this concept work in DDD? It would make address identifiable by the combination of roadname, postcode and door number (omitting information such as town and city). Money would be another value object.

The distinction seems to be in objects with no single identifiable field and value objects tend to not actually belong the entity. For example, "I" (replace with my name) may wear shoes etc but "I" am not shoes, shirt, etc (http://www.lostechies.com/blogs/joe_ocampo/archive/2007/04/23/a-discussion-on-domain-driven-design-value-objects.aspx).

Is this the right way to think about things? Also, with this mentality/approach, I can apply this to value/reference type selection in C#. I may as well go with the DDD approach though?

Thanks

A: 

I think you have the concepts of the distinction between entities and value types correct as they are understood within domain driven design (though I am far from an expert in this matter - perhaps better to say you match my understanding of these concepts). However I would advise against using this as a deciding metric when choosing whether to model those objects as reference or value in C#.

The key difference between a value and reference type is that the value types are copied when they are passed to a method. This means that they are more likely to sit on the stack rather than the heap and can be more costly to pass around; as such the size becomes a factor for consideration. The recommendation is that a struct should be below 16 bytes in size (at the bottom of remarks here) and a comprehensive address structure (House Number, House Name, Street Region, City, Country etc...) will easily break this.

That being said though, the semantics of entity:value :: class:struct are very similar and I can see a lot of benefits from modelling the data that way (two people who live at the same address don't share that address since changing one person's address shouldn't change the other's. So having the address as a struct would enforce this kind of separation. Whereas all instances of a person in the application should point to the same person). But there are performance and memory considerations. Perhaps immutable classes would be a good fit for these value types?

To sum up: The distinction between entity and value in DDD is based around what the object is. In code is should be based on what you intend to do with it.

Martin Harris
The entity/value object distinction can help with choosing between classes and structs in C#. I am sure there are considerations which could cause exceptions (to choose the opposite type when otherwise, all other facts about the object make it suitable for one type). However, one can modify an address (the building). Shouldn't this be mutable? An engine may be a struct and a composite of smaller items, but it too can be modified.
dotnetdev
Honestly I'm not sure I follow your comment entirely, but as regards the mutability of addresses, the reason I suggested an immutable class is that it stops the possibility that changes to address on person A get transferred to person B when they share an instance of an address class as it would force you to create a new unique instance for person A. This is the primary advantage you get from structs and I was trying to show that it would be possible to get the same behaviour with a class.
Martin Harris
It gets a little tricky, from a business perspective. For example, a family would have a house but this too would be a reference type. When the kids are young, they won't move house so the house is a shared reference.
dotnetdev
A value object would be something which cannot live on its own. So an attire can only be accessed through a Person object, likewise a Home through a homeowner or person, and a car is a property of a Driver. However, an engine is a property of a car so should this be a vo but an engine itself can have several smaller components (quite a few in fact). There are cases of more than 1 employee at the same company living in the same house, too.My thinking was that a struct should be an object which is immutable in real life.
dotnetdev
I agree that the semantics of a value object in DDD and a struct in OOP are very similar however I reiterate that a defining property of a struct that is not shared by a VO is that it should be lightweight otherwise it will adversely affect performance. Therefore I would still recommend an immutable class, rather than a struct, when you want to model a VO with large amounts of data - such as an address.
Martin Harris