Skip to content

Feature Request: Support Generation from Domain Models #173

@etegan

Description

@etegan

Hello and first of: Thanks for these great packages!

Problem Statement

Currently, the generator requires models with nullable fields to represent form state, forcing developers to maintain two parallel models:

  1. A domain model with proper nullability representing the business logic
  2. A form state model with nullable fields for form handling

This leads to unnecessary duplication and makes it harder to maintain type safety between form submissions and domain objects.

Example of current approach:

// Domain Model
class User {
  final String name;      // Required in domain
  final String email;     // Required in domain
  final String? phoneNumber;
}

// Currently needs manual form model
class UserFormModel {
  final String? name;     // Must be nullable for form
  final String? email;    // Must be nullable for form
  final String? phoneNumber;
}

Proposed Enhancement

The generator should automatically generate both:

  1. The form model class with appropriate nullable fields
  2. An extension method on the domain model for conversion

The @Rf annotation should support configuring partial data injection through related models.

Example Usage

// Domain model with partial data configuration
@Rf(partialData: {
  'UserInvitation': {
    'email': 'email'  // maps invitation.email to userFormModel.email
  }
})
class User {
  final String name;
  final String email;
  final String? phoneNumber;
}

// Generated form model with inferred validators
class UserFormModel {
  final String? name;     // Generated with RequiredValidator() since non-nullable in User
  final String? email;    // Generated with RequiredValidator() since non-nullable in User
  final String? phoneNumber;  // No RequiredValidator() since nullable in User
}

// Generated extension
extension UserFormExtension on User {
  UserFormModel get formModel => UserFormModel(
    name: name,
    email: email,
    phoneNumber: phoneNumber,
  );
}

// Usage in UI
UserFormBuilder(
  // Optional initial data from domain model using generated extension
  model: existingUser?.formModel,  
  // Partial data injection configured by @Rf annotation
  userInvitation: UserInvitation(email: "example@email.com"),
  builder: (context, form, child) => ...
)

Benefits

  • Reduces boilerplate and maintenance overhead
  • Maintains single source of truth for model structure
  • Preserves type safety throughout the form lifecycle
  • Enables better IDE support and static analysis
  • Allows seamless integration with domain-driven design practices
  • Provides clean, intuitive API through generated extensions on domain model class
  • Declarative configuration of partial data injection
  • Automatic validation rules based on domain model nullability

Implementation Notes

The generator would need to:

  1. Analyze domain model field nullability
  2. Parse @Rf annotation for partial data configuration
  3. Generate form state model with nullable fields
  4. Generate validation rules based on domain model nullability
  5. Generate extension methods for converting domain models to form models
  6. Generate partial data injection logic based on annotation configuration

Would you consider this enhancement for a future release? It would significantly improve the developer experience when working with domain-driven architectures.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions