Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class UserInfo(account: String, password: String)
- trait UserService {
- def getUserInfo(account: String): UserInfo
- }
- class UserServiceComponent extends UserService {
- def getUserInfo(account: String): UserInfo = UserInfo(account, "123")
- }
- case class LoginEnv(userName: String, password: String, service: UserService)
- object LoginFacade {
- val login = Reader[LoginEnv, Boolean] { loginEnvironment =>
- loginEnvironment.service.getUserInfo(loginEnvironment.userName).password == loginEnvironment.password
- }
- }
- object HomeController {
- val autenticarUsuario = LoginFacade.login.tapWith { (loginEnv, isAuthorized) =>
- if (isAuthorized) {
- s"Olá, ${loginEnv.userName}"
- } else {
- "Erro de login"
- }
- }
- }
- object ReaderMonadInjection extends App {
- val userService = new UserServiceComponent()
- val loginEnvironment = new LoginEnv("Rodrigo", "123", userService)
- val loginMessage = HomeController.autenticarUsuario.run(loginEnvironment)
- println(loginMessage)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement