You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been looking for a way to define two implementations for the same service at the same time (For example, SendMailService) so that one of them injects the Logger implementation FakeLogger and another of them the implementation RealLogger) with the intention of being able to take one or the other depending on the needs. However, I think we cannot build the container with the same service in duplicate (The error is that the service identified as 'SendMailService' has already been registered and you would need to "unregister it before you can register it again"
The best solution I have found would be the following:
// SendMailService.ts
@Service()exportclassSendMailService{publicconstructor(privatereadonlylogger: Logger){}}// Service Registerdiodcontainer.registerFactoryAs(c=>{returnnewSendMailService(env.realImplementation
? c.getDependency(RealLogger)
: c.getDependency(FakeLogger),);},SendMailService,DependencyScope.Transient,);// SomeController.ts
@Controller()exportclassSomeController{publicasyncdoStuffs(): Promise<void>{env.realImplementation=true;// or falseconstservice=container.get(SendMailService);awaitservice.send();}}
Do you think there could be a better solution or that the option of taking advantage of tags that allow a dual implementation to coexist could be introduced in the future?
Hi!
I have been looking for a way to define two implementations for the same service at the same time (For example,
SendMailService
) so that one of them injects theLogger
implementationFakeLogger
and another of them the implementationRealLogger
) with the intention of being able to take one or the other depending on the needs. However, I think we cannot build the container with the same service in duplicate (The error is that the service identified as 'SendMailService' has already been registered and you would need to "unregister it before you can register it again"The best solution I have found would be the following:
Do you think there could be a better solution or that the option of taking advantage of tags that allow a dual implementation to coexist could be introduced in the future?
Example:
The text was updated successfully, but these errors were encountered: