如何正确使用 ObjectBox flutter store 的开闭?

How properly use the ObjectBox flutter store opening and closing?

我希望定义 Flutter 和 ObjectBox(1.1.1 -> Flutter)的最佳开发实践。

我想建立一个由Repository组成的架构来实现我对存储在数据库中的对象的查询功能。这是对象人的示例。存储库将主要从 viewModels 调用。

import 'package:flutter_app_best_practice/data/person_repository_interface.dart';
import 'package:flutter_app_best_practice/model/person.dart';
import 'package:flutter_app_best_practice/objectbox.g.dart';
import 'package:objectbox/objectbox.dart';

class PersonRepository implements IPersonRepository{
  var _store;
  late Box _box;

  @override
  Future<List<Person>>? getPeople() {
    List<Person> list = _box.getAll() as List<Person>;
    return Future.value(list);
  }

  @override
  Future<void>? addPerson(String name, int age) async {
    Person person = new Person();
    person.name = name;
    person.age = age;
    _box.put(person);  // Add
    return;
  }

  @override
  Future<void>? initialize() async {
    // Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
    openStore().then((Store store) {
      _store = store;
    });

    _box = _store.box<Person>();

    return;
  }
}

这是我的问题:

您如何看待这个实现:


class PersonRepository implements IPersonRepository {

  @override
  Future<List<Person>>? getPeople() async {
    late List<Person> persons;

    await openStore().then((Store store) {
      var _box = store.box<Person>();
      persons = _box.getAll();
      store.close();
    });

    return Future.value(persons);
  }

  @override
  Future<void>? addPerson(String name, int age) async {

    await openStore().then((Store store) {
      var _box = store.box<Person>();
      Person person = new Person();
      person.name = name;
      person.age = age;
      _box.put(person);  // Add
      store.close();
    });
    
    return;
  }
}

Or call _store.close with this architecture ?

正常应用中可以忽略Store.close(),不需要过多的后台操作。

Instead of having an Initialize function, would it be better to open the store before each transaction and then close it after the transaction is complete?

不,开店“相当”昂贵,因此您不应该在每次访问商店时都这样做——这只会减慢您的速度,而且没有任何好处。

Are there examples of a similar structure using this latest version of objectBox (using openStore (). Then ((Store store))

objectbox 包本身有一个示例,尽管代码结构不同:https://github.com/objectbox/objectbox-dart/blob/main/objectbox/example/flutter/objectbox_demo/lib/main.dart#L79


给你一个例子,你可以做什么,同时保持你的代码试图做的抽象级别:

class PersonRepository implements IPersonRepository {
  final Store _store;
  final Box<Person> _box;

  PersonRepository(this._store) : _box = _store.box() {}

  @override
  Future<List<Person>>? getPeople() {
    List<Person> list = _box.getAll() as List<Person>;
    return Future.value(list);
  }

  @override
  Future<void>? addPerson(String name, int age) async {
    Person person = new Person();
    person.name = name;
    person.age = age;
    _box.put(person);  // Add
    return;
  }
}

abstract class IDatabaseRepositories {
  IPersonRepository get people;
}

class ObjectBoxRepositories implements IDatabaseRepositories {
  final Store _store;

  final PersonRepository people;

  static Future<IDatabaseRepositories> create() async =>
      ObjectBoxRepositories._(await openStore());

  ObjectBoxRepositories._(this._store) : people = PersonRepository(_store);
}