1

While designing a class I assumed that having an $id property will make that class Entity rather than a value object.

I also have a toArray() method which converts the object to associative array and that response is send to post and patch api's.

Now I have the following questions:

POST works, since I’m not sending the id in the body. But for PATCH is it fine if I set the property dynamically after object creation? For Ex:

$redCircle = new Circle(“red”);
$redCircle->id = 10;
$api->patch($redCircle->toArray());
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
Dinesh
  • 11
  • 1
  • 4

1 Answers1

0

Your point of view is very technical, as opposed to DDD.

You should design your Aggregates and nested Entities according to the business rules (the invariants).

While designing a class I assumed that having an $id property will make that class Entity rather than a value object.

This is not true. There are cases when a local Value object (that comes from a remote Aggregate, in another Bounded context) needs an ID property in order to be kept up-to-date (i.e. by background tasks). The Anti-corruption layer would need this property, so the reasons are pure technical.

POST works, since I’m not sending the id in the body. But for PATCH is it fine if I set the property dynamically after object creation?

Again, this is not a DDD view of the problem. In DDD, an Aggregate execute commands: one does not simply update its internal state directly; this would break its encapsulation.

But to answer your question, considering that you have a CRUD app: In order to mutate parts of an Entity you would need to load it from the Repository before mutation. The Repository would set the ID along with the other properties.

One of the best ways to locate entities is to use a RESTful API, so the client would not need to construct the URLs for the PATCH operation.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
  • Thanks for the info. But I have a follow-up question. What if the data coming from form is converted to a domain object and that domain object is passed to API body in some format. And the Id on the object is generated by the database table. – Dinesh Jan 17 '19 at 16:01
  • @SaiVallamsetti then the Repository would populate the ID accordingly – Constantin Galbenu Jan 17 '19 at 16:06