Publishing events to another Bounded Context
Hi,
I have started testing Event Flow in our application and I would love to get your ideas on the following challenge:
I created a generic Event Bus that multiple Bounded Contexts (in DDD terms) use to communicate with each other.
The Event Bus is based on the Publisher/Subscriber pattern where one Bounded Context publish an Event to a "topic" and other Bounded Contexts can subscribe to the "topic". At this point, everything works great.
However, when an Event is received by the "Subscriber", I would like to apply that Event to its ReadModels. I would you implement this part knowing that all I have is the Event Data in JSON format?
Cheers, J-F
When serialising events to JSON you can use the built-in functionality in EventFlow IEventJsonSerializer. It makes it considerably easier. When deserialising you might want to run the IEventUpgradeManager to make sure every single event is upgraded to the latest version (if you use the versioning feature).
Publishing domain events to read stores manually with EventFlow is fairly easy, merely invoke all implementations of IReadStoreManager as seen in the DomainEventPublisher source code (https://github.com/eventflow/EventFlow/blob/develop/Source/EventFlow/Subscribers/DomainEventPublisher.cs).
For the event bus itself, have a look at the EventFlow.RabbitMQ project in the source code to get some inspiration as publishing to a external queue based on topics is what that does.
Hope that answers your question.
Thanks for the details rasmus... however I already have the Publish part completed :) I am currently able to publish Commands all the way to publish my Events to the Event Bus but I am struggling with the Subscriber of the event applying the event to its read models.
Let's say I have an Customer Bounded Context and an Invoice Bounded Context. If we follow the DDD design pattern, each bounded context are totally isolated from each other and only communicate through messages (using an Event Bus like RabbitMQ).
For example, the Customer BC will publish an CustomerNameChangedEvent and the Invoice BC subscribes to the event and will receive it through the Event Bus.
However, when the Invoice BC receives the CustomerNameChangedEvent, how do I take that event and apply to the Invoice ReadModels?
Let's remember that the CustomerNameChangedEvent I just received, contains the AggregateId => CustomerId, not the AggregateId => InvoiceId so I cannot easily pass that CustomerNameChangedEvent along to the Invoice BC's DomainEventPublisher...
There needs to be a way to transform the CustomerNameChangedEvent into the Invoice BC language so we can easily apply events inside a separate bounded context... this is a pretty standard use case.
Cheers, J-F
Great, I hear you, didn't get the part about different bounded contexts wan't to use events from other bounded contexts 😄
First, when you don't use the same IDs for the read models and aggregates, EventFlow uses the IReadModelLocator which can be specified while registering the read model. It only has a single method IEnumerable<string> GetReadModelIds(IDomainEvent domainEvent) which should map the incoming domain event to the correct read model ID. How you do the mapping is specific for your domain, but a common use case is to have the related IDs either as part of the domain event (if its part of the domain) or as part of the meta data. The IReadModelLocator implementation would then simply return the Invoice ID on the domain event, read it from meta data or maybe even use the original aggregate ID to do a lookup.
You might want to map the event from the foreign bounded context to a local type.
As a side note, it does seem like the IDomainEventPublisher can do with a cleanup, as PublishAsync requires several pieces of information that it really doesn't need, e.g. the aggregate and identity type as generics and the aggregate ID. I'll create another method that doesn't have these requirements which should make it easier for you to integrate outside domain events into your bounded context.
Providing an overload for PublishAsync() sounds like a great plan and will gladly make use of it.
I was also playing with the idea of transforming the Events received by the Subscribing Bounded Context into a matching Command and publish it through its Command Bus? What do you think?
High level flow:
- BC1 publishes an Event to Event Bus
- BC2 is notified of the Event
- BC2 creates a new Command using the received Event's data
- BC2 publishes the new Command to its internal Command Bus
- BC2 runs the Command through its Aggregate's Load, Validation and Update functions
- BC2 emits a new Event and commits it to its internal Event Store
- BC2 refreshes its Read Models
Alternate High level flow:
- BC1 publishes an Event to Event Bus
- BC2 is notified of the Event
- BC2 commits the Event to its internal Event Store (skips validation, etc...)
- BC2 refreshes its Read Models
This would have the advantage of being able to "replay" the newly generated Events within BC2 to help refresh the ReadModels.
What are your thoughts?
It sounds like your focus is on how the technical/coding can be done. Maybe focus on how the domain should be modeled. For that you might want to introduce the concept of domain services as a bridge between the events from the external context to the internal one (http://domainlanguage.com/wp-content/uploads/2016/05/DDD_Reference_2015-03.pdf).
So, as an example we have two bounded contexts invoicing (BI) and customers (BC), then BC might want to list the invoices for each customer.
- BI: invoice is created
- BI: emits event
- BC: receives event, but as this from an outside bounded context it doesn't really map directly into BC domain, it might and often do, but its still a event from a "foreign" domain and might contain concepts that either doesn't make sense, use different words or even use the same words for something already present in BC
- BC: maps the BI event into something that makes sense in BC. It might be as simple as taking an event an calling a domain service (which then again publishes one or more commands), or you might create a anti corruption layer that has the sole responsibility of mapping BI <-> BC concepts. I think the complexity of this step largely depends on the size of our code base and how much the two different domains differ
An alternative might also to keep the fact that the outside bounded context uses events and DDD isolated from your internal domain. Again some sort of anti corruption layer, a layer that has the responsibility of maintaining an internal representation of the outside context. In this case simple updates to e,g. MSSQL rows based on events might be sufficient. The internal read models in MSSQL could then be served through a service that maps them to nice entities that the core domain could use.
I'll try to sum it up a bit
- Events from other bounded context might need an anti corruption layer to make sense in a domain
- Don't force everything through EventFlow
- Keep it simple, if possible
Please note that the above is very opinionated and is merely one of many ways of implementing it.
Closed as there hasn't been activity for more than a month on this question