Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base Classes "Entity" and "ValueObject" in Domain-Driven Design

Do you always create these two abstract base classes as the basis of any new project in DDD?

I've read that Entity should have two things. First, an identity property, probably of a generic type. Second, an Equals() method that determines whether it's the same as another Entity. Anything else? Any other natural methods or rules of thumb?

like image 521
S. Valmont Avatar asked Jun 02 '11 17:06

S. Valmont


People also ask

What is entity in Domain-Driven Design?

In domain-driven design, an entity is a representation of an object in the domain. It is defined by its identity, rather than its attributes. It encapsulates the state of that object through its attributes, including the aggregation of other entities, and it defines any operations that might be performed on the entity.

What is the difference between entity and value object in DDD?

The main difference between entities and value objects lies in the way we compare their instances to each other. The concept of identifier equality refers to entities, whereas the concept of structural equality - to value objects. In other words, entities possess inherent identity while value objects don't.

What is Valueobject in C#?

Value object implementation in C# In terms of implementation, you can have a value object base class that has basic utility methods like equality based on the comparison between all the attributes (since a value object must not be based on identity) and other fundamental characteristics.

What is DDD example?

An aggregate is a domain-driven design pattern. It's a cluster of domain objects (e.g. entity, value object), treated as one single unit. A car is a good example. It consists of wheels, lights and an engine.


2 Answers

I like to have a common abstract ancestor for all my Domain objects but that is a matter of preference and overall infrastructure requirements.

After that, yes I have abstract classes for Entity and Value objects.

Don't forget that also overriding Equals for Value objects to return equality based on equal property state can be important.

Also people frequently overlook the value of packages. Put all these core base classes in their own "kernel" library and don't be reluctant to split your domain model into multiple assemblies instead of winding up with a single large "Domain Library".

like image 94
Sisyphus Avatar answered Nov 01 '22 14:11

Sisyphus


If you're using .NET/C#, I've published a set of DDD interfaces and classes for public use. Take a look to see what typically goes inside them. The embedded code comments should hint towards their usage.

You can [download it here][1]. Project is dead now.

like image 26
Vijay Patel Avatar answered Nov 01 '22 13:11

Vijay Patel