Closed mkq closed 1 month ago
@autoInjectable
.@mkq What happens if HighLevelService
is registered with a lifecycle of Lifecycle.ContainerScoped
rather than as a singleton?
How dumb of me! ContainerScoped fixes it. Thanks for the quick response!
Describe the bug I register different basic services in different child containers of the root container. Then I resolve dependent
@injectable
services from each child container. I assume each of those should then be automatically registered in the corresponding child container, so it should also use the basic service from the corresponding child container.Our use case: When we serve a request given some business entity id, we must detect the company by that id and make all requests to some other APIs to the base URL for the company. I.e. the same APIs are available for company A and B, only the base URLs differ. I thought the cleanest solution would be to put the (OpenAPI-generated axios based) API clients in two different containers and also have separate instances of each dependent service in the corresponding container. Then, I would only need to pick the right child container at the start of the request (by given business entity id), resolve the high-level services from it, and nowhere else do something like "if id matches …, use company A basic service, else company B basic service". The basic API clients require some programmatic setup, so I register them explicitely (
registerInstance
), while high-level services are@injectable() @singleton()
.To Reproduce
Expected behavior Test should pass, indicating that the HighLevelService resolved from childContainerB uses the
new BasicService('B')
registered in childContainerB.Version: 4.8.0