Replies: 1 comment
-
While I still consider myself a learner and a newbie, I will try to share my understanding and experience. While you can try to delegate the entity creation and it is often seen in most demos, I also seen quite a few scenarios where you have a public constructor to build the domain entity. Let's pick banking-kata-dotnet, while having a builder factory for the bank account, that is used in multiple places, we can also see that there is a public constructor: BankAccount.cs And you can see it being used in the command handler: OpenAccountUserCase.cs That said, I think there is not a single right way to do so, and in the end it might be related with personal or team preference and coding standards. I'm sure some one more experienced will help out clearing the doubt @tomazp |
Beta Was this translation helpful? Give feedback.
-
Hello everyone.
I've been wondering about the best way to construct domain entities. I often read that domain entity creation should be delegated to the domain layer via factory - either factory method or dedicated factory class.
But this doesn't seem practical when domain entities are large (many fields) which happens often in the real applications.
So far I've been using a builder for this while keeping the constructors private. But both the Builder and the build method are public. In practice the entity ends up being assembled in a command handler.
Am I approaching this correctly? Should domain object creation be completely hidden from the outside?
Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions