Angular-Dart DI 库中的工厂注入 [英] Factory injection in Angular-Dart DI library

查看:17
本文介绍了Angular-Dart DI 库中的工厂注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Dart 应用程序中,我使用了 MVP 模式和 angular-dart 依赖注入库 (angular-di).

在上面的例子中,我不能注入 MyView 或 MyPresenter,因为这是一个循环依赖.

class MyView {MyPresenter 演示者;MyView(this.presenter);}类 MyPresenter {MyView 视图;MyPresenter(this.view);}

我通常使用 Guice 在 Java 中执行此操作的方式是注入一个工厂,例如:

class MyView {MyPresenter 演示者;MyView(this.presenter);}类 MyPresenter {工厂<MyView>工厂视图;MyView 视图;MyPresenter(this.factoryView) {视图 = factoryView(this);}}

如何使用 angular-di 完成此操作?有没有可能在不写工厂的情况下注入工厂?

解决方案

Angular 2 Dart

typedef MyView MyViewFactoryFn(MyPresenter p);提供(MyView, useValue: (MyPresenter p) => new MyView(p));MyPresenter(MyViewFactoryFn vf) {视图 = vf(this);}

typedef MyView MyViewFactoryFn(MyPresenter p);MyView viewFactory(MyPresenter p) =>新的 MyView(p)

const Provider(MyView, useFactory: viewFactory);MyPresenter(MyViewFactoryFn vf) {视图 = vf(this);}

另见

Angular 1 Dart

你可以绑定一个闭包

typedef MyView MyViewFactoryFn(MyPresenter p);bind(MyView, toValue: (MyPresenter p) => new MyView(p));

构造函数应该看起来像

MyPresenter(MyViewFactoryFn vf) {视图 = vf(this);}

还有一个 toFactory: 但我猜 DI 会调用工厂本身,但我猜 toValue: 带闭包可以工作(虽然没有尝试过).

In my Dart application I'm using the MVP pattern and the angular-dart Dependency Injection library(angular-di).

In the example above, I cannot inject MyView or MyPresenter, as this is a circular dependency.

class MyView {
    MyPresenter presenter;
    MyView(this.presenter);
}
class MyPresenter {
    MyView view;
    MyPresenter(this.view);
}

The way I usually did this in Java with Guice was injecting a Factory, like:

class MyView {
    MyPresenter presenter;
    MyView(this.presenter);
}
class MyPresenter {
    Factory<MyView> factoryView;
    MyView view;
    MyPresenter(this.factoryView) {
        view = factoryView(this);
    }
}

How do I accomplish this using angular-di? Is there possible to inject the factory without having to write the factory itself?

解决方案

Angular 2 Dart

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);
}

See also

Angular 1 Dart

You can bind a closure

typedef MyView MyViewFactoryFn(MyPresenter p);
bind(MyView, toValue: (MyPresenter p) => new MyView(p));

the constructor should then look like

MyPresenter(MyViewFactoryFn vf) {
  view = vf(this);
}

There is also a toFactory: but I guess DI will call the factory itself but I guess toValue: with a closure could work (not tried though).

这篇关于Angular-Dart DI 库中的工厂注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆