#cqrs #query #container #command #provider #ioc #async

kti_cqrs_provider_rs

CQRS provider with ioc container

4 releases (breaking)

0.3.0 Dec 8, 2024
0.2.0 Mar 17, 2024
0.1.0 Mar 3, 2024
0.0.1 Nov 5, 2023

#982 in Algorithms

Download history 73/week @ 2024-08-26 9/week @ 2024-09-02 50/week @ 2024-09-09 7/week @ 2024-09-16 44/week @ 2024-09-23 23/week @ 2024-10-07 31/week @ 2024-10-14 10/week @ 2024-10-21 83/week @ 2024-12-02 88/week @ 2024-12-09

171 downloads per month

MIT/Apache

11KB
242 lines

CQRS provider with ioc container

Wrapped kti_cqrs_rs via provider for more complex usage

Simple example (existed in repo)

pub struct UserController {
  context: Arc<dyn ContextPort>,
}

#[async_trait]
impl AdapterPort<UserController> for UserController {
  fn token() -> &'static str {
    "UserController"
  }

  async fn get_adapter(context: &Arc<dyn ContextPort>) -> Result<Box<Self>, Error> {
    let me = context
      .resolve_provider(Self::token())
      .await?
      .downcast::<Self>()
      .map_err(|_| format!("Cant resolve provider: {}", Self::token()))?;

    Ok(me)
  }
}

impl UserController {
  pub fn new(context: Arc<dyn ContextPort>) -> Self {
    Self { context }
  }

  pub async fn get_user_by_name(&self, name: &str) -> Result<Option<User>, Error> {
    let bus = CqrsProvider::get_adapter(&self.context).await?;

    let query = GetUserByNameQuery::new(name);

    bus.query(Box::new(query)).await
  }

  pub async fn create_user(&self, name: &str, email: &str) -> Result<(), Error> {
    let bus = CqrsProvider::get_adapter(&self.context).await?;

    let command = CreateUserCommand::new(name, email);

    bus.command(Box::new(command)).await?;

    Ok(())
  }

  pub async fn create_safe_user(&self, name: &str, email: &str) -> Result<(), Error> {
    let bus = CqrsProvider::get_adapter(&self.context).await?;

    let command = CreateSafeUserCommand::new(name, email);

    bus.command(Box::new(command)).await?;

    Ok(())
  }

  pub async fn update_user_email(&self, name: &str, email: &str) -> Result<(), Error> {
    let bus = CqrsProvider::get_adapter(&self.context).await?;

    let command = UpdateUserCommand::new(name, email);

    bus.command(Box::new(command)).await?;

    Ok(())
  }

  pub async fn update_user_name(&self, current_name: &str, new_name: &str) -> Result<(), Error> {
    let bus = CqrsProvider::get_adapter(&self.context).await?;

    let event = RenameUserEvent::new(current_name, new_name);

    bus.event(Box::new(event)).await?;

    Ok(())
  }
}

Dependencies

~2.2–8MB
~63K SLoC