Hello, colleagues!
The following use case is typical for REST service, I believe.
Consider a POST-controller: it parses JSON data into some Entity (case class) andThen persists it into repository.
It returns `Created` response on success, otherwise it can be `Bad request` (parsing failed) or `Internal Server Error` (db operation failed).
So, we design EntityService like this:
// Source can be PlayFramework's JsValue
trait EntityService[Source] {
def parse(src: Source): \/[Errors, Entity]
def persist(entity: Entity, repo: Repo): EitherT[Future, Errors, Entity]
}
How should I modify this code to make it compose? ParsingService needs nor Repo, neither Future (it is non-blocking). Persisting needs both. Does this mean that should wrap parsing result into Future or EitherT or Kleisli?
In more complex use cases we may have a need to compose further i.e. use Kleisli composition when it comes to futures and eithers. But I tried to keep it simple.
Thank you!
|