I utilize Inversify for object binding in the following manner:
container.applyMiddleware(loggerMiddleware);
let module = new ContainerModule((bind: interfaces.Bind) => {
bind<Logger>(TYPES.Logger).toConstantValue(logger);
bind<ILoggerFactory>(TYPES.ILoggerFactory).to(WinstonLoggerFactory);
bind<string>(TYPES.ConnectionString).toConstantValue(cs);
bind<{}>(TYPES.ConnectionOptions).toConstantValue({});
bind<ITradeController>(TYPES.ITradeController).to(TradeController);
bind<ITradeService>(TYPES.ITradeService).to(TradeService).whenInjectedInto(TradeController);
bind<ITradeRepository>(TYPES.ITradeRepository)
.to(TradeMongoRepository)
.whenInjectedInto(TradeService);
bind<IUserService>(TYPES.IUserService).to(UserService).whenInjectedInto(TradeService);
bind<IUserRepository>(TYPES.IUserRepository)
.to(UserMongoRepository)
.whenInjectedInto(UserService);
});
container.load(module);
Furthermore, I have defined an Inversify middleware as shown below:
import * as inversify from 'inversify';
function logger(next: inversify.interfaces.Next): inversify.interfaces.Next {
return (args: inversify.interfaces.NextArgs) => {
let start = new Date().getTime();
let result = next(args);
let end = new Date().getTime();
console.log(`wooooo ${end - start}`);
return result;
};
}
export default logger;
Upon running the application, only a single message 'wooooo' is displayed.
For instance, if services are invoked using the code snippet below:
const tradeController = container.get<ITradeController>(TYPES.ITradeController);
const result = await tradeController.findByUserId(params.userId);
When the `tradeController` calls `tradeService`, and subsequently the `tradeService` calls `tradeRepository`, each level is expected to log 'wooooo', but only one log entry is recorded. What could be causing this issue?