From 0d855c3c10abf53aa38682324dd30e99b15b9d41 Mon Sep 17 00:00:00 2001 From: "daniel.eades" Date: Thu, 2 Jan 2025 15:00:33 +0000 Subject: [PATCH] update the 'View' trait --- src/persist/doc.rs | 9 +++++---- src/persist/generic_query.rs | 8 ++++---- src/persist/view_repository.rs | 2 +- src/query.rs | 6 ++++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/persist/doc.rs b/src/persist/doc.rs index a9764bc..bd610c3 100644 --- a/src/persist/doc.rs +++ b/src/persist/doc.rs @@ -1,10 +1,10 @@ -use crate::doc::MyAggregate; +use crate::doc::{MyAggregate, MyEvents}; use crate::persist::event_stream::ReplayStream; use crate::persist::{ PersistedEventRepository, PersistenceError, SerializedEvent, SerializedSnapshot, ViewContext, ViewRepository, }; -use crate::{Aggregate, EventEnvelope, View}; +use crate::{Aggregate, View}; use async_trait::async_trait; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -12,8 +12,9 @@ use serde_json::Value; #[derive(Debug, Default, Serialize, Deserialize)] pub struct MyView; -impl View for MyView { - fn update(&mut self, _event: &EventEnvelope) { +impl View for MyView { + type Event = MyEvents; + fn apply(&mut self, _event: &Self::Event) { todo!() } } diff --git a/src/persist/generic_query.rs b/src/persist/generic_query.rs index bafefb9..4840c92 100644 --- a/src/persist/generic_query.rs +++ b/src/persist/generic_query.rs @@ -11,7 +11,7 @@ use crate::{Aggregate, EventEnvelope, Query, View}; pub struct GenericQuery where R: ViewRepository, - V: View, + V: View, A: Aggregate, { view_repository: Arc, @@ -22,7 +22,7 @@ where impl GenericQuery where R: ViewRepository, - V: View, + V: View, A: Aggregate, { /// Creates a new `GenericQuery` using the provided `ViewRepository`. @@ -102,7 +102,7 @@ where ) -> Result<(), PersistenceError> { let (mut view, view_context) = self.load_mut(view_id.to_string()).await?; for event in events { - view.update(event); + view.apply(&event.payload); } self.view_repository.update_view(view, view_context).await?; Ok(()) @@ -119,7 +119,7 @@ where impl Query for GenericQuery where R: ViewRepository, - V: View, + V: View, A: Aggregate, { async fn dispatch(&self, view_id: &str, events: &[EventEnvelope]) { diff --git a/src/persist/view_repository.rs b/src/persist/view_repository.rs index 56e151c..1ea35de 100644 --- a/src/persist/view_repository.rs +++ b/src/persist/view_repository.rs @@ -6,7 +6,7 @@ use async_trait::async_trait; #[async_trait] pub trait ViewRepository: Send + Sync where - V: View, + V: View, A: Aggregate, { /// Returns the current view instance. diff --git a/src/query.rs b/src/query.rs index df9b77b..d689b24 100644 --- a/src/query.rs +++ b/src/query.rs @@ -23,8 +23,10 @@ pub trait Query: Send + Sync { /// A `View` represents a materialized view, generally serialized for persistence, that is updated by a query. /// This a read element in a CQRS system. /// -pub trait View: Debug + Default + Serialize + DeserializeOwned + Send + Sync { +pub trait View: Debug + Default + Serialize + DeserializeOwned + Send + Sync { + /// The event type that this view is constructed from + type Event; /// Each implemented view is responsible for updating its state based on events passed via /// this method. - fn update(&mut self, event: &EventEnvelope); + fn apply(&mut self, event: &Self::Event); }