Noe*_*Yap 4 dependency-injection scala guice
我阅读了没有体操PDF的依赖注入,这表明不需要任何花哨的DI框架,但它超出了我的掌握(至少没有具体的例子).当我有机会时,我会尝试在没有体操和死亡简单依赖注入的情况下观察依赖注入.
在Java中使用Guice,如果A依赖于B和C并且B和C都依赖于D,那么就会有:
public class A {
@Inject
public A(B b, C c) {
this.b = b;
this.c = c;
}
}
public class B {
@Inject
public B(D d) {
this.d = d;
}
}
public class C {
@Inject
public C(D d) {
this.d = d;
}
}
public class D { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
以及一个模块,说明要使用哪个D实现,然后只需要从进样器中请求A的实例:
A a = injector.createInstance(A.class);
Run Code Online (Sandbox Code Playgroud)
鉴于上面的URL中显示的内容,上述代码的Scala等价物如何?
FWIW,我也正在调查https://github.com/dickwall/subcut/blob/master/GettingStarted.md,我只是想了解反DI解决方案.
对于您正在描述的用例,隐式参数完全足够.
case class A(implicit b: B, c: C)
case class B(implicit d: D)
case class C(implicit d: D)
class D { /* ... */ }
implicit val theD = new D
implicit val theB = B()
implicit val theC = C()
Run Code Online (Sandbox Code Playgroud)
现在你可以要求一个A
公正的:
val a = A()
Run Code Online (Sandbox Code Playgroud)
可以用self-types来解决。
A 依赖于 B 和 C,B 和 C 都依赖于 D
所以人们可以这样写:
class A {
self: B with C =>
}
trait B {
self: D =>
}
trait C {
self: D =>
}
trait D {}
Run Code Online (Sandbox Code Playgroud)
然后在呼叫端:
val x = new A with BImpl with CImpl with DImpl
Run Code Online (Sandbox Code Playgroud)
但下面的代码无法编译,因为对 B、C、D 类的依赖关系未解决:
val x = new A
Run Code Online (Sandbox Code Playgroud)