简体   繁体   中英

Moving all implicits to a common object

I'm trying to move all of my implicits to a common object so I can use them in all of my DAO classes, but I cannot set it up correctly.

I have this so far:

package com.example.dao

import java.sql.Timestamp
import java.time.Instant

import play.api.db.slick.DatabaseConfigProvider
import play.api.db.slick.HasDatabaseConfigProvider
import slick.driver.JdbcProfile

object DbImplicits {
  import driver.api._
  implicit val dateColumnType = MappedColumnType.base[Instant, Timestamp](
    i => Timestamp.from(i),
    ts => {
      ts.toInstant

    }
  )
}

I am getting this error:

not found: object driver

In your DAO it's taken from extended trait HasDatabaseConfigProvider .

Just put your conversation into trait then ( trait DbImplicits ) that require to be mixed-in together with or extends HasDatabaseConfigProvider .

trait DbImplicits { self: HasDatabaseConfigProvider[JdbcProfile] =>
  import self.driver._

    implicit val dateColumnType = MappedColumnType.base[Instant, Timestamp](
        i => Timestamp.from(i),
        ts => {
           ts.toInstant
        }
    )
}

Later use it like this:

class MyClassUsingImplicits @Inject()(
    protected val dbConfigProvider: DatabaseConfigProvider
) extends HasDatabaseConfigProvider[JdbcProfile] 
  with DbImplicits {
  import driver._

  // your further code comes here
}

All this is kind of specific to play-slick .

I also updated the original answer: https://stackoverflow.com/a/41437295/2239369

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM