SHARE
TWEET

Untitled

a guest Apr 21st, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. case class User()
  2. case class Book()
  3.  
  4. trait UserRepository {
  5.   def getResource(): User
  6. }
  7.  
  8. trait UserRepositoryComponent {
  9.     def userRepository: UserRepository
  10. }
  11.  
  12. trait BookRepository {
  13.   def getResource(): Book
  14. }
  15.  
  16. trait BookRepositoryComponent {
  17.     def bookRepository: BookRepository
  18. }
  19.  
  20. trait DbUserRepositoryComponent extends UserRepositoryComponent {
  21.   val db: DB
  22.     override def userRepository = new UserRepository {
  23.     overrride def getResource(): User = {
  24.     db.run()
  25.     User()
  26.    }
  27.   }
  28. }
  29.  
  30. trait DbBookRepositoryComponent extends BookRepositoryComponent {
  31.   val db: DB
  32.     override val bookRepository = new BookRepository {
  33.     overrride def getResource(): Book = {
  34.       db.run()
  35.       Book()
  36.      }
  37.   }
  38. }
  39.  
  40. // trait Service extends UserRepository with BookRepository { => Service IS A UserRepository
  41. trait Service {
  42.     self: UserRepositoryComponent with BookRepositoryComponent => // UserService REQUIRES some Impl of UserRepository
  43.  
  44.   def foo(): Unit = {
  45.     userRepository.getResource()
  46.     bookRepository.getResource()
  47.   }
  48. }
  49.  
  50.  
  51. class ServiceImplA extends DbUserRepository with DbBookRepository
  52. class ServiceImplB extends MockUserRepository with MockBookRepository
  53. class ServiceImplC extends FileUserRepository with FileBookRepository
  54. class ServiceImplD extends DbUserRepository with FileBookRepository
RAW Paste Data
Top