レベルエンター山本大のブログ

面白いプログラミング教育を若い人たちに

BLOCKVROCKリファレンス目次はこちら

[翻訳]Guice User's Guide ■10. Bootstrapping Your Application

独力で動くアプリケーション

The idea of bootstrapping is fundamental to dependency injection. Always explicitly asking the Injector for dependencies would be using Guice as a service locator, not a dependency injection framework.

「独力で動く(bootstrapping)」というアイデアは、Dependency Injectionの基本です。いつでもInjectorに依存性を明示的に問い合わせると、GuiceDependency Injectionフレームワークではなく、サービスロケーターとして使用されます。

Your code should deal directly with the Injector as little as possible. Instead, you want to bootstrap your application by injecting one root object. The container can further inject dependencies into the root object's dependencies, and so on recursively. In the end, your application should ideally have one class (if that many) which knows about the Injector, and every other class should expect to have dependencies injected.

開発者のコードは、Injectorを使って出来るだけ少しの直接処理とするべきです。その代わりに、アプリケーションを独力で動く状態にしたいのなら、ルートの一つのオブジェクトに注入します。コンテナは、ルートオブジェクトの奥のオブジェクトの依存性を再帰的に注入する事が出来ます。結局、アプリケーションには、Injectorを知っている1つのクラス(もしたくさんなら)が理想的です。そして他のあらゆるクラスが依存性を注入されると予想するでしょう。

For example, a web application framework such as Struts 2 bootstraps your application by injecting all of your actions. You might bootstrap a web service framework by injecting your service implementation classes.

たとえば、Struts 2のようなWebアプリケーションフレームワークでは、すべてのActionをインジェクトすることによって、アプリケーションが独力で動作するようになります。サービスを実装したクラスを注入することによって、Webサービスフレームワークを独力で動作させるようにもできるでしょう。

Dependency injection is viral. If you're refactoring an existing code base with a lot of static methods, you may start to feel like you're pulling a never-ending thread. This is a Good Thing. It means dependency injection is making your code more flexible and testable.

Dependency Injectionは、ウィルスです。既存のコードベースに対して、多くのスタティックメソッドを使ってリファクタリングしているならば、果てしない糸を引いている気がし始めるでしょう。これはよい事なのです。それが意味するのは、Dependency Injectionが開発者のコードをより柔軟に、テストしやすくしているといいうことなのです。

If you get in over your head, rather than try to refactor an entire code base all in one shot, you might temporarily store a reference to the Injector in a static field somewhere or use static injection. Name the field's class clearly though: InjectorHack and GodKillsAKittenEveryTimeYouUseMe come to mind. Keep in mind that you you'll have to mock this class, and your unit tests will have to install an Injector here by hand, and remember to clean up afterwards.

 一撃で全体のコードベースをリファクタリングすることを試みるより、むしろ考えられるのは、どこかのスタティックフィールドに一時的にInjectorの参照を格納するか、スタティックインジェクションを使うことかもしれません。もっともその場合は、明確にフィールドのクラスを命名してください: InjectorHackやGodKillsAKittenEveryTimeYouUseMe() [あなたが私を使うたびに神が子猫を殺します():
※訳注参照]が思い浮かびます。

モックにもこのクラスを保持しなくてはならないことを忘れないでください。ユニットテストでは手作業でInjectorをここにインストールし、その後忘れずに後始末をしなければならなくなります。


(※訳注 2002年に「Every time you masturbate… God kills a kitten」〔あなたが自慰行為をするたびに…子猫たちが1匹ずつ神に殺されます。どうか子猫たちの生命を考えてあげてください〕というフォトショップでの合成による偽公共広告が作成され、Fark.comやブログ、電子メールなどで広まってインターネット上の流行となりたちどころに面白画像やニュース画像のパロディを作る際の定番のネタとなりました。ちなみにこの広告ではNHKのドーモくんが神として使われていて、海外のインターネットユーザーはこれによりドーモクンを認知しているとか[以下その画像])