-
Notifications
You must be signed in to change notification settings - Fork 4
Create Event Bus
Creating a SeekU Event Bus
Creating a SeekU event bus is as simple as implementing an interface.
The required interface is SeekU.Eventing.IEventBus
That interface has two methods:
- PublishEvent
- PublishEvents
Most often you'll be publishing a single event. However there are times when it's necessary to publish a series of events as a single unit. The interface supports both scenarios.
In either case, your implementation is responsible for publishing one or more instances of a DomainEvent object.
In order to create event the most complex implementation, all that is necessary is to take a domain event object instance and send it to the appropriate event handlers. This is true even in complex scenarios like using NServiceBus.
For example, you might use RabbitMQ as your event bus:
public class RabbitMQEventBus : IEventBus
{
void PublishEvent(DomainEvent domainEvent)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("hello", false, false, false, null);
// serialize your event however you prefer
var body = {SomeSerializer}.Serialize(domainEvent)
channel.BasicPublish("", "hello", null, body);
}
}
void PublishEvent(IEnumerable<DomainEvent> domainEvents)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("hello", false, false, false, null);
// serialize your event however you prefer
var body = {SomeSerializer}.Serialize(domainEvents)
channel.BasicPublish("", "hello", null, body);
}
}
}