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

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

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

Guice User's Guide ■5. Guice vs. Dependency Injection By Hand

Guice vs. お手製DI

As you can see, Guice saves you from having to write factory classes. You don't have to write explicit code wiring clients to their dependencies. If you forget to provide a dependency, Guice fails at startup. Guice handles circular dependencies automatically.

お分かりのように、Guiceはファクトリークラスを書く作業から解放してくれます。クライアントとそれらの依存性を結びつけるコードを明示的に書く必要がありません。もし、依存性の提供を忘れたらGuiceは起動できません。Guiceは複数の依存性を自動的に巡回して処理します。

Guice enables you to specify scopes declaratively. For example, you don't have to write the same code to store an object in the HttpSession over and over.

Guiceはスコープを明確に記述できるようにします。たとえば、HttpSessionにオブジェクトを格納するために、同じコードを何度も書く必要がありません。

In the real world, you often don't know an implementation class until runtime. You need meta factories or service locators for your factories. Guice addresses these problems with minimal effort.

現実世界では、実行時まで実装クラスを知らないことがしばしばです。メタファクトリーまたはファクトリーのためのサービスロケーターが必要です。Guiceは、最低限の労力でそれらの問題を処理します。

When injecting dependencies by hand, you can easily slip back into old habits and introduce direct dependencies, especially if you're new to the concept of dependency injection. Using Guice turns the tables and makes doing the right thing easier. Guice helps keep you on track.

手作業で、依存性を注入しているときは、古い習慣に逆戻りしたり、依存性への直結を持ち込んでしまったりする可能性があります。Dependency Injectionのコンセプトに対して初心者であるなら特にそうです。Guiceを使うことで、形成を逆転させます。そして正しい事をするのを簡単にしてくれます。Guiceは軌道からそれないように手助けしてくれるのです。