...

/

Domain Events Optimization: Handling Efficiency

Domain Events Optimization: Handling Efficiency

Learn how to efficiently maintain handlers and dispatch events for managing system changes and integrating domain events to enhance application reactivity.

Simplifying handler maintenance and EventDispatcher

Consider for a moment a larger application with multiple handlers and a greater number of events with an equal number of methods defined in DomainEventHandlers. Keeping each handler up to date would be tedious. We need a solution that will help us avoid creating empty and unused methods when new domain events have been added:

type ignoreUnimplementedDomainEvents struct{}
var _ DomainEventHandlers = (*ignoreUnimplementedDomainEvents)(nil)
func (ignoreUnimplementedDomainEvents) OnOrderCreated( ... )
error { ... }
func (ignoreUnimplementedDomainEvents) OnOrderReadied( ... )
error { ... }
func (ignoreUnimplementedDomainEvents) OnOrderCanceled( ... )
error { ... }
func (ignoreUnimplementedDomainEvents) OnOrderCompleted( ... )
error { ... }
Multiple methods defined in DomainEventHandlers

Because of the interface check, if DomainEventHandlers is changed when a new event is added, we will be alerted that ignoreUnimplementedDomainEvents is no longer in sync with those changes when we try to compile. We will avoid writing unused methods to keep up with the changes to DomainEventHandlers by including ignoreUnimplementedDomainEvents as an embedded field in our handlers:

Press + to interact
type NotificationHandlers struct {
notifications domain.NotificationRepository
ignoreUnimplementedDomainEvents
}

The last new component to build is ... ...