Understanding domain and integration events
Mia Lopez
I'm trying to understand a difference between domain and integration events in the following scenario.
I have two services: service A and service B.
Any event I want to track in the service A will be tracked using a domain event. All consumers for this domain event will be within service A boundary. This further means that domain events doesn't live in the Common or Shared project or Nuget package cause it will be used inside service A boundary.
If I want to integrate Service A and Service B I will use an integration event. This events are stored in the Shared project or Nuget package. For example: Service B will subscribe to the integration event issued from the Service A.
Would you consider this as valid or I'm missing something. Also, so far I saw on various projects that all events lives rather in Nuget package or in the Shared project, event though they are used within the service boundary or in the scenario where same event is used by consumer inside and outside service boundary.
What is the best practice in this case?
Should all events be divided as Domain and Integration Events?
1 Answer
Your understanding is mostly correct.
A Domain Event is something that would be 'raised' by your domain model, so the declaration of the domain event best sits as part of the domain model itself. Consider the following event:
public class WidgetCreatedEvent
{ public Widget CreatedWidget { get; } public WidgetCreatedEvent(Widget createdWidget) { CreatedWidget = createdWidget; }
}and the entity:
public class Widget
{ public string Name { get; private set; } public Widget(string name) { Name = name; AddDomainEvent(new WidgetCreatedEvent(this)); }
}You could choose to create a domain event that has primitive / value object properties instead of referencing the Widget class itself, but if you went for the above model, these would need to sit in the same assembly, otherwise you'd end up with a circular assembly reference.
An Integration Event would be declared in a shared project that Domain A and Domain B can both reference.
The integration event would be raised by your Domain A application layer instead of the domain layer. Similarly, the integration event would be consumed by your Domain B application layer and handled accordingly.