Assisted injection in Aurelia?

前端 未结 2 1935
春和景丽
春和景丽 2020-12-24 08:50

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

@injec         


        
2条回答
  •  隐瞒了意图╮
    2020-12-24 09: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);
      }
    }
    

提交回复
热议问题