Skip to content

Integration Event Converter

The integration event converter is a tool for converting domain events into integration events.

With the integration event converter, you can decouple the sending of integration events from the business logic in other domain event handlers.

Define Integration Event Converter

  1. Install the nuget package NetCorePal.Extensions.DistributedTransactions.Abstractions
dotnet add package NetCorePal.Extensions.DistributedTransactions.Abstractions
  1. Define the integration event converter, which requires:

  2. Inherit the NetCorePal.Extensions.DistributedTransactions.IIntegrationEventConverter interface;

  3. Implement the Convert method of the IIntegrationEventConverter interface, which takes a domain event IDomainEvent as a parameter and returns an integration event IntegrationEvent.

​ Here is an example:

// Define integration event converter
using NetCorePal.Web.Application.IntegrationConvert;
namespace YourNamespace;

public class OrderCreatedIntegrationEventConverter 
    : IIntegrationEventConverter<OrderCreatedDomainEvent, OrderCreatedIntegrationEvent>
{
    public OrderCreatedIntegrationEvent Convert(OrderCreatedDomainEvent domainEvent)
    {
        return new OrderCreatedIntegrationEvent(domainEvent.Order.Id);
    }
}
  1. The framework comes with a code generator to automatically generate integration event handlers.

Here is an example of an integration event handler generated by the code generator:

```csharp // using NetCorePal.Web.Application.IntegrationConverters; using NetCorePal.Extensions.DistributedTransactions; using NetCorePal.Extensions.Domain; using NetCorePal.Web; namespace YourNamespace; { ///

/// OrderCreatedIntegrationEventConverterDomainEventHandlers /// public class OrderCreatedIntegrationEventConverterDomainEventHandler(IIntegrationEventPublisher integrationEventPublisher, OrderCreatedIntegrationEventConverter converter) : IDomainEventHandler { /// /// OrderCreatedIntegrationEventConverterDomainEventHandler /// /// notification /// cancellationToken public async Task Handle(OrderCreatedDomainEvent notification, CancellationToken cancellationToken){ // Convert and publish integration event var integrationEvent = converter.Convert(notification); await integrationEventPublisher.PublishAsync(integrationEvent, cancellationToken); }

}

}