简体   繁体   中英

How reuse a dependency object in another composition in cake pattern

I have two service classes like below...

User Service:

class UserService { dao: UserGroupDao =>
 ...
 def read(userId: String): Future[Option[User]] = dao.readUser(userId)
 ...
}

Group Service:

class GroupService {dao: UserGroupDao =>
 def createGroup(group: Group): Future[Either[String, String]]) = {
  val userService = new UserService() with UserMysqlDao
  userService.read(group.ownerId) map {
   case Some(u) => dao.createGroup(group)
   case None => Left("Invalid User!")
  }
 }
 ...
}

I am just validating if the owner of the group is a valid user. For that I reused the userService.read method with a hardcoded Dao Implementation ie UserMySqlDao.
Here my question is instead of providing hardcoded Dao Impl how could I use the groupservice's dao object. because the type are same for UserService and GroupService.

I tried using the following

val userService = new UserService with dao

but this failed. I am new to scala hence, not really clear why this failed. It would be helpful if someone can put some light on why this is not legal.

Thanks in Advance :)

If I understand right your question, you're looking for a solution for multiple dependencies declaration using the Cake Pattern.

The solution commonly used is to define a new type, a component , that contains references to all the dependencies that you need. In your specific case, the story will be the following.

trait GroupServiceComponent {
  val userService: UserService
  val userGroupDao: UserGroupDao

  class GroupService {
    def createGroup(group: Group): Future[Either[String, String]]) = {
      userService.read(group.ownerId) map {
        case Some(u) => userGroupDao.createGroup(group)
        case None => Left("Invalid User!")
      }
    }
  }
}

Let me know if you need more information.

Just thought I'd have a play around for fun and see what I could get to compile. Not sure that this is a good solution or even good style but my thought was to have the GroupService depend on a UserService, that made it's dao public.

class UserService { d: UserGroupDao =>
  val dao = d
  def read(userId: String): Future[Option[User]] = dao.readUser(userId)
}

class GroupService { svc: UserService =>
  def createGroup(group: Group): Future[Either[String, String]] = {
    svc.read(group.ownerId) map {
      case Some(_) => svc.dao.createGroup(group)
      case None => Left("Invalid User!")
    }
  }
}

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