我遵循了“功能和反应建模”()一书的设计原则。
因此,所有服务方法都返回Kleisli。
问题是如何在这些服务上添加一个可更新的缓存。
这是我目前的实现,有没有更好的方法(现有的组合器,更多的功能方法,…)??
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Await, Future}
import scalaz.Kleisli
trait Repository {
def all : Future[Seq[String]]
def replaceAll(l: Seq[String]) : Future[Unit]
}
trait Service {
def all = Kleisli[Future, Repository, Seq[String]] { _.all }
def replaceAll(l: Seq[String]) = Kleisli[Future, Repository, Unit] { _.replaceAll(l) }
}
trait CacheService extends Service {
var cache : Seq[String] = Seq.empty[String]
override def all = Kleisli[Future, Repository, Seq[String]] { repo: Repository =>
if (cache.isEmpty) {
val fcache = repo.all
fcache.foreach(cache = _)
fcache
}
else
Future.successful(cache)
}
override def replaceAll(l: Seq[String]) = Kleisli[Future, Repository, Unit] { repo: Repository =>
cache = l
repo.replaceAll(l)
}
}
object CacheTest extends App {
val repo = new Repository {
override def replaceAll(l: Seq[String]): Future[Unit] = Future.successful()
override def all: Future[Seq[String]] = Future.successful(Seq("1","2","3"))
}
val service = new CacheService {}
println(Await.result(service.all(repo), Duration.Inf))
Await.result(service.replaceAll(List("a"))(repo), Duration.Inf)
println(Await.result(service.all(repo), Duration.Inf))
}关于@timotyperigo注释的更新,我已经在存储库级别实现了缓存
class CachedTipRepository(val self:TipRepository) extends TipRepository {
var cache: Seq[Tip] = Seq.empty[Tip]
override def all: Future[Seq[Tip]] = …
override def replace(tips: String): Unit = …
}我仍然对改进设计的反馈感兴趣。
发布于 2017-07-07 18:02:33
蒂莫西是完全正确的:缓存是存储库(而不是服务)的实现特性。实现特性/细节不应在合同中公开,此时您的设计(但不是您的实现)做得很好!
深入了解您的设计问题,您很感兴趣的是如何在Scala中完成依赖项注入:
蛋糕模式和构造函数注入有一个相似之处:依赖关系在创建时被绑定。使用Reader monad (Kleisli只是在上面提供了一个额外的层),您可以延迟绑定,这将导致更多的可组合性(由于组合器)、更强的可测试性和更多的灵活性。
如果通过添加缓存功能来装饰现有的TipRepository,那么可能不需要Kleisli的好处,甚至可能使代码更难阅读。使用构造函数注入似乎是合适的,因为它是最简单的模式,可以让您“做好”事情。
https://stackoverflow.com/questions/36461840
复制相似问题