LoginSignup
14
10

More than 1 year has passed since last update.

[JavaScript] Dependency Injectionコンテナどれにしよう?

Last updated at Posted at 2018-10-07

Azure FunctionsをJavaScriptで書いていました。
テストを書くにあたり、やっぱり依存先クラスを自前でnewしてるのは辛いなと思い、DIコンテナを探すことにしました。

awilix

こちら nodejs, ES6 で dependency injection with awilix で紹介されていました。

コンテナを作り

const container = awilix.createContainer({
  injectionMode: awilix.InjectionMode.PROXY
})

コンテナにクラスを登録し

container.register({
  userController: awilix.asClass(UserController)
})

コンテナからインスタンスを取り出して使います。

var userController = container.resolve('userController');

簡単です。

依存関係を直接書かなくても解決してくれるのかな?いいですね。

InversifyJS

こちら InversifyJSで学ぶDI(Dependency Injection) で紹介されていました。

クラスを修飾し

inversify.decorate(inversify.injectable(), Katana);
inversify.decorate(inversify.injectable(), Shuriken);
inversify.decorate(inversify.injectable(), Ninja);

依存関係を登録し(引数の0や1は、多分コンストラクタ引数の順番)

inversify.decorate(inversify.inject("Katana"), Ninja, 0);
inversify.decorate(inversify.inject("Shuriken"), Ninja, 1);

コンテナを作成し

var container = new inversify.Container();

型名とクラスを紐付け

container.bind("Ninja").to(Ninja);
container.bind("Katana").to(Katana);
container.bind("Shuriken").to(Shuriken);

コンテナからインスタンスを取り出して使います。

var ninja = container.get("Ninja");

だいぶ冗長ですね。安全のために仕方ないみたいです。
TypeScriptだとデコレータがあるのでよい感じです。

bottlejs

こちら bottlejsでDIを試す で紹介されていました。

コンテナを作成し

var bottle = new Bottle();

コンテナに型名とクラスを登録し

bottle.service('Barley', Barley);
bottle.service('Hops', Hops);
bottle.service('Water', Water);

コンテナに依存関係を登録し

bottle.service('Beer', Beer, 'Barley', 'Hops', 'Water');

コンテナからインスタンスを取り出して使います。

var beer = bottle.container.Beer

シンプルですね。
コードサンプルがいい(笑)

その他

npmjs探すといっぱいありますね。

結論

DIといいながら、単にクラスへの直接依存からコンテナへの依存に切り替わっているだけなのは、JavaScript故に仕方がないことかしらん。

それなりのチームであればinversifyJS、少人数and/of小規模ならawilixかbottlejsがよさそうです。

ところで、探してはみたけれど、Azure FunctionsでDIコンテナ使うのはごしたい感じ。

14
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
10