angular / di.js

Dependency Injection Framework for the future generations...
Other
818 stars 95 forks source link

Non-module constructor parameter #90

Open djindjic opened 9 years ago

djindjic commented 9 years ago

Is possible (and is it relevant question for this repo issues) to have some additional constructor parameter which is not injected module itself? For example coffee_type:

import {Inject} from 'di';
import {Pump} from './pump';

@Inject(Pump)
export class CoffeeMaker {
  constructor(pump, coffee_type) {
    this.pump = pump;
    this.coffee_type = coffee_type;
  }
}

let injector = new Injector();
let coffee_maker = injector.get(CoffeeMaker>>>>>('cappuccino')<<<<<<);
L8D commented 9 years ago

Though this feature is possible and convenient, the workaround (and possibly better solution) is to supply these extra parameters through provided dependencies. For example:

import {Inject, Provide} from 'di';
import {Pump} from './pump';

class CoffeeType {}

@Inject(Pump, CoffeeType)
export class CoffeeMaker {
  constructor(pump, coffeeType) {
    this.pump = pump;
    this.coffeeType = coffeeType;
  }
}

@Provide(CoffeeType)
function Cappuccino() {
  return 'cappuccino';
}

let injector = new Injector();

let withCappucino = injector.createChild([Cappuccino]);
let coffeeMaker = withCappucino.get(CoffeeMaker);