If Aurelia understands "import", why use dependency injection? -
i don't understand.. if can use import
in aurelia, why have wire constructor @autoinject()
, that? i'm sure i'm missing something, but, can tell, can use imported module whenever want.
import "whatever" export class someviewmodel { activate() { // use } }
typically, in aurelia application, thing import
ing isn't instance of something
it's class something
. use whatever has been import
ed, need instance of it.
import 'whatever'; let = new something();
when use aurelia's dependency injection system, utilizing design pattern called "inversion of control." instead of class (or you) being in charge of instantiating dependencies, lists dependencies has , has instances of dependencies injected in constructor function.
this helps testability, can pass mocked instances of dependencies class in test fixtures (note in tests, tests pass mocks constructor, , not rely on aurelia's di container).this allows tap in dependency injection container's ability configured create dependencies using different object lifestyles such singletons , transient.
--- edits answer op's questions comments ---
if import module defined export default class aurelia view model using constructor injection, not need instantiated. instance of class something.
this because aurelia's dependency injection container instantiating instance you. why code looks this:
import {inject} 'aurelia-framework'; import 'somewhere'; @inject(something) export class foo { constructor(something) { this.something = something; } //... }
and not
import 'somewhere'; export class foo { constructor(something) { this.something = something; } //... }
you telling aurelia "i need 1 of these, please give me," , aurelia says "sure thing, i've created 1 or had 1 lying around, here is."
in other words, appears aurelia's constructor di works class exports, , instantiate class. looks if want import moment js aurelia view model, should continue doing things way i've done them (not using aurelia's di). sound correct?
this correct. libraries moment
give function use, , not class can instantiated aurelia. these continue use them in past.
Comments
Post a Comment