其他方式,类型()的角度镖角度、类型、方式

2023-09-13 03:02:42 作者:伱若活的凄凉涐定笑的猖狂

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

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