其他方式,类型()的角度镖 [英] Other way that type() in angular dart

查看:112
本文介绍了其他方式,类型()的角度镖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的角镖教程,我有一个疑问的。

I did the angular dart tutorial and I have a question about it.

要声明可供dependecy注射型,我必须这样做:

To declare a type available for dependecy injection, I have to do this:

class MyAppModule extends Module {
  MyAppModule() {
    type(RecipeBookController);
  }
}

等了所有类型的。

And so on for all types.

在一个大的应用程序,你可以有上百个类型,所以它的声明所有类型的一个奇怪的方式。

In a big app, you can have hundreds type, so it's a weird way to declare all types.

是否有任何其他方式做到这一点?

Is there any other way to do this?

感谢。

推荐答案

您可以使用反射来收集类型。如果您需要了解该方法的更多信息请添加评论(我会尽量避免在Web应用程序反射)。

You can use reflection to collect the types. Please add a comment if you need more information about this approach (I try to avoid reflection in web apps).

修改结果
反射可以工作,但是当你开始使用特殊情况下,它会读取速度非常快。结果
当您使用DI你经常在您的类的构造函数需要类型的对象的情况下 InterfaceX ,并要指定那些符合要求的类(实现接口)实际上应该被注入。然后,你开始code的特殊情况与反思。结果
使用键入(InterfaceX,implementedBy:Y); 总是超级可读的结果。
修改END

EDIT
Reflection may work, but when you start using special cases it gets unreadable very fast.
When you use DI you often have situations where your class' constructor requires an object of type InterfaceX and you want to specify which of the classes that fulfill the requirement (implement the interface) should actually be injected. Then you start to code special cases with reflection.
Using type(InterfaceX, implementedBy: Y); is always super readable.
EDIT END

我不知道你是否认为这是一个进步,但我们所做的(我在几个项目中看到)

I don't know if you see this as an improvement but what we did (and I have seen in several projects)

创建更多的模块,并将其与添加到 MyAppModule 安装

Create more modules and add them to MyAppModule with install

见例如在结果
- https://github.com/akserg/angular.dart.ui/blob/master/lib/accordion/accordion.dart

- https://github.com/akserg/angular。 dart.ui / BLOB /主/ lib中/ angular_ui.dart

accordion.dart

class AccordionModule extends Module {
  AccordionModule() {
    type(AccordionComponent);
    type(AccordionHeadingComponent);
    type(AccordionGroupComponent);
    value(AccordionConfig, new AccordionConfig());
  }
}

angular_ui.dart

class AngularUIModule extends Module {
  AngularUIModule() {
    install(new AlertModule());
    install(new AccordionModule()); // the above module
    install(new ButtonModule());
    install(new CarouselModule());
    install(new CollapseModule());
    install(new DropdownToggleModule());
    install(new ProgressbarModule());
    install(new RatingModule());
    install(new TabsModule());
    install(new TimeoutModule());
    install(new TransitionModule());
    install(new ModalModule());
  }
}

这篇关于其他方式,类型()的角度镖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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