Skip to content

Conversation

adenhertog
Copy link

The IoC integration lacks the request context when resolving instances of a controller. This context is very useful for many IoC integrations that manage their lifecycle on a per-request basis.

As an example, if the consumer wants to bind the request IP address in a child container so that it is available in a request scope, then they'll need to do this work just before the controller gets created. For example:

import { IoCAdapter } from 'routing-controllers'
import { Container } from 'inversify'

class InversifyAdapter implements IocAdapter {
  constructor (
    private readonly container: Container
  ) {
  }

  get<T> (someClass: ClassConstructor<T>, action?: Action): T {
    const childContainer = this.container.createChild()
    childContainer.bind(API_SYMBOLS.RequestorIp).toConstantValuetion.context.ip)
    return childContainer.resolve<T>(someClass)
  } 
}

Here, the resolved controller and all of its dependencies will have access to the IP of the current request scope.

The action has been added as an optional second argument to the get() callback so that it's backwards compatible with existing code.

@adenhertog adenhertog merged commit a4e108b into master Jun 23, 2019
@adenhertog adenhertog deleted the ioc-context branch June 23, 2019 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant