-
-
Save ahjohannessen/06d6cc853752429b6de01a66714d8f1d to your computer and use it in GitHub Desktop.
Dynamic Transactors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interp.configureCompiler(_.settings.YpartialUnification.value = true) | |
import $ivy.`org.tpolecat::doobie-core:0.5.3` | |
import cats._ | |
import cats.effect._ | |
import cats.implicits._ | |
import doobie._ | |
import doobie.implicits._ | |
case class Customer(id: String) | |
case class DBInfo(address: String, un: String, pw: String) | |
trait DBInfoProvider[F[_]] { | |
def getDBInfo(c: Customer): F[DBInfo] | |
} | |
case class User(name: String) | |
trait Users[F[_]] { | |
def getUsers(c: Customer): F[List[User]] | |
def saveUsers(newList: List[User])(c: Customer): F[Unit] | |
} | |
object TransactorProvider { | |
def getTransactorForDB(db: DBInfo): IO[Transactor[IO]] = ??? | |
} | |
final class UsersDoobieImplementation(DBP: DBInfoProvider[IO]) extends Users[IO] { | |
import TransactorProvider._ | |
def getUsers(c: Customer): IO[List[User]] = for { | |
dbInfo <- DBP.getDBInfo(c) | |
t <- getTransactorForDB(dbInfo) | |
users <- getAllUsers.transact(t) | |
} yield users | |
def saveUsers(newList: List[User])(c: Customer): IO[Unit] = for { | |
dbInfo <- DBP.getDBInfo(c) | |
t <- getTransactorForDB(dbInfo) | |
_ <- saveAllUsers(newList).transact(t) | |
} yield () | |
private def getAllUsers: ConnectionIO[List[User]] = ??? | |
private def saveAllUsers(nw: List[User]): ConnectionIO[Unit] = ??? | |
} | |
final class SomeBusinessLogig[F[_]](U: Users[F])(implicit F: Monad[F]) { | |
def manipiulateUsersForCustomer(c: Customer): F[Unit] = for { | |
users <- U.getUsers(c) | |
manipulatedUsers = users.tail | |
_ <- U.saveUsers(manipulatedUsers)(c) | |
} yield () | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment