15

I have a class whose constructor has two arguments; one is a dependency, the other is a configuration property:

@inject(Dependency)
class MyClass{
  constructor(dependency, config){
  }
}

How can I make use of Aurelia's dependency injection to automatically inject the dependency but allow the class's consumer to specify the config value?

Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
Gorse horse
  • 5,093
  • 1
  • 20
  • 14

2 Answers2

19

Here are some options:


Option 0: Factory Resolver

foo.js

import {inject} from 'aurelia-framework';
import {FooDependency} from './foo-dependency';

@inject(FooDependency)
export class Foo {
  constructor(dep, config) {
  }
}

needs-foo.js

import {inject, Factory} from 'aurelia-framework';
import {Foo} from './foo';

@inject(Factory.of(Foo))
export class NeedsFoo {
  constructor(fooFactory) {
    let config = {};
    this.foo = fooFactory(config);
  }
}

Option 1: Factory

foo.js

import {inject} from 'aurelia-framework';
import {FooDependency} from './foo-dependency';

class Foo {
  constructor(dep, config) {
  }
}

@inject(FooDependency)
export class FooFactory {
  constructor(dep) {
    this.dep = dep;
  }

  makeFoo(config) {
    return new Foo(this.dep, config);
  }
}

needs-foo.js

import {inject} from 'aurelia-framework';
import {FooFactory} from './foo';

@inject(FooFactory)
export class NeedsFoo {
  constructor(fooFactory) {
    let config = {};
    this.foo = fooFactory.makeFoo(config);
  }
}

Option 2: Child Container

foo.js

import {inject} from 'aurelia-framework';
import {FooDependency} from './foo-dependency';

export const configKey = 'config';

@inject(FooDependency, configKey)
export class Foo {
  constructor(dep, config) {
  }
}

needs-foo.js

import {inject, Container} from 'aurelia-framework';
import {Foo, configKey} from './foo';

@inject(Container)
export class NeedsFoo {
  constructor(container) {
    let config = {};
    let childContainer = container.createChild();
    childContainer.registerInstance(configKey, config);
    this.foo = childContainer.get(Foo);
  }
}

Option 3: Brute Force

foo.js

export class Foo {
  constructor(dep, config) {
  }
}

needs-foo.js

import {inject, Container} from 'aurelia-framework';
import {FooDependency} from './foo-dependency';
import {Foo} from './foo';

@inject(Container)
export class NeedsFoo {
  constructor(container) {
    let config = {};
    let dep = container.get(FooDependency);
    this.foo = new Foo(dep, config);
  }
}
Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
2

In the end I created a custom resolver, which means the code is nice and modular and easy to use in other classes.

foo.js

import {inject} from 'aurelia-framework';
import {FooDependency} from './foo-dependency';

@inject(Dependency)
export class Foo{
  constructor(dep, cfg){}

  static useArgs(...args){
     return new Resolver(Foo, args);
  }
}

@resolver
class Resolver{
  constructor(Class, args){
    this.Class = Class;
    this.args = args;
  }

  get(container){
    var Class = this.Class,
      // Class.inject is the array of the class's dependencies
      // we need to resolve them all before they're useful
      dependencies = Class.inject.map((dep)=>container.get(dep)),
      // Pass the resolved dependencies + any additional arguments to the new class
      args = dependencies.concat(this.args);

    return new Class(...args);
  }
}

needs-foo.js

import {inject} from 'aurelia-framework';
import {Foo} from 'foo';

@inject(Foo.useArgs('my config'))
export class NeedsFoo{
    constructor(fooConfigured){
    }
}
Gorse horse
  • 5,093
  • 1
  • 20
  • 14
  • 3
    oh man- I thought you needed to set the config dynamically, each time a `Foo` is instantiated. It looks like for your needs, none of what we did is needed. All you need to do is register your config in the container: `container.registerInstance('config key', myConfig);` then add `@inject(Dependency, 'config key')` to `Foo`. – Jeremy Danyow Jan 15 '16 at 12:34
  • @JeremyDanyow No you were right the first time, set dynamically each time. This way allows me to call `Foo.useArgs()` with a different value each time I want to use Foo, because it returns a new instance of `Resolver` each time. – Gorse horse Jan 15 '16 at 13:48