I'm studying DDD at the same time that I'm reading some Khalil Stemmler posts and I'm a little confused about who should initialize the VO: the entity it is related to or the useCase that uses that entity. A simple example:
Let's assume that we are developing an internal purchase system for a company that has suppliers in several countries.
So the entity Supplier
may have properties like name: string
, country: Country
and taxId: ???
. The taxId
property has complicated business rules because when a Supplier
is from USA the validation rules from TaxId follows the TIN rules, but if the Supplier
is from Brazil the taxId validation rules follow the CNPJ rules. Because of this, it is necessary to have a Value Object for TaxId (which probably depends on Country to be instantiated) that deals with these complicated rules, right?
Then follows the central point of my doubt: When instantiating my entity Supplier
(in a useCase for example) I must pass as a parameter to the factory method a taxId: string
(raw format) and the factory instantiates the Value Object TaxID
(ie, if the TaxId is wrongly formatted I can't instantiate my Supplier
entity and I get a error message) or should I instantiate the TaxId
VO before and in case of success I pass taxId: TaxId
as the factory parameter?