---
title: "Concepts"
description: "Dex 的编程模型很精简。Flow 是一个 durable program，Step 推进它，Primitive 保存它的状态并把它和外部连接起来。你的应用通过 Client 驱动 Flow。"
canonical_url: "https://docs.superdurable.io/zh-Hans/concepts/"
source: "docs/i18n/zh-Hans/docusaurus-plugin-content-docs/current/concepts/index.mdx"
locale: "zh-Hans"
---

# Concepts

Dex 的编程模型很精简。Flow 是一个 durable program，Step 推进它，Primitive 保存它的状态并把它和外部连接起来。你的应用通过 Client 驱动 Flow。

<a id="flow-and-step"></a>

## Flow 和 Step

| 概念                                                            | 是什么                                         | 用来做什么        |
| ------------------------------------------------------------- | ------------------------------------------- | ------------ |
| [Flow](https://docs.superdurable.io/zh-Hans/concepts/flow.md) | 带业务 ID 的 durable program。它拥有自己的 Step、状态和消息。 | 建模一个业务流程或实体。 |
| [Step](https://docs.superdurable.io/zh-Hans/concepts/step.md) | Flow 中的一个工作单元。它先等待，再执行，然后决定接下来运行什么。         | 完成工作，推进流程。   |

<a id="primitive-一览"></a>

<a id="the-primitives"></a>

## Primitive

Primitive 是 Flow 使用的构件。每个 Primitive 都属于一个 Flow。

| Primitive                                                                          | 是什么                          | 用来做什么                      |
| ---------------------------------------------------------------------------------- | ---------------------------- | -------------------------- |
| [Attribute](https://docs.superdurable.io/zh-Hans/concepts/primitives/attribute.md) | 属于一个 Flow 的 durable 状态。      | 在 Step 之间保存数据，从外部读取，并按它搜索。 |
| [RPC](https://docs.superdurable.io/zh-Hans/concepts/primitives/rpc.md)             | 对 Flow 的同步调用。                | 从应用发送命令、读取视图。              |
| [Channel](https://docs.superdurable.io/zh-Hans/concepts/primitives/channel.md)     | Flow 中的 durable 消息队列。        | 投递 Step 等待的消息。             |
| [Stream](https://docs.superdurable.io/zh-Hans/concepts/primitives/stream.md)       | 读取方可以续读的 best-effort feed。   | 向 UI 推送实时进度。               |
| [Timer](https://docs.superdurable.io/zh-Hans/concepts/primitives/timer.md)         | 等到某个时间的 durable 等待。          | 设置截止时间、提醒和定时任务。            |
| [SubFlow](https://docs.superdurable.io/zh-Hans/concepts/primitives/subflow.md)     | 由另一个 Flow 作为子 Flow 启动的 Flow。 | 把大量或并行的工作拆成子 Flow。         |

<a id="client"></a>

## Client

[Client](https://docs.superdurable.io/zh-Hans/concepts/client.md) 是应用在 Worker 之外使用的 API，用来启动、调用、读取、等待和运维 Flow。

<a id="connector"></a>

## Connector

[Connector](https://docs.superdurable.io/zh-Hans/concepts/connector.md) 把 Flow 接到外部服务商，例如邮件或支付服务。它为 Flow 提供调用服务商的现成 Step，以及根据服务商事件启动或更新 Flow 的 Trigger。

## 它们如何配合

```text
 Your application                      One Flow (one Flow ID)
 ┌────────────┐   StartFlow, RPC      ┌──────────────────────────────────┐
 │   Client   │ ────────────────────▶ │  Step ──▶ Step ──▶ Step ...      │
 │            │   GetAttributes, wait │   waits on Channel, Timer,       │
 │            │ ◀──────────────────── │   SubFlow; calls Connectors      │
 │            │   Stream (live)       │                                  │
 │            │ ◀──────────────────── │  Attributes  Channels  Streams   │
 └────────────┘                       └──────────────────────────────────┘
```

**Flow** 是容器。一个 Flow ID（例如一个订单号）同一时间最多只有一个 active run。

**Step** 推进 Flow。Step 可以等待 Channel 消息、Timer 或 SubFlow，然后执行自己的工作，例如通过 Connector 调用外部服务。最后它返回一个 decision，指定接下来的 Step，或者关闭 Flow。Step 失败时，Dex 会重试。

**Attribute** 和 **Channel** 是 Flow 的 durable 状态。Step 和 RPC 读写它们。Dex 把 Step 方法或 RPC 的状态变更和它的结果一起保存，见 [Step 的保证](https://docs.superdurable.io/zh-Hans/concepts/step.md#guarantees)。

应用通过 **Client** 启动 Flow、调用 RPC、读取 Attribute 和等待变化。要修改运行中的 Flow，应用调用 RPC。**Stream** 把实时更新送回 UI。

## Component

这些概念组成编程模型。[Component](https://docs.superdurable.io/zh-Hans/components.md) 是运行它的进程：

- 你的 **application** 在 Worker 中运行 Flow 代码，并通过 Client 调用 Dex。
- **server-zd** 保存 Flow 状态，并把 Step 和 RPC 交给 Worker。
- **dexcli** 在本地运行 Dex，**Dex Web** 让运维人员查看 Flow。

## 下一步

- 先读 [Flow](https://docs.superdurable.io/zh-Hans/concepts/flow.md) 和 [Step](https://docs.superdurable.io/zh-Hans/concepts/step.md)。各个 Primitive 都建立在它们之上。
- [Design patterns](https://docs.superdurable.io/zh-Hans/design-patterns.md) 说明如何组合这些概念解决常见问题。
- [Correctness and expressiveness](https://docs.superdurable.io/zh-Hans/correctness-and-expressiveness.md) 说明 Dex 能表达什么，以及它如何保持正确。
- [Developer reference](https://docs.superdurable.io/zh-Hans/reference.md) 包含参数、默认值、上限和版本细节。
