仓库 24.00
Repository模式在webforJ中提供了一种标准化的方式来管理和查询实体集合。它作为UI组件和数据之间的抽象层,使得在维护一致性行为的同时,轻松处理不同的数据源。
为什么使用仓库
Repository消除了手动更新,同时保持原始数据不变:
// 没有仓库 - 手动更新
List<Customer> customers = loadCustomers();
Table<Customer> table = new Table<>();
table.setItems(customers);
// 添加需要完全重新加载
customers.add(newCustomer);
table.setItems(customers); // 必须重新加载所有内容
// 有仓库 - 自动同步
List<Customer> customers = loadCustomers();
CollectionRepository<Customer> repository = new CollectionRepository<>(customers);
Table<Customer> table = new Table<>();
table.setRepository(repository);
// 添加自动同步
customers.add(newCustomer);
repository.commit(newCustomer); // 只更新已更改的内容
集合仓库
CollectionRepository是最常见的实现,包装任何Java集合:
// 来自ArrayList
List<Customer> customers = new ArrayList<>();
CollectionRepository<Customer> customerRepo = new CollectionRepository<>(customers);
// 来自HashSet
Set<String> tags = new HashSet<>();
CollectionRepository<String> tagRepo = new CollectionRepository<>(tags);
// 来自任何集合
Collection<Employee> employees = getEmployeesFromHR();
CollectionRepository<Employee> employeeRepo = new CollectionRepository<>(employees);