Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the 'View' trait #116

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

danieleades
Copy link
Contributor

@danieleades danieleades commented Jan 2, 2025

aligns the 'View' trait and the 'Aggregate' trait more closely.

the View trait seems to be a synonym for a projection. An Aggregate is really a special case of a projection (that also handles commands), so it makes sense for these to be aligned.

I would like to take this further and have the Aggregate trait 'inherit' the View trait, but there are a couple of inconsistencies

  1. the Aggregate events must implement DomainEvent, which supports the upcasting framework in this library, but the View event does not need to implement DomainEvent and hence doesn't support upcasting. What's the reason for this inconsistency?
  2. the View does not have a 'type' String, does this mean view snapshots are not persisted, or are persisted using a different mechanism to aggregates?
  3. i couldn't see any good reason why the Aggregate ingests Events and the View ingests wrapped EventEnvelopes. By design, the View should not need any additional metadata to reconstitute the state from the events, so i'm not sure this makes sense

i think this PR stands up even without resolving the above questions, but if they can be resolved than the Aggregate trait can be changed from

pub trait Aggregate: Default + Serialize + DeserializeOwned + Sync + Send {
    /// Specifies the inbound command used to make changes in the state of the Aggregate.
    type Command;
    /// Specifies the published events representing some change in state of the Aggregate.
    type Event: DomainEvent;
    /// The error returned when a command fails due to business logic.
    /// This is used to provide feedback to the user as to the nature of why the command was refused.
    type Error: std::error::Error;
    /// The external services required for the logic within the Aggregate
    type Services: Send + Sync;
   /// ...
}

to

pub trait Aggregate: View {
    /// Specifies the inbound command used to make changes in the state of the Aggregate.
    type Command;
    /// The error returned when a command fails due to business logic.
    /// This is used to provide feedback to the user as to the nature of why the command was refused.
    type Error: std::error::Error;
    /// The external services required for the logic within the Aggregate
    type Services: Send + Sync;
   /// ...
}

@@ -23,8 +23,10 @@ pub trait Query<A: Aggregate>: 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<A: Aggregate>: Debug + Default + Serialize + DeserializeOwned + Send + Sync {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a View shouldn't even know what an Aggregate is, it should only care about (and know about) events.

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

Attention: Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 87.35%. Comparing base (3615030) to head (1ba843b).

Files with missing lines Patch % Lines
persistence/postgres-es/src/testing.rs 0.00% 2 Missing ⚠️
src/persist/doc.rs 0.00% 1 Missing ⚠️
src/persist/generic_query.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #116   +/-   ##
=======================================
  Coverage   87.35%   87.35%           
=======================================
  Files          32       32           
  Lines        3376     3376           
=======================================
  Hits         2949     2949           
  Misses        427      427           
Flag Coverage Δ
cqrs 84.43% <0.00%> (ø)
mysql 88.69% <100.00%> (ø)
postgres 65.58% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@danieleades
Copy link
Contributor Author

@davegarred any thoughts about the questions in the summary of this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant