返回资讯中心
外部精选
软件工程
#开发者实时流

ArchitectureKit 1.0 for Rust 发布,支持基于 CQRS 和事件溯源的 DDD 应用

来源标题:ArchitectureKit 1.0 for Rust is There

ArchitectureKit 1.0 for Rust 已发布,为基于 DDD 的应用提供 CQRS 和事件溯源的构建模块,底层依赖 EventSourcingDB。该库覆盖写入侧的 commands、events 和状态决策,以及读取侧的 projections、views 和 queries,并可选通过 axum 以 HTTP 处理请求。使用需 Rust 1.88 或更高版本及运行中的 EventSourcingDB。

Hacker News 最新goloroden2026/11/13 · 今天9 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

ArchitectureKit 1.0 for Rust 现已发布

为基于 DDD 的 Rust 应用提供构建模块,支持 CQRS 和事件溯源,构建于 EventSourcingDB 之上——这是一个专为事件溯源打造的数据库。

ArchitectureKit 覆盖了事件溯源应用的两个方面:写入侧的 commands、events 以及用于决策的 state,读取侧的 projections、views 和 queries。一个可选特性支持通过 HTTP 处理 commands 和 queries,并提供 axum 的 handlers。

ArchitectureKit 的完整文档,包括前置条件、views、projections、queries、错误处理、测试和 HTTP,可在 https://www.eventfoundation.io/docs/architecturekit/rust 查看。API 文档位于 docs.rs。

ArchitectureKit 需要 Rust 1.88 或更高版本,以及一个运行中的 EventSourcingDB。要在你的机器上启动一个,请参阅 EventSourcingDB 的快速入门。

添加该 crate,同时添加用于事件数据的 serde,以及用于运行它的 Tokio:

cargo add architecturekit cargo add serde --features derive cargo add tokio --features macros,rt-multi-thread

ArchitectureKit 随附了 EventSourcingDB 的客户端 SDK,位于 architecturekit::eventsourcingdb,因此它们的版本始终匹配,客户端 SDK 的新主版本会随 ArchitectureKit 的新主版本一同发布。

创建一个 EventSourcingDB 客户端,以及一个使用该客户端和用于所有写入事件的 source 的 store:

use architecturekit::{Store, eventsourcingdb::Client}; let client = Client::new("http://localhost:3000".parse()?, "secret"); let store = Store::builder(client, "https://library.eventsourcingdb.io").build();

定义一个 command,描述某人想要做什么:

use architecturekit::{Command, Precondition, Subject}; #[derive(Clone)] struct AcquireBook { book_id: String, title: String, author: String, isbn: String, impl Command for AcquireBook { fn subject(&self) -> Subject { Subject::new(&format!("/books/{}", self.book_id)) fn preconditions(&self) -> Vec<Precondition> { vec![Precondition::on_state_read()]

定义一个 event,描述已经发生了什么:

use architecturekit::Event; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Event)] #[event(type = "io.eventsourcingdb.library.book-acquired")] struct BookAcquired { title: String, author: String, isbn: String,

定义 command 决策所依据的 state,以及 events 如何改变它:

use architecturekit::{Events, State}; #[derive(Events)] enum BookEvent { Acquired(BookAcquired), #[derive(Clone, Default)] struct Book { is_acquired: bool, impl State for Book { type Event = BookEvent; fn evolve(self, event: &BookEvent) -> Self { match event { BookEvent::Acquired(_) => Book { is_acquired: true },

将 state 与基于它的决策连接起来:

use architecturekit::{Decider, DomainError}; fn acquire_book() -> Decider<AcquireBook, Book, DomainError> { Decider::new(|command: AcquireBook, book: Book| async move { if book.is_acquired { return Err(DomainError::new(format!( "book has already been acquired: {}", command.book_id ))); Ok(vec![BookEvent::Acquired(BookAcquired { title: command.title, author: command.author, isbn: command.isbn, })]) })

执行该 command:

let written_events = store .execute( &acquire_book(), AcquireBook { book_id: "42".to_owned(), title: "2001 – A Space Odyssey".to_owned(), author: "Arthur C. Clarke".to_owned(), isbn: "978-0756906788".to_owned(), ) .await?;

其他所有内容,请参阅完整文档。

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →