2017-01-22 3 views
1

언어 모델, 테이블 및 저장소가 있습니다. 지금까지 작동 :일반 저장소 방법으로 고생 할 때

package repositories 

import javax.inject.Inject 

import Helper 
import model.{Language, LanguageModel} 
import play.api.Logger 
import play.api.cache.SyncCacheApi 
import slick.jdbc.JdbcProfile 

import scala.concurrent.{ExecutionContext, Future} 
import scala.util.{Failure, Success} 

class LanguageRepository @Inject()(cache: SyncCacheApi, jdbcProfile: JdbcProfile, implicit val executionContext: ExecutionContext) 
{ 
    private val model = new LanguageModel(jdbcProfile) 

    import jdbcProfile.api._ 

    def all(userName: String): Future[Seq[Language]] = 
    { 
    cache.get[Future[Seq[Language]]](buildCacheKey(userName)) match 
    { 
     case Some(x) => {Logger.info("[LanguageRepository](all) Found something in cache"); x} 
     case None => { 
     Logger.info("[LanguageRepository](all) Nothing useful to be found in cache, calling database now") 
     val result = retrieve(userName) 
     result.onComplete{ 
      case Success(value) => if(!value.isEmpty) cache.set(buildCacheKey(userName), result) 
      case Failure(e) =>() 
     } 

     result 
     } 
    } 
    } 

    private def retrieve(userName: String): Future[Seq[Language]] = 
    { 
    // TODO extract this to a repositoryTrait and implement fallbacks etc 
    val db = Database.forURL(Helper.getDbUrl(), driver = Helper.getDbDriver()) 

    db.run(model.all.result) 
    } 

    private def buildCacheKey(userName: String): String = s"$userName.languages" 
} 

지금 나는 과거의 현재와 함께 고민하고있다.

그래서이 특성을 만들어서 LanguageRepository에서 확장하여 모든 저장소/모델에 대해 동일한 일반 검색 방법을 없애고 싶습니다. 그러나 지금까지 슬프게 운이 :

trait Repository 
{ 
    type Entity 
    val model: Base 
    val profile: JdbcProfile 

    import profile.api._ 

    protected def retrieve(userName: String): Future[Seq[Entity]] = 
    { 
    val db = Database.forURL(Helper.getDbUrl(), driver = Helper.getDbDriver())  
    db.run(model.all.result) 
    } 
} 

이 기본입니다 :

여기
trait Base 
{ 
    val dbProfile: JdbcProfile 
    import dbProfile.api._ 

    type Entity 
    type EntityTable <: Table[Entity] 
    lazy val all = TableQuery[EntityTable] 
} 

나는 하나의 오류 >> 클래스 형을 요구받을 만 Base.this.EntityTable는

class LanguageModel(databaseProfile: JdbcProfile) extends Base 
    { 
    override val dbProfile: JdbcProfile = databaseProfile 
    import dbProfile.api._ 

    ... 

    override type EntityTable = LanguageTable 
    } 

저장소

발견 유형 자체가 일치하지 않으므로 자체도 컴파일되지 않습니다. 여러 가지 문제가 있으며이를 해결하기 위해 어디서부터 시작해야할지 모르겠습니다.

답변

1

기본 테이블 정의가 그렇게 작동하지 않습니다. 클래스 유형이 필요합니다. 대신 제네릭을 사용해야합니다. 또한 여러 추상화를 작성하는 대신 하나의 추상화로 시작하여 거기에서 진화합니다. 다음 줄을 따라 무언가를 시도하십시오 :

class Repository[A, B <: Table[A]](t: TableQuery[B]) { 
    val model = t 
    //protected def retrieve .. 
} 

class LanguageModel(databaseProfile: JdbcProfile) extends Repository[Language, LanguageTable](TableQuery[LanguageTable]) { 
    //... 
} 

먼저 컴파일 할 모든 것을 가져 와서 한 번에 하나의 추상화 클래스를 추가하십시오.

+0

모델은 저장소가 아닙니다. – Sorona