Modddels
HomeGithubPub.dev
v0.2
v0.2
  • Motivation
  • Setup
  • Generalities
    • Modddels Overview
    • Validations & Validation steps
    • Structure of a Modddel
    • Member parameters & Dependency parameters
    • Usage
      • Data equality & toString
      • Pattern matching
      • Reading the fields
      • Reading the failures
      • CopyWith
  • ValueObjects
    • ValueObjects Overview
    • Creating a ValueObject
  • Entities
    • Entities Overview
    • ContentValidation & ContentFailure
    • ValidationSteps in Entities
    • SimpleEntity
      • Creating a SimpleEntity
      • Special Cases
    • IterableEntity & Iterable2Entity
      • IterableEntity / Iterable2Entity kinds
      • The Type Template
      • Creating an IterableEntity / Iterable2Entity
      • InvalidMembers Description
      • Special Cases
      • Creating your own IterableEntity / Iterable2Entity kind
  • Advanced Notions
    • The NullFailure Annotation
    • Using multiple parameters annotations
    • Class Hierarchy of ValueObjects
    • Class Hierarchy of Entities
    • Models that are always valid / invalid
  • Union of Modddels
    • Union of Modddels Overview
    • Basic Usage
    • Case-modddels pattern matching
    • Default Factory Constructor
    • Shared Properties
    • The Validate Methods
    • CopyWith
  • Unit-Testing Of Modddels
    • Unit-Testing Overview
    • Available Tests
    • Customizing Tests
  • Additional Information
    • Using type aliases
    • Re-running the generator
    • All Available Modddels
    • VsCode Snippets
Powered by GitBook
On this page
  • Member parameters
  • Dependency Parameters
  1. Generalities

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,
}) //...

The dependency parameters are not taken into account for data equality, and are not included in the toString output.

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;
}

PreviousStructure of a ModddelNextUsage