Let’s say I have two bounded contexts in my system, responsible for Products and Orders respectively.
In the Products context, I have services responsible for aggregates that listen to all events on product streams and keep a representation up to date, and can give current info about it. The same in the Orders context. Both separate and working well.
Then I want to make a read side service, let’s call it CartViewService that is able to give data about a not yet finished order with product information embedded, like { items: [{ product: 123, image: 'http://a.cdn/345.jpeg' }]}. That is a projection of projections from two different contexts.
My design thought here is that only the Orders context service should know about all the event types that can appear on an Order, and likewise for the Products context. So they would each make their projections (in service code, not in EventStore projection code, for reasons outside of this topic…). And the CartViewService, not knowing all event types, would just need to get notified on NewVersion of the Orders and Products in that order.
I’m hesitant of the best approach here. One solution I see would be that the aggregate services, after receiving a domain event (like new-product-photo), would publish an event on a PublicProduct-<uuid> stream with the event type NewVersion and the full projection of the product. That event could just go to a pub/sub message broker as well as EventStore.
Is this reasonable, or am I missing something obvious? I do NOT want the readside to need to know about all event types of products and orders or share code between these contexts.
Not sure if this is much help, but we have a similar system (product, orders and sales) and we have a single Projection that listens to $ce-Products, $ce-Orders and $ce-Sales. We keep track of what is happening with a Product (has it been ordered, delivered, stock changes) and emit a new event to a stream that our read model subscribes to.
So you use a design where the read model doesn’t have to know anything about the granular domain events, but can just get new versions of the full model whenever updated?
@fredrik, for sure, exposing all the granular events to other services is not an approach that I’d recommend. This ends up with leaking abstraction and making changes harder. I recommend defining what’s needed to be published from a module in the global context. So think from 2 perspectives: specific module and system-wide.
For example, the payment module doesn’t have to know all the details of completing the order (e.g. adding, removing items). It’s interested only in the confirmed order. If the payment module needs to gather all of the information from the granular events, each change to this process could potentially impact payment processing.
Thus, it’s good to enrich and transform the granular internal events into external information. It doesn’t have to be just a state snapshot, best if it’s an event that gathers specific business information. Still, pragmatically if we’re just synchronising data in read models, then what @steven.blair suggested may be, of course, a good option.