Bojs
Bojs

Reputation: 93

where to instantiate simple values /entity objects? DDD

Using domain driven design, where are simple value objects/entities instantiated?

For example, if i needed to create a simple value object in a service class, would I just call the new operator on the value object's class, coupling this to the service class?

Can the new operator be called in a service class according to domain driven design?

These value objects can't be injected through a DI container and they don't warrant the use of a factory because of their simplicity.

Upvotes: 0

Views: 745

Answers (1)

plalx
plalx

Reputation: 43718

What's wrong with instantiating them directly from their constructor?

Usually you only want factories when the instantiation process is complex or when you want to relieve the client from choosing a concrete class.

However, since the ubiquitous language is crucial in DDD it's quite common that an aggregate will have factory methods to create other aggregates to which they relates.

For example, rather than spawning project tasks out of thin air like below:

var task = new Task(projectId, ...);

You could do:

var task = project.addTask(...);

That would express the "tasks can be added to projects" use case better at the cost of having to load a Project aggregate.

Upvotes: 3

Related Questions