在我的 Dart 应用程序中,我使用 MVP 模式和 Angular-dart 依赖注入库(Angular-di)。
在上面的示例中,我无法注入 MyView 或 MyPresenter,因为这是循环依赖项。
class MyView {
MyPresenter presenter;
MyView(this.presenter);
}
class MyPresenter {
MyView view;
MyPresenter(this.view);
}
我通常使用 Guice 在 Java 中执行此操作的方式是注入一个工厂,例如:
class MyView {
MyPresenter presenter;
MyView(this.presenter);
}
class MyPresenter {
Factory<MyView> factoryView;
MyView view;
MyPresenter(this.factoryView) {
view = factoryView(this);
}
}
我如何使用 Angular-di 来实现这一点?是否可以注入工厂而无需编写工厂本身?
Angular 2 飞镖
typedef MyView MyViewFactoryFn(MyPresenter p);
provide(MyView, useValue: (MyPresenter p) => new MyView(p));
MyPresenter(MyViewFactoryFn vf) {
view = vf(this);
}
or
typedef MyView MyViewFactoryFn(MyPresenter p);
MyView viewFactory(MyPresenter p) => new MyView(p)
const Provider(MyView, useFactory: viewFactory);
MyPresenter(MyViewFactoryFn vf) {
view = vf(this);
}
也可以看看
- https://webdev.dartlang.org/angular/api/angular2.core/Provider-class https://webdev.dartlang.org/angular/api/angular2.core/Provider-class
- https://webdev.dartlang.org/angular/api/angular2.core/provide https://webdev.dartlang.org/angular/api/angular2.core/provide
角 1 飞镖
您可以绑定一个闭包
typedef MyView MyViewFactoryFn(MyPresenter p);
bind(MyView, toValue: (MyPresenter p) => new MyView(p));
构造函数应该看起来像
MyPresenter(MyViewFactoryFn vf) {
view = vf(this);
}
还有一个toFactory:
但我想 DI 会调用工厂本身但我想toValue:
带有一个闭包可以工作(虽然没有尝试过)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)