4

言語: スカラ; フレームワーク: Play 2.5; ライブラリ: Silhouette 4.0、Guice、scala-guice。

公式の Silhouette シード プロジェクトの 1 つは、guice と scala-guice (net.codingwell.scalaguice.ScalaModule) を使用して DI 構成を記述します。コードは次のようになります。

import net.codingwell.scalaguice.ScalaModule

class Module extends AbstractModule with ScalaModule{

  /**
    * Configures the module.
    */
  def configure() {

    bind[Silhouette[MyEnv]].to[SilhouetteProvider[MyEnv]]
    bind[SecuredErrorHandler].to[ErrorHandler]
    bind[UnsecuredErrorHandler].to[ErrorHandler]
    bind[IdentityService[User]].to[UserService]

net.codingwell.scalaguice ライブラリの魔法がなければ、このコードはどのように見えるのだろうか。誰かが元の装いだけを使ってこれらのバインディングを書き直すことはできますか?

さらに、次のコードもあります。

@Provides
  def provideEnvironment(
      userService: UserService,
      authenticatorService: AuthenticatorService[CookieAuthenticator],
      eventBus: EventBus
  ): Environment[MyEnv] = {
    Environment[MyEnv](
      userService,
      authenticatorService,
      Seq(),
      eventBus
    )
  }

前もって感謝します。

4

2 に答える 2

3

正しい方向を示してくれたinsan-eに感謝します。Guiceを使用して一般的な実装を注入する方法を示す回答は次のとおりです。

Guice を使用して汎用実装を挿入する

したがって、式から scala-guice ライブラリを削除すると、バインディングは次のように記述できます。

import com.google.inject.{AbstractModule, Provides, TypeLiteral}
class Module extends AbstractModule {

  /**
    * Configures the module.
    */
  def configure() {
    bind(new TypeLiteral[Silhouette[MyEnv]]{}).to(new TypeLiteral[SilhouetteProvider[MyEnv]]{})
于 2016-11-08T10:02:59.087 に答える
2

関数を導入するトレイトに関する記述があります。こちらをご覧ください: https://github.com/codingwell/scala-guice/blob/develop/src/main/scala/net/codingwell/scalaguice/ScalaModule.scala #L32

したがって、この場合は次のように変換されます。

class SilhouetteModule extends AbstractModule {

  def configure {
    bind(classOf[Silhouette[DefaultEnv]]).to(classOf[SilhouetteProvider[DefaultEnv]])
    bind(classOf[CacheLayer]).to(classOf[PlayCacheLayer])

    bind(classOf[IDGenerator]).toInstance(new SecureRandomIDGenerator())
    bind(classOf[PasswordHasher]).toInstance(new BCryptPasswordHasher)
    ...
}
于 2016-11-07T19:31:30.510 に答える