通过Express中间件传递Factory类实例

问题描述

我正在使用NodeJS / Express微服务,并尝试获取特定路由所需的所有必要类对象的实例。

我希望能够将依赖关系传递给我的每个中间件功能,但是我找不到通过中间件链传递依赖关系的最佳方法

我的路由器当前看起来像:

Exception error;
try
{
    Do();
}
catch (ArgumentNullException ex)
{
    // Safe the ex
    error = ex;
}
catch (Exception ex)
{
    // Safe the ex
    error = ex;
}
finally
{
    var isSucceed = error == null;
    if (isSucceed)
        _logger.Loginformation($"The process has been completed successfully");
    else
        _logger.LogError("The process faulted.",error);
}

factoryCreate是一个控制器函数,用于创建工厂实例:

ModelRoute.post(
  '/',ModelController.factoryCreate(),ModelController.validateData(),ModelController.sendData(),ModelController.sendResponse(),);

如您所见,我正在通过res.locals传递对象,但希望可以在控制器级别上做到这一点?

这是我的工厂班级:

  factoryCreate() {
    return (req: Request,res: Response,next: NextFunction) => {
      let factory = Container.get(AbstractFactory);
      res.locals.Class = factory.getClass();
      res.locals.Client = factory.getClient();
      res.locals.Response = factory.getResponse();
      return next();
    };
  }

任何更好地构造此代码方法将不胜感激。我正在使用typedi进行DI。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)