dart 在干净的体系结构中,从数据源获取实体列表时,如何在数据层为实体建立模型

kx5bkwkv  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(86)

我遵循一个简单的显示联系人项目的干净架构。在我的flutter项目中,我有这些目录:Data、Domain、Presentation。我首先在Domain层中为示例联系人创建了一个实体

class Contact {
  final String fullName;
  final String email;
  final bool favourite;

  Contact(
      {required this.fullName, required this.email, required this.favourite});
}

字符串
当我们从数据源获取联系人时,它通常是以List<Contact>的形式出现的,就像下面的JSON响应一样

{
  "contacts": [
    {
      "fullName" : "John Doe (JSON)",
      "email" : "johndoe@json.com",
      "favourite" : true
    },
    {
      "fullName" : "Ema Doe (JSON)",
      "email" : "emandoe@json.com",
      "favourite" : false
    },
    {
      "fullName" : "Micheal Roe (JSON)",
      "email" : "michro@json.com",
      "favourite" : false
    }
  ]
}


现在的问题是我们如何在数据层实现模型,这将是正确的基础上干净的架构。这个模型正确吗?

class ContactsModel {
  late List<Contact> contacts;

  ContactsModel({required this.contacts});

  ContactsModel.fromJson(dynamic json) {
    if (json['contacts'] != null) {
      contacts = [];
      json['contacts'].forEach((v) {
        contacts.add(ContactModel.fromJson(v));
      });
    }
  }
}

class ContactModel extends Contact {
  ContactModel(
      {required String fullName,
      required String email,
      required bool favourite})
      : super(fullName: fullName, email: email, favourite: favourite);

  ContactModel.fromJson(dynamic json)
      : super(
          fullName: json['fullName'],
          email: json['email'],
          favourite: json['favourite'],
        );
}

bkhjykvo

bkhjykvo1#

使用联系人列表作为模型类。在您的域层中,这里是您的实体

class Contact extends Equatable {
  final String fullName;
  final String email;
  final bool favourite;

  Contact(
      {required this.fullName, required this.email, required this.favourite});

  @override
  List<Object?> get props => [fullName, email, favourite];
}

字符串
这是数据层中的ContactModel

class ContactsModel {
  List<Contact> contacts = [];

  ContactsModel({required this.contacts});

  ContactsModel.fromJson(Map<String, dynamic> json) {
    if (json['contacts'] != null) {
      json['contacts'].forEach((v) {
        contacts.add(ContactModel.fromJson(v));
      });
    }
  }

  ContactsModel.fromXml(XmlDocument xmlDocument) {
    contacts = [];
    for (XmlElement xmlElement in xmlDocument.findAllElements('contact')) {
      contacts.add(ContactModel.fromXml(xmlElement));
    }
  }

  @override
  List<Object?> get props => [contacts];
}
class ContactModel extends Contact {
  ContactModel(
      {required String fullName,
      required String email,
      required bool favourite})
      : super(fullName: fullName, email: email, favourite: favourite);

  ContactModel.fromJson(Map<String, dynamic> json)
      : super(
          fullName: json['fullName'],
          email: json['email'],
          favourite: json['favourite'],
        );

  ContactModel.fromXml(XmlElement xmlElement)
      : super(
          fullName: xmlElement.findElements('fullName').single.text.trim(),
          email: xmlElement.findElements('email').single.text.trim(),
          favourite: xmlElement
                  .findElements('favourite')
                  .single
                  .text
                  .toLowerCase()
                  .trim() ==
              'true',
        );
}

相关问题