Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@akozhemiakin
Last active February 24, 2017 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akozhemiakin/55a02f327361243af56d92f2aac6177e to your computer and use it in GitHub Desktop.
Save akozhemiakin/55a02f327361243af56d92f2aac6177e to your computer and use it in GitHub Desktop.
Question about organizing dependency tree with Kleisli
// 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] = ???
}
@lu4nm3
Copy link

lu4nm3 commented Feb 24, 2017

Did you by chance find an answer to your question? I've run into a similar scenario with configuring my repository through dependency injection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment