Member parameters & Dependency parameters
As explained earlier, each parameter in the factory constructor corresponds to a property generated within the modddel.
There are two types of parameters : "member parameters" and "dependency parameters".
Member parameters
"Member parameters" represent properties held by the modddel. For example, in a Birthday ValueObject :
factory Birthday({
required DateTime date,
}) { // ...In this example, date is a member parameter of the Birthday ValueObject, as it holds the actual value (the date of the birthday).
Dependency Parameters
On the other hand, "dependency parameters" are used for dependency injection within your modddel. For example, the Birthday ValueObject might rely on a currentTimeService for validation. To achieve this, you can annotate the currentTimeService parameter with @dependencyParam:
factory Birthday({
required DateTime date,
@dependencyParam required ICurrentTimeService currentTimeService,
}) //...Here is a full example on how to use the dependency parameter :
As you can see :
(A) We annotate the
currentTimeServiceparameter with@dependencyParam.(B) After running the generator, we can access the
currentTimeServicefrom within the validation method(s) this way :We can also access it from an instance of the modddel :
Like any other instance member, you should NEVER access the instance's dependency parameter(s) from within the "validate" method(s).
Last updated