From 883edb027397a3e66e399ce49d4e1dd395f32a1b Mon Sep 17 00:00:00 2001 From: Almaju Date: Tue, 19 Dec 2023 23:32:37 +0100 Subject: [PATCH] Refactor command and query execution to use "runtime" instead of "services" --- application/src/commands.rs | 20 ++++++++++---------- application/src/queries.rs | 4 ++-- crates/framework/src/lib.rs | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/application/src/commands.rs b/application/src/commands.rs index e04fa39..33473b8 100644 --- a/application/src/commands.rs +++ b/application/src/commands.rs @@ -7,16 +7,16 @@ use framework::*; use serde::Deserialize; // Shared command logic -async fn pull_and_push(services: &R, message: &TodoListMessage) -> Result<()> +async fn pull_and_push(runtime: &R, message: &TodoListMessage) -> Result<()> where R: TodoListRepository + TodoListStore + Send + Sync, { - let mut todolist = services.pull().await?; + let mut todolist = runtime.pull().await?; let new_events = message.send(&todolist)?; new_events.apply(&mut todolist); let projection = TodoListProjection::project(&todolist); - services.push(&new_events).await?; - services.save(&projection).await?; + runtime.push(&new_events).await?; + runtime.save(&projection).await?; Ok(()) } @@ -30,8 +30,8 @@ impl Command for AddTaskCommand where R: TodoListRepository + TodoListStore + Send + Sync, { - async fn execute(&self, services: &R) -> Result<()> { - pull_and_push(services, &TodoListMessage::AddTask(self.name.clone())).await?; + async fn execute(&self, runtime: &R) -> Result<()> { + pull_and_push(runtime, &TodoListMessage::AddTask(self.name.clone())).await?; Ok(()) } } @@ -46,8 +46,8 @@ impl Command for RemoveTaskCommand where R: TodoListRepository + TodoListStore + Send + Sync, { - async fn execute(&self, services: &R) -> Result<()> { - pull_and_push(services, &TodoListMessage::RemoveTask(self.index)).await?; + async fn execute(&self, runtime: &R) -> Result<()> { + pull_and_push(runtime, &TodoListMessage::RemoveTask(self.index)).await?; Ok(()) } } @@ -62,8 +62,8 @@ impl Command for CompleteTaskCommand where R: TodoListRepository + TodoListStore + Send + Sync, { - async fn execute(&self, services: &R) -> Result<()> { - pull_and_push(services, &TodoListMessage::CompleteTask(self.index)).await?; + async fn execute(&self, runtime: &R) -> Result<()> { + pull_and_push(runtime, &TodoListMessage::CompleteTask(self.index)).await?; Ok(()) } } diff --git a/application/src/queries.rs b/application/src/queries.rs index 7787fe4..88cb6a7 100644 --- a/application/src/queries.rs +++ b/application/src/queries.rs @@ -12,7 +12,7 @@ where { type Output = TodoListProjection; - async fn execute(&self, services: &R) -> Result { - services.fetch().await + async fn execute(&self, runtime: &R) -> Result { + runtime.fetch().await } } diff --git a/crates/framework/src/lib.rs b/crates/framework/src/lib.rs index a10adfe..93e159e 100644 --- a/crates/framework/src/lib.rs +++ b/crates/framework/src/lib.rs @@ -21,11 +21,11 @@ pub trait Projection: Serialize + for<'de> Deserialize<'de> { #[async_trait] pub trait Command: for<'de> Deserialize<'de> { - async fn execute(&self, services: &R) -> Result<()>; + async fn execute(&self, runtime: &R) -> Result<()>; } #[async_trait] pub trait Query: for<'de> Deserialize<'de> { type Output: Projection; - async fn execute(&self, services: &R) -> Result; + async fn execute(&self, runtime: &R) -> Result; }