Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/forms/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 是否可以使用i dependecy注入来注入scala对象?_Forms_Scala_Dependency Injection_Singleton_Scaldi - Fatal编程技术网

Forms 是否可以使用i dependecy注入来注入scala对象?

Forms 是否可以使用i dependecy注入来注入scala对象?,forms,scala,dependency-injection,singleton,scaldi,Forms,Scala,Dependency Injection,Singleton,Scaldi,是否可以使用i dependecy注入来注入scala对象? 如果是这样,我如何获得喷油器 我有一个对象注册表单,它必须是一个单身 object SignUpForm { val form = Form( mapping( "firstName" -> nonEmptyText, "lastName" -> nonEmptyText, "email" -> email.verifying(Constraints.userExis

是否可以使用i dependecy注入来注入scala对象? 如果是这样,我如何获得喷油器

我有一个对象注册表单,它必须是一个单身

object SignUpForm {

  val form = Form(
    mapping(
      "firstName" -> nonEmptyText,
      "lastName" -> nonEmptyText,
      "email" -> email.verifying(Constraints.userExists),
      "password" -> mapping (
        "main" -> nonEmptyText.verifying(Constraints.passwordLenth),
        "confirm" -> nonEmptyText
      )(Password.apply)(Password.unapply).verifying(Constraints.passwordConfirmation)
    )(Data.apply)(Data.unapply)

  )

  case class Password(
    main: String,
    confirm: String
  )

  case class Data(
    firstName: String,
    lastName: String,
    email: String,
    password: Password
  )
}
。。。和对象约束

object Constraints {

  val userService = inject[UserService]

  def passwordConfirmation: Constraint[Password] = Constraint("password.confirm"){ password =>
    if (password.main.equals(password.confirm)) Valid else Invalid(Seq(ValidationError("password doesnt equal the confirmation password", "password.main", "confirm")))
  }

  def passwordLenth: Constraint[String] = Constraint("password.length"){ password =>
    if (password.length >= 8) Valid else Invalid(Seq(ValidationError("The minimum password length is " + 8 + " characters", "password.main", "length")))
  }

  def userExists: Constraint[String] = Constraint("user.exists"){ email =>
    if (userExistsWithEmail(email.toLowerCase)) Valid else Invalid(Seq(ValidationError("The user with email " + email + " already exists", "user", "email")))
  } ...
问题是->需要注入userservice,但只要没有作为构造函数参数传递的隐式注入器,就不能注入它(这是不可能的,因为我们这里有一个对象)

。。。我怎样才能解决这个问题


提前谢谢

看来您已经回答了自己的问题。对象是无法初始化的单例,因此不能有构造函数参数


将您的
约束
对象转换为一个类,然后在您(或您的DI框架)创建它时就可以注入依赖项。

不幸的是,对于BushTi或任何其他DI库(我知道的),这是不可能的。依赖项注入的全部目的是避免使用单例对象。因此,我强烈建议您在大多数情况下避免使用它们。
...
private def userExistsWithEmail(email: String):Boolean = {
    val result = Await.result(userService.retrieve(LoginInfo(CredentialsProvider.Credentials, email)),1 seconds)

     result match {
      case Some(u) => true
      case None => false
    }
  }
}