...

/

Transactional DI: Adapter and Handler Optimization

Transactional DI: Adapter and Handler Optimization

Look into a module to switch its composition root over to using the DI containers.

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 RegisterDomainEventHandlersTx() function

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
Registering our gRPC server

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 ...