【问题标题】:storing a nested data structure in ObjectBox with type conversion method (.toObjectBox)使用类型转换方法 (.toObjectBox) 在 ObjectBox 中存储嵌套数据结构
【发布时间】:2021-08-19 14:42:14
【问题描述】:

我猜最好(唯一?)的方法是为嵌套结构创建一个单独的框,然后创建从父级 (ONE) 到子级 (MANY) 的关系。

但是,我的问题是如何实现 .toObjectBox() 方法将我的域数据模型转换为 ObjectBox 数据模型。 dartz 包中是否有类似的Either(返回左侧或右侧对象),它同时传输 2 个对象?

ObjectBox 实体也是如此

@Entity()
class Customer {
  int id;
  @Backlink('customer')
  final orders = ToMany<Order>();
}

@Entity()
class Order {
  int id;
  final customer = ToOne<Customer>();
}

我会将父级(客户)和嵌套数据(订单)存储在 2 个相关框中

Customer customer = domainData.toObjectBox; // but how to get the order out?
customer.orders.add(Order('Order 1'));  // shouldn't this be Order(1)?
final customerId = store.box<Customer>().put(customer);

这就是我通常实现toObjectBox 方法的方式。在这里,您会看到这行不通,因为我必须将父级和嵌套的子级分开。虽然我想我可以用一些意大利面条来实现这一点,但我想知道是否有一种聪明的方法可以做到这一点,因为我想这应该是一种常见的模式(尽管我还没有找到任何关于它的问答)。

@Entity()
Customer {
  int id;
  List<Order> orders;
  Customer({required this.id, required this.orders});
  
  CustomerObox toObjectBox() {
    return CustomerObox(
      id: id,
//      orders: orders.map((x) => x.toObjectBox()).toList()
    );
}

=== 更新 ======================================

我同时尝试自己创建一个退货结构,现在我正在努力实现这一目标。

class Pair<T1, T2> {
  final T1 parent;
  final T2 child;
  Pair({required this.parent, required this.child});
}

@Entity()
class Customer {
  int id;
  List<Order> orders;
  Customer({required this.id, required this.orders});
      
  static Pair<CustomerObox, List<OrderObox>> toObjectBox(Customer cust) {
    Pair<CustomerObox, List<OrderObox>>(
        parent: CustomerObox(
                  id: cust.id,
                ),
        child: cust.orders.map((o) => o.toObjectBox()).toList()
    );
  }      
}

【问题讨论】:

  • 您不能/不想用@Entity() 注释您的域类的原因,如第一个代码块中所述?这将产生最好的结果,无需任何手动编写(==脆弱)转换......
  • 只是忘了在这里输入。由于我的真实代码会使其更难遵循,因此我调整了 ObjectBox 示例 - 但仅在 stackoverflow 的编辑器中,而不是在 IDE 中。我更新了代码,所以我假设所有 @Entity() 现在都应该存在

标签: flutter clean-architecture objectbox flutter-objectbox


【解决方案1】:

所以我实现了toObjectBox,如上面我问题的更新所示。

在我的本地数据源实现中(我使用层:表示 > 业务逻辑(块)> 域(模型、合同等)> 存储库 > 数据源)我实现了以下方法

@override
  Future<void> storeCustomer(Customer customer) async {
    // Pair(T1 parent, T2 child) with T2 being List<OrderObox>
    final Pair customerObox = Customer.toObjectBox(customer);
    final CustomerObox customerParent = customerObox.parent;
    final List<OrderObox> customerChildren = customerObox.child;

    for (final child in customerChildren)
      customerParent.records.add(child);
    //  puts both, parent and children:
    final id = _sBox.put(customerParent);
  }
  @override
  Future<List<Customer>> getAllCustomers() async {
    final List<Customer> parents = [];
    final List<CustomerObox> parentsObox = await _sBox.getAll();
    for (final p in parentsObox) {
      final List<CustomerObox> childrenObox = p.records;
      parents.add(SessionJournalDto.fromObjectBox(p));
      for (final c in childrenObox)
        parents.last.records.add(Order.fromObjectBox(c));
    }
    return parents;

  }

【讨论】:

  • 如果有人有更好的主意,请告诉我
猜你喜欢
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多