da_gen 0.1.1 copy "da_gen: ^0.1.1" to clipboard
da_gen: ^0.1.1 copied to clipboard

This is a Dart CLI tool that automatically generates files for the Data layer in a MVVM structure.

da_gen #

Generate folders and files for the Data layer of MVVM in Flutter/Dart applications with a single command.

Key Features #

  1. Automatic Generation of Data Layer in MVVM

    • Creates DataSource, Repository, Model, DTO, and Mapper files
    • Sets up proper dependency relationships between components
  2. Optional Support for freezed and json_serializable

Installation #

dart pub global activate da_gen

Or add the following to your pubspec.yaml:

dev_dependencies:
  da_gen: ^0.1.0

Usage #

dagen <model_name>           # Required: Model name (positional argument)
      -d, --dir <directory>   # Optional: Base directory path (current version always uses the lib folder in project root)
      -f, --flat              # Optional: Generate all files in a single folder
      --freezed, -z           # Optional: Use freezed
      --json, -j              # Optional: Use json_serializable
      -h, --help              # Display help

Examples #

Basic Generation

dagen User

This generates the following file structure and code:

lib/
└── data/
    ├── data_source/
    │   ├── user_data_source.dart
    │   └── user_data_source_impl.dart
    ├── dto/
    │   └── user_dto.dart
    ├── mapper/
    │   └── user_mapper.dart
    ├── model/
    │   └── user.dart
    └── repository/
        ├── user_repository.dart
        └── user_repository_impl.dart
// lib/data/data_source/user_data_source.dart
abstract interface class UserDataSource {

}

// lib/data/data_source/user_data_source_impl.dart
import './user_data_source.dart';

class UserDataSourceImpl implements UserDataSource {
  
}

// lib/data/dto/user_dto.dart
class UserDto {

}

// lib/data/mapper/user_mapper.dart
import '../model/user.dart';
import '../dto/user_dto.dart';

extension UserMapper on UserDto {
  User toUser() {
    return User();
  }
}

// lib/data/model/user.dart
class User {

}

// lib/data/repository/user_repository.dart
abstract interface class UserRepository {

}

// lib/data/repository/user_repository_impl.dart
import './user_repository.dart';
import '../data_source/user_data_source.dart';

class UserRepositoryImpl implements UserRepository {
  final UserDataSource _userDataSource;

  const UserRepositoryImpl({
    required UserDataSource userDataSource,
  }) : _userDataSource = userDataSource;
}

Using Freezed

dagen User -z

This applies the freezed template to the Model class:

// lib/data/model/user.dart
import 'package:freezed_annotation/freezed_annotation.dart';

part 'user.freezed.dart';

@freezed
abstract class User with _$User {
  const factory User({
    
  }) = _User;
}

Using JSON Serializable

dagen User -j

This applies the json_serializable template to the DTO class:

// lib/data/dto/user_dto.dart
import 'package:json_annotation/json_annotation.dart';

part 'user_dto.g.dart';

@JsonSerializable(explicitToJson: true)
class UserDto {
  
  
  UserDto();
  
  factory UserDto.fromJson(Map<String, dynamic> json) => _$UserDtoFromJson(json);
  
  Map<String, dynamic> toJson() => _$UserDtoToJson(this);
}

Flat Directory Structure

dagen User -f

This generates all files in the current directory instead of nested folders.

License #

This project is distributed under the MIT License. See the LICENSE file for more details.

14
likes
0
points
13
downloads

Publisher

unverified uploader

Weekly Downloads

This is a Dart CLI tool that automatically generates files for the Data layer in a MVVM structure.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

args, path

More

Packages that depend on da_gen