Multi-instance Modules
Some modules — particularly gRPC clients and database connections — are often needed in multiple configurations within a single service. Lakta supports this via NamedModule.
How it works
Section titled “How it works”A NamedModule registers its DI providers under its name rather than the type alone. This lets you have, say, two gRPC client connections with different targets.
Using WithName
Section titled “Using WithName”Every built-in module that supports multi-instance ships a WithName option:
lakta.NewRuntime( grpcclient.NewModule(grpcclient.WithName("payments")), grpcclient.NewModule(grpcclient.WithName("notifications")),)Config for each instance lives under its name:
modules: grpc: client: payments: target: "payments-svc:50051" notifications: target: "notifications-svc:50051"Retrieving named instances
Section titled “Retrieving named instances”Use do.MustInvokeNamed with the instance name:
paymentsConn := do.MustInvokeNamed[*grpc.ClientConn](injector, "payments")notifsConn := do.MustInvokeNamed[*grpc.ClientConn](injector, "notifications")Implementing NamedBase in custom modules
Section titled “Implementing NamedBase in custom modules”type MyModule struct { lakta.NamedBase}
func NewMyModule(name string) *MyModule { m := &MyModule{} m.NamedBase = lakta.NewNamedBase(name) return m}NamedBase satisfies the NamedModule interface and stores the name for use in ConfigPath() and DI registration.