Question

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?

Was it helpful?

Solution

A Supplier factory that that calls a TaxId VO factory is OK. Similarly a Supplier factory that takes a TaxId VO as parameter is also OK. However, avoid having the TaxId validation in the Supplier factory.

Depending on your use cases, you might need to be able to create a TaxId VO without the Supplier entity. If you have such use case, then a Supplier factory that takes an existing TaxId VO would be convenient.

By the way, you should not have an invalid TaxId VO. Yet, the validity of the TaxId VO depends on a Country. Thus, I'd include Country in the TaxId VO (you would be passing the Country to the TaxId factory anyway).

Licensed under: CC-BY-SA with attribution
scroll top