Cake Pattern与依赖注入

一篇中文的,相当不错:
Dependency Injection in Scala

Cake Pattern 大概是除了 JSR-330 外,最常被提到在 Scala 下的實作 DI 的方式了,Cake Pattern 是由 Scala 之父 Martin Odersky 的一篇論文 Scalable Component Abstractions 中提到,不過大 多數人看過的版本,應該是 Jonas Bonér 的 Real-World Scala: Dependency Injection (DI)

Real-World Scala: Dependency Injection (DI) 原文:
http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/

2 thoughts on “Cake Pattern与依赖注入

  1. 你好

    在《快学Scala》271 页 (原书255页), 第18章第11节, 依赖注入

    ———————————
    然后,我们就可以像这样来组装我们的应用:
    object MyApp extends App with FileLogger(“test.log”) with MockAuth(“users.txt”)
    像这样使用特质的组合有些别扭。
    ———————————

    以上代码经过我的测试是无法编译通过的。为何?
    如果FileLogger 或 MockAuth 为 class, 则编译错误为 class XXX needs to be a trait to be mixed in
    如果FileLogger 或 MockAuth 为 trait,则编译错误为 traits or objects may not have parameters

    请问作为Scala资深使用者的您,是否有什么解决办法?抑或这是原书的一个错误?

    Thanks & Regards,
    Jonathan

Leave a Reply

Your email address will not be published. Required fields are marked *