inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.33k stars 719 forks source link

Can't resolve es6 native types with dependencies #23

Closed orzarchi closed 9 years ago

orzarchi commented 9 years ago

Using node 4.0 and native ES6 classes:

class A{

}

class B{
  constructor(a){}
}

var kernel = new inversify.Kernel();
kernel.bind(new inversify.TypeBinding('a', A));
kernel.bind(new inversify.TypeBinding('b', B));
kernel.resolve('b');

results in Error: Could not resolve service class

remojansen commented 9 years ago

I'm have never used inversify with harmony mode before, I was able to reproduce the issue and I'm trying to find a way to fix it. Will give you more details soon.

remojansen commented 9 years ago

@orzarchi I have done a release (v.1.0.2) which resolves your issue:

Error: Could not resolve service class

However there is another issue that needs to be fixed in order to support ES6 native classes:

TypeError: Class constructors cannot be invoked without 'new'

I will fix it as soon as someone answers this SO question:

http://stackoverflow.com/questions/33193310/constr-applythis-args-in-es6-classes

Thanks for using inversify!

remojansen commented 9 years ago

Hi @orzarchi I'm happy to let you know that if you download the version v1.0.3 from npm you should be able to use ES6 classes with --harmony mode in node 4.0:

"use strict";

var inversify = require("inversify");

class A {
  constructor(){
    this.something = "A is doing something!";
  }
  doSomething(){
    console.log(this.something);
  }
}

class B {
  constructor(a) {
    this._a = a;
  }
  useA() {
    this._a.doSomething();
  }
}

var kernel = new inversify.Kernel();
kernel.bind(new inversify.TypeBinding('a', A));
kernel.bind(new inversify.TypeBinding('b', B));

var b = kernel.resolve('b');
b.useA();

I was able to run the file above using:

node index.js --harmony

Please let me know if you experience more issues.

orzarchi commented 9 years ago

Hi, thanks for adding this feature!

After poking around in your code, I think I have something that can help: I've found an npm module called node-introspect that seems to offer almost all the functionality needed to resolve argument names in functions and constructors.

I've made a pull request with some fixes ( https://github.com/kilianc/node-introspect/pull/4). Currently me and the author are working out the kinks. Since it seems like your argument resolve code is pretty similar, maybe we can all join forces, and you can reuse this code for Inversify?

On Sun, Oct 18, 2015 at 11:37 PM, Remo H. Jansen notifications@github.com wrote:

Hi @orzarchi https://github.com/orzarchi I'm happy to let you know that if you download the version v1.0.3 from npm you should be able to use ES6 classes with --harmony mode in node 4.0:

"use strict";

var inversify = require("inversify");

class A { constructor(){ this.something = "A is doing something!"; } doSomething(){ console.log(this.something); } }

class B { constructor(a) { this._a = a; } useA() { this._a.doSomething(); } }

var kernel = new inversify.Kernel(); kernel.bind(new inversify.TypeBinding('a', A)); kernel.bind(new inversify.TypeBinding('b', B));

var b = kernel.resolve('b'); b.useA();

I was able to run the file above using:

node index.js --harmony

Please let me know if you experience more issues.

— Reply to this email directly or view it on GitHub https://github.com/inversify/InversifyJS/issues/23#issuecomment-149045123 .