Transactional DI: Adapter and Handler Optimization
Explore how to enhance transactional messaging in Golang by updating driver adapters, gRPC server methods, and event handlers to use scoped dependency injection containers. Understand how to create transactional boundaries for requests, improve handler efficiency, and maintain application functionality while introducing atomic transactions.
Driver adapters
The driver adapters had been using the various variables we had created in the first two sections, but those variables no longer exist. Every driver needs to be updated to accept the container instead.
We will leave the existing driver functions alone and will create new functions that take the container instead. For example, the RegisterDomainEventHandlers() function will be replaced with a new function with the following signature:
func RegisterDomainEventHandlersTx(container di.Container)
The gRPC server and the three handlers will each need to be updated to use the container and start a new scoped request.
Updating the gRPC server
The new function to register our gRPC server will have the following signature, which swaps out the application.App parameter for the container:
func Register(container di.Container,registrar grpc.ServiceRegistrar,) error
This function will create a new server called serverTx that is built with the container instead of the application instance. Like the existing server, it will implement depotpb.DepotServiceServer, but it will proxy all calls into instances ...