角飞镖中 type() 的其他方式 [英] Other way that type() in angular dart

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

问题描述

我完成了 angular dart 教程,但我对此有疑问.

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

要声明可用于依赖注入的类型,我必须这样做:

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

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

对所有类型依此类推.

在一个大应用中,你可以有数百个类型,所以声明所有类型是一种奇怪的方式.

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?

谢谢.

推荐答案

您可以使用反射来收集类型.如果您需要有关此方法的更多信息,请添加评论(我尽量避免在网络应用中进行反射).

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 类型的对象,并且您想指定实际应该注入哪些满足要求(实现接口)的类.然后你开始用反射编写特殊情况.
使用 type(InterfaceX,implementedBy: Y); 总是超级可读.
编辑结束

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)

创建更多模块并使用 install

例如见
- https://github.com/akserg/angular.dart.ui/blob/master/lib/accordion/accordion.dart
- https://github.com/akserg/angular.dart.ui/blob/master/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());
  }
}

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

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