Skip to content

Instantly share code, notes, and snippets.

@luketlancaster
Created September 26, 2016 18:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save luketlancaster/bf1a2320132f44ec62955049ae7b4b7b to your computer and use it in GitHub Desktop.
Notes about Domain Value Objects

Something that has value (currency, address, etc)

Can be reused across domains. So a user entity can use the address value, just as the shop entity can as well

Immutable. If you want to change it, make a new one

https://lostechies.com/joeocampo/2007/04/23/a-discussion-on-domain-driven-design-value-objects/

“A Value Object cannot live on its own without an Entity"

“An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT. VALUE OBJECTS are instantiated to represent elements of the design that we care about only for what they are, not who or which they are.” [Evans 2003]

In the above post the author uses his kid as an example:

In this context the Attire type cannot exist on its own, it is dependent on the Kid Entity, it’s a Value object of that entity. "In order to access the attire, or change anything on it, you must go through the kid. This concept of ownership and responsibility is called an Aggregate in DDD"

“Does this object need identity outside of the object?"

http://culttt.com/2014/04/30/difference-entities-value-objects/

Person: entity bc/we can change their attributes, and they’re still the same object (represented by id in database) Location: has attrs for Lat + Lon, but we don’t care about the specific instance of Location, just that it is one

When the person changes location, we don’t update ‘Location,’ we create a new one.

Value Objects equality is not based on identity (Person.id === Person.id), but on the values themselves. -We can have two locations with equal Lat and Lon, and those will be equal even though they’re two objects

When to use an Entity vs Value? Context of the application

Wiki Definition: "In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object"

https://www.infoq.com/news/2015/01/aggregates-value-objects-ddd

Value objects are for Rayner a key building block pattern, some reasons being:

- Adds expressive names to key concepts in a model.
- Increases the conceptual cohesiveness of objects.
- Enables moving from using primitives for simple domain concepts like name of a customer.
- Allows entities to focus on identity by delegating behavioral logic to value objects.

http://deviq.com/value-object/ Value Objects can be especially useful as a means for describing concepts in an application that have intrinsic rules but which are not themselves entities.

Generally, validation of Value Objects should not take place in their constructor. Constructors as a rule should not include logic, but should simply assign values. If validation is required, it should be moved to a factory method, and indeed it is a common pattern to make Value Objects’ constructors private, and provide one or more public static methods for creating the Value Object.

https://leanpub.com/tdd-ebook/read#leanpub-auto-value-objects

https://github.com/nicolopignatelli/valueobjects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment