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
  • Steps to create a SimpleEntity
  • Complete Example
  1. Entities
  2. SimpleEntity

Creating a SimpleEntity

PreviousSimpleEntityNextSpecial Cases

Steps to create a SimpleEntity

Declare your class this way :

class Person extends SimpleEntity<InvalidPerson, ValidPerson> with _$Person {}

Add the , the (don't forget including the contentValidation), and the .

Add the . The member parameters of the factory constructor should all be modddels.

//...
factory Person({
  required FullName name,
  required Age age,
}) //...

Create your .

You can now run the generator, and then override and implement the . Note that the "validate" method of the contentValidation is automatically generated, and you should never override it (Your IDE's Quick Fix won't override it neither).

Complete Example

// ... Imports & Part statements 

@Modddel(
  validationSteps: [
    ValidationStep([
      contentValidation,
    ]),
    ValidationStep([
      Validation('blackList', FailureType<PersonBlackListFailure>()),
    ])
  ],
)
class Person extends SimpleEntity<InvalidPerson, ValidPerson> with _$Person {
  Person._();

  factory Person({
    required Age age,
    required FullName name,
  }) {
    return _$Person._create(age: age, name: name);
  }

  @override
  Option<PersonBlackListFailure> validateBlackList(person) {
    /// Notice how we can directly access the fields of `age` and `name`,
    /// because at this point they are ValidModddels.
    if (person.name.firstName == 'Dash' &&
        person.name.lastName == 'Birdy' &&
        person.age.value == 28) {
      return some(const PersonBlackListFailure.blackListed());
    }
    return none();
  }
}

@freezed
class PersonBlackListFailure extends ValueFailure
    with _$PersonBlackListFailure {
  const factory PersonBlackListFailure.blackListed() = _BlackListed;
}
imports and part statements
@Modddel annotation
private empty constructor
factory constructor
failure(s) sealed class(es)
"validate" methods