cqrs_es/query.rs
1use async_trait::async_trait;
2use std::fmt::Debug;
3
4use serde::de::DeserializeOwned;
5use serde::Serialize;
6
7use crate::aggregate::Aggregate;
8use crate::event::EventEnvelope;
9
10/// Each CQRS platform should have one or more queries where it will distribute committed
11/// events.
12///
13/// Some example of tasks that queries commonly provide:
14/// - update materialized views
15/// - publish events to messaging service
16/// - trigger a command on another aggregate
17#[async_trait]
18pub trait Query<A: Aggregate>: Send + Sync {
19 /// Events will be dispatched here immediately after being committed.
20 async fn dispatch(&self, aggregate_id: &str, events: &[EventEnvelope<A>]);
21}
22
23/// A `View` represents a materialized view, generally serialized for persistence, that is updated by a query.
24/// This a read element in a CQRS system.
25///
26pub trait View<A: Aggregate>: Debug + Default + Serialize + DeserializeOwned + Send + Sync {
27 /// Each implemented view is responsible for updating its state based on events passed via
28 /// this method.
29 fn update(&mut self, event: &EventEnvelope<A>);
30}