“Some objects are not defined primarily by their attributes. They represent a thread of identity that runs through time and often across distinct representations… An object defined primarily by its identity is called an ENTITY” (Evans, 91)

Value objects as IDs

Value objects may be preferable to use as a type of ID property of the entity. For example, instead of:

@dataclass
class Product:
	id: UUID
	price: Decimal

use this instead:

@dataclass
class ProductId:
	id: UUID
	
	def __post_init__():
		# check if valid UUID
		...
 
 
@dataclass
class Product:
	id: ProductId
	price: Decimal

It can help with:

  • refactoring
  • guarding from invalid entities
  • help to catch bugs, when, for example, ID of the Order is passed to a method instead of an ID of the Product, and the static analyzer couldn’t catch it because they both have a UUID or str type that is expected by method.

Read more on that: