问题
I have certain design issues. I can not change the main class and it's like the following:
object Main
{
...
implicit val schema = "schema"
implicit val db= Database.forURL("url")
val impl = new ImplA()
val implB = new ImplB()
system.actorOf(ActorA.props(impl, implB))
}
The implementation classes are like:
class ImplA(implicit val schema: String, db: Database) {
val queryA = TableQuery[STable]
def getData() =
{
...
db.run(query)
}
}
class ImplB(implicit val schema: String, db: Database) {
val queryB = TableQuery[BTable]
def getDataB() =
{
...
db.run(query)
}
}
I am using slick and the table structures are like:
class BTable(tag: Tag)(implicit val schema: String)
extends Table[CaseClass](tag, Some(schema), "table") {
def a = column[String]("a")
def b = column[String]("b")
def * =
(a,b).shaped <> (CaseClass.tupled, CaseClass.unapply)
}
There are two problems that i need to solve:
- PROBLEM 1 - I want to take the following lines out of actor and put it in ImplA or ImplB but it is a mixed query. So it i put it in ImplA then implB will not be available. And they are classes.
- PROBLEM 2-I totally want to eliminate the db and schema from Actor but have a use class where a class which is initialized from inside the actor requies the db implicitly.
class ActorA(implAa: ImplA, implB: ImplB)(implicit db: Database, schema: String) extends Actor
{
// PROBLEM 1
val query = (for {
sc <- implAa
c <- implB
} yield(sc,c)
db.run(query)
// PROBLEM 2
new DemoClass(param).methodCall
}
class DemoClass(param: String)(implicit db: Database)
{
def methodCall = ...
}
Without changing the main class, how can i address the problems? I can change Impls or table class make them trait or object or anything that can solve the issues.
来源:https://stackoverflow.com/questions/61802758/use-case-for-database-and-akka-actors