tl;dr: Entity constructor should be called once in its entire life. That’s why good ORMs don’t use constructor to reconstitute objects from the database. Good ORMs use reflection, deserialization or proxies. So, fire your Domain Events in the constructor.
Last week, I saw a presentation about going from coupled-to-framework code to a Domain-Driven Design approach in a sample application. It was a good talk. However, in my honest opinion, there were some mistakes that are important to address. In order not to troll, I have checked with the speakers to write this post ;).
One of the mistakes, that I would like to point is where a Domain Event about “a new product was created” should be trigger specifically inside the Product Entity.
Speakers were showing a “Video” Entity with a named constructor called “create”. They were calling _new _inside the named constructor and then firing the event. I mean, the Domain Event was not fired inside of the real constructor. Let’s see the code we’re talking about (the whole repository can be found here):
The reason about this approach was because when dealing with databases, new can be called multiple times when fetching back the object. I think here’s is the misunderstanding. :)
Consider the following version that fires the event inside the __constructor:
In terms of DDD, the second approach is better. The Domain Event is fired when the Entity is created. But if you consider the first approach, a part from the mistake of the constructor being public (it’s a good practice to make it private with named constructors), the Domain Event is fired right after the Entity is created.
However, let’s argue about the possible problem with the last approach with ORMs. They were defending that the first approach is better because when you fetch an instance from the Database it doesn’t call the constructor so the Domain Event is not fired again. That’s not right.
Let’s see some Doctrine code.
Doctrine depends on “doctrine/instatiator” package on composer.json
Doctrine uses Instantiator to build new instances without calling new. Check it on github.
In my personal experience, in the LastWishes project, I sign up a user once and then sign in multiple times. The Domain Event is fired in the constructor and there is only one creation event fired ever.
How many times new should be called in the life of an Entity?
Beyond this specific topic, I would like to point another even more general idea around this topic. How many times new should be called in the life of an Entity? One. Correct! It doesn’t matter how this Entity is persisted and retrieved, an Entity is created once. Then it remains alive persisted in memory or in a database. No-one should invoke new again.
Hope it helps.