entity_mapper
Quick Start • Documentation • Example • GitHub
Clean Architecture entity mapping made simple.
A lightweight code generator that creates type-safe Entity ↔ Model mapping methods using dart_mappable-style patterns. Perfect for Clean Architecture and Domain-Driven Design applications.
Features
• 🎯 Clean Architecture ready: Perfect separation between domain and data layers
• 🔄 dart_mappable Pattern: Industry-standard approach and familiar API
• ⚡ Zero runtime overhead: All mapping code generated at build time
• 🛠️ Lightweight & focused: Only Entity ↔ Model mapping (no unnecessary bloat)
• 🔒 Fully type-safe: Generated code maintains complete type safety
• � Simple & focused: Just specify the entity type - that's it!
Quick Start
Requirements: Dart SDK ≥ 3.8.0, Flutter ≥ 3.32.0
Add dependencies:
flutter pub add entity_mapper
flutter pub add build_runner --dev
Annotate your model classes:
// domain/entities/user.dart
class User {
  const User({required this.id, required this.name, required this.email});
  final String id;
  final String name;
  final String email;
}
// data/models/user_model.dart
import 'package:entity_mapper/entity_mapper.dart';
part 'user_model.entity_mapper.dart';
@MapToEntity(User)
class UserModel with UserEntityMappable {
  const UserModel({required this.id, required this.name, required this.email});
  final String id;
  final String name;
  final String email;
}
Generate code and use:
dart run build_runner build
// Convert entity ↔ model
final user = User(id: '1', name: 'John', email: 'john@example.com');
final userModel = UserEntityMapper.toModel(user);
final backToEntity = userModel.toEntity();
Overview
Annotations
Use @MapToEntity() on model classes to specify the target entity:
@MapToEntity(User) // Target entity type
class UserModel with UserEntityMappable {
  const UserModel({
    required this.id,
    required this.name,
    required this.email,
  });
  
  final String id;
  final String name;
  final String email;
}
Generated API
Static Mapper Classes:
- {Entity}EntityMapper.toModel(entity)- Convert entity to model
- {Entity}EntityMapper.toEntity(model)- Convert model to entity
Mixin Methods:
- toEntity()- Convert this model instance to entity
Advanced Usage
Nested Models
@MapToEntity(Car)
class CarModel with CarEntityMappable {
  final String brand;
  final EngineModel engine;  // Automatically handles nested mapping
}
@MapToEntity(Engine)
class EngineModel with EngineEntityMappable {
  final String type;
  final int horsepower;
}
Lists and Collections
@MapToEntity(User)
class UserModel with UserEntityMappable {
  final String name;
  final List<String> tags; // Simple lists work automatically
  final List<AddressModel> addresses; // Nested model lists also supported
}
API Reference
@MapToEntity(Type entityType)
Parameters:
- entityType- The entity type to map to/from
Example:
@MapToEntity(User)
class UserModel with UserEntityMappable {
  // Model implementation
}
Examples
Check out the example directory for complete examples including nested models and real-world Clean Architecture scenarios.
Contributing
Contributions welcome! Submit issues and pull requests on GitHub.
License
Keywords
#clean-architecture #entity-mapping #code-generation #domain-driven-design #dart-mappable #build-runner #source-gen
Libraries
- entity_mapper
- Clean Architecture entity mapping made simple with code generation