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 :
@Modddel(
validationSteps: [
ValidationStep(
[Validation('age', FailureType<BirthdayAgeFailure>())],
name: 'Value',
),
]
)
class Birthday extends SingleValueObject<InvalidBirthday, ValidBirthday>
with _$Birthday {
Birthday._();
factory Birthday({
required DateTime date,
// (A)
@dependencyParam required ICurrentTimeService currentTimeService,
}) {
return _$Birthday._create(
date: date,
currentTimeService: currentTimeService,
);
}
@override
Option<BirthdayAgeFailure> validateAge(birthday) {
// (B)
final service = birthday.currentTimeService;
final age = service.getAge(birthday.date);
if (age < 13) {
return some(const BirthdayAgeFailure.tooYoung());
}
return none();
}
}
@freezed
class BirthdayAgeFailure extends ValueFailure with _$BirthdayAgeFailure {
const factory BirthdayAgeFailure.tooYoung() = _TooYoung;
}
As you can see :
(A) We annotate the
currentTimeService
parameter with@dependencyParam
.(B) After running the generator, we can access the
currentTimeService
from within the validation method(s) this way :@override Option<BirthdayAgeFailure> validateAge(birthday) { final service = birthday.currentTimeService; // ... }
We can also access it from an instance of the modddel :
final birthday = Birthday( date: DateTime(1995, 8, 20), currentTimeService: currentTimeService, ); final service = birthday.currentTimeService;
Like any other instance member, you should NEVER access the instance's dependency parameter(s) from within the "validate" method(s).
@override
Option<BirthdayAgeFailure> validateAge(birthday) {
// Bad, will throw a runtime error.
final service = this.currentTimeService;
// Good
final service = birthday.currentTimeService;
}
Last updated