Loopback 4 什么是存储库?

标签 loopback v4l2loopback

我很难理解 Loopback 4 中 Repositories 的概念,文档说:

A Repository represents a specialized Service interface that provides strong-typed data access (for example, CRUD) operations of a domain model against the underlying database or service.

但是这个描述并不能帮助我完全理解它们背后的想法,有人可以简单地解释一下它到底是什么,并提供一些其他框架中类似概念的例子吗?

最佳答案

当使用 ORM(对象关系映射器)框架时,我们将数据表示为模型类:

@model()
class Person {
  @property()
  name: string;
}

要持久化和查询数据,我们需要向我们的模型添加行为。 存储库是提供此类行为的类。

例如,LoopBack 的 EntityCrudRepository 接口(interface)描述了在 SQL 表/NoSQL 文档集合中创建、更新、删除和查询数据的方法。

// simplified version
interface EntityCrudRepository<T, ID> {
  create(data: DataObject<T>): Promise<T>;
  find(filter?: Filter<T>): Promise<T[]>;
  updateById(id: ID, data: DataObject<T>): Promise<void>;
  deleteById(id: ID): Promise<void>;
  // etc.
}

KeyValueRepository 描述了用于像 Redis 这样的键值存储的 API:

interface KeyValueRepository<T> {
  get(key: string): Promise<T>;
  set(key: string, value: DataObject<T>): Promise<void>;
  delete(key: string): Promise<void>;
}

另见 Patterns of Enterprise Application Architecture :

Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.

关于Loopback 4 什么是存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54382351/

相关文章:

javascript - 如何将包含图像文件的流发送到环回中的远程方法?

c++ - 如何写入/通过 V4L2loopback 模块创建的虚拟网络摄像头?

ffmpeg 水平翻转网络摄像头到虚拟摄像机

windows-subsystem-for-linux - 在 WSL2 中安装 v4l2loopback

python - 尝试使用 PyFakeWebcam 时出现 IOError

node.js - 环回中日期和时间的数据类型是什么

iphone - iOS(iPad、iPhone)有环回接口(interface)吗?

javascript - 生成新的验证 token

node.js - 环回组件存储文件上传请求中止 formidable\lib\incoming_form.js