Last active
February 24, 2017 19:05
-
-
Save akozhemiakin/55a02f327361243af56d92f2aac6177e to your computer and use it in GitHub Desktop.
Question about organizing dependency tree with Kleisli
This file contains hidden or 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
// I have no experience with Scalaz, but I believe that Scalaz's Kleisli and the one of Cats are quite similar | |
import cats.Id | |
import cats.data.Kleisli | |
import cats.implicits._ | |
case class User(id: Int, name: String, active: Boolean) | |
trait UserService { | |
type DeactivateConfigT | |
def deactivate(id: Int): Kleisli[Id, DeactivateConfigT, User] | |
} | |
trait UserRepository { | |
type GetConfigT | |
type SaveConfigT | |
def get(id: Int): Kleisli[Option, GetConfigT, Unit] | |
def save(user: User): Kleisli[Id, SaveConfigT, Unit] | |
} | |
object DbUserRepository extends UserRepository { | |
final case class DbConfig(host: String, user: String, password: String, db: String) | |
type GetConfigT = DbConfig | |
type SaveConfigT = DbConfig | |
// Implementation of the following methods are straightforward (or at least I think so), because | |
// they do not depend on something returning Kleisli itself | |
def get(id: Int): Kleisli[Option, GetConfigT, Unit] = ??? | |
def save(user: User): Kleisli[Id, SaveConfigT, Unit] = ??? | |
} | |
object DefaultUserService extends UserService { | |
final case class DeactivateConfig(repo: UserRepository) | |
type DeactivateConfigT = DeactivateConfig | |
// And here I have no idea how we should use UserRepository here. Especially taking into account | |
// that we want to keep things decoupled and we depend here on the UserRepository trait, not a | |
// concrete implementation with a known configuration requirements | |
def deactivate(id: Int): Kleisli[Id, DeactivateConfigT, User] = ??? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you by chance find an answer to your question? I've run into a similar scenario with configuring my repository through dependency injection.