Use Case for Database and akka actors

心已入冬 提交于 2020-05-17 06:12:09

问题


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:

  1. 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.
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!