In an application, there are often similar but different object models, where the data in the two models may be similar, but the models have different structures and concerns. Object mapping is a convenient way to transform one model into another so that separate models remain isolated.

Dependent libraries

preface

Dart supports reflection, but Flutter disables Dart’s reflection ability, so some Java operations that require reflection are not available. After a short time of thinking, I decided to use serialization and deserialization to achieve object mapping.

I admit I was a bit lazy and should have done the serialization code myself, but due to time problems I implemented the object mapping based on JSON_serializable. Although this development is convenient, but the use of a little more trouble.

I will gradually fill in the serialization part of the code. If you’re interested, you can also come to Github and work on it.

The installation

Add the following dependencies to your pubspec.yaml:

dependencies:
  model_mapper: ^ 0.0.1
  
dev_dependencies:
  build_runner: ^ 1.10.3
  json_serializable: ^ 4.0.0
  model_mapper_generator: ^ 0.1.0 from
Copy the code

usage

Model_mapper runs on top of JSON_serialIZABLE, so the first step should be to configure Json_serializable

  1. Configure your Model with jSON_serializable:

    import 'package:json_annotation/json_annotation.dart';
    
    part 'demo.g.dart';
    
    @JsonSerializable(a)class Demo1 {
      final String a;
      final String b;
    
      Demo1(this.b, {required this.a});
    
      factory Demo1.fromJson(Map<String.dynamic> json) => _$Demo1FromJson(json);
    
      Map<String.dynamic> toJson() => _$Demo1ToJson(this);
    }
    
    @JsonSerializable(a)class Demo2 {
      final String a;
      final String b;
    
      Demo2(this.a, this.b);
    
      factory Demo2.fromJson(Map<String.dynamic> json) => _$Demo2FromJson(json);
    
      Map<String.dynamic> toJson() => _$Demo2ToJson(this);
    
      @override
      String toString() {
        return "Demo2 { a: $a, b: $b }"; }}Copy the code
  2. Create your ModelMapper and use ModelMapperFor to add all the models you want to map to each other.

    import 'package:model_mapper/model_mapper.dart';
    
    part 'demo_mapper.g.dart';
    
    @ModelMapperFor([Demo1, Demo2])
    abstract class DemoMapper extends ModelMapper {}Copy the code
  3. Execute the code generation command

    If you are a Flutter project you can run the Flutter packages pub run build_runner build.

    If the project is a Dart project, run Dart pub run build_runner build.

    Replacing build with watch can be executed automatically after the file changes: pub run build_runner watch.

  4. Now it’s ready to go. Note that the actual name of the ModelMapper class to be called is $before the name of the ModelMapper class you defined. For example, if you defined DemoMapper, you should call $DemoMapper

    var demo1 = Demo1("b", a: "a");
    
    var demo2 = $DemoMapper.instance().map(demo1, Demo2);
    print(demo2);
    // output: Demo2: { a: a, b: b }
    Copy the code

The last

Github: github.com/T-Oner/mode…