cqrs icon indicating copy to clipboard operation
cqrs copied to clipboard

Query trait doesn't need to know about Aggregates

Open danieleades opened this issue 1 year ago • 1 comments

the Query trait has this signature:

#[async_trait]
pub trait Query<A: Aggregate>: Send + Sync {
    /// Events will be dispatched here immediately after being committed.
    async fn dispatch(&self, aggregate_id: &str, events: &[EventEnvelope<A>]);
}

the EventEnvelope has this signature:

#[derive(Debug)]
pub struct EventEnvelope<A>
where
    A: Aggregate,
{
    /// The id of the aggregate instance.
    pub aggregate_id: String,
    /// The sequence number for an aggregate instance.
    pub sequence: usize,
    /// The event payload with all business information.
    pub payload: A::Event,
    /// Additional metadata for use in auditing, logging or debugging purposes.
    pub metadata: HashMap<String, String>,
}

i can't see any reason why either the Query trait or the EventEnvelope struct should need to know what an Aggregate is, since they're only concerned with events.

I would suggest changing their signatures to-

#[async_trait]
pub trait Query<E: Event>: Send + Sync {
    /// Events will be dispatched here immediately after being committed.
    async fn dispatch(&self, aggregate_id: &str, events: &[EventEnvelope<E>]);
}

#[derive(Debug)]
pub struct EventEnvelope<E>
{
    /// The id of the aggregate instance.
    pub aggregate_id: String,
    /// The sequence number for an aggregate instance.
    pub sequence: usize,
    /// The event payload with all business information.
    pub payload: E,
    /// Additional metadata for use in auditing, logging or debugging purposes.
    pub metadata: HashMap<String, String>,
}

danieleades avatar Jan 03 '25 11:01 danieleades

That seems reasonable, but I'm curious as to what the downstream effects will be on the persistence modules. I'm guessing it was originally base on the Aggregate because it made those libraries simpler. That may or may not still be the case.

serverlesstechnology avatar Jan 25 '25 16:01 serverlesstechnology