热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在角镖中键入()的其他方式-Otherwaythattype()inangulardart

IdidtheangulardarttutorialandIhaveaquestionaboutit.我做了角落飞镖教程,我有一个问题。Todeclareatype

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

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?

有没有其他方法可以做到这一点?

Thanks.

2 个解决方案

#1


3  

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).

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

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

编辑反射可能有效,但是当你开始使用特殊情况时,它会很快变得难以理解。当您使用DI时,您经常遇到类的构造函数需要InterfaceX类型的对象并且您想要指定实际应该注入满足要求的哪些类(实现接口)的情况。然后你开始用反射编写特殊情况。使用类型(InterfaceX,implementedBy:Y);总是超级可读。编辑结束

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

我不知道你是否认为这是一种改进,但我们做了什么(我在几个项目中看到过)

Create more modules and add them to MyAppModule with install

创建更多模块并使用install将它们添加到MyAppModule

see for example at
- 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

例如参见 - 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());
  }
}

#2


1  

Here are my thoughts as someone relatively new to angular dart. This doesn't directly answer your question but I hope it's useful.

以下是我对角镖相对较新的想法。这不能直接回答你的问题,但我希望它有用。

Firstly It's generally thought to be "bad" to use globals in programming. However if you're starting out with a framework, you don't have time to learn all of the best practices and since your app starts off tiny it's okay to use a few globals. Eventually you refactor as you learn better practices but you should be allowed to take a few shortcuts when building a small app. With websites, Routes are effectively globals but even on a large site you only have a few hundred before you split the site into multiple sites.. Similar applies for types IMO.

首先,人们普遍认为在编程中使用全局变量是“坏事”。但是,如果您刚开始使用框架,则没有时间学习所有最佳实践,并且由于您的应用程序开始很小,因此可以使用一些全局变量。最后,当您学习更好的实践时,您会重构,但在构建一个小应用程序时,您应该被允许采取一些快捷方式。对于网站,路由实际上是全局的,但即使在大型站点上,在将站点拆分为多个站点之前,您只有几百个。类似适用于IMO类型。

types are effectively like having global variables in a program. This is because there is only one html DOM and so selectors within that dom need to be aware of other selectors to avoid collision. (selectors are like globals in a sense - same with css - although we could reuse selectors for different types in different views, that would muddle the css relationship and also add one extra complication when trying to read the html source and refer back to the dart code (you would have to decide which view is active etc, and then when you decide to pull the selectors together into the same view things get further complex when you have to rename one of them)). Basically to avoid selectors colliding with selectors we need to avoid types colliding with types IMO. Having types effectively as globals is possibly the cleanest way to solve this problem with the current state of html.

类型实际上就像在程序中具有全局变量一样。这是因为只有一个html DOM,所以dom中的选择器需要知道其他选择器以避免冲突。 (选择器在某种意义上就像全局变量 - 与css相同 - 尽管我们可以在不同的视图中重用不同类型的选择器,这会混淆css关系,并且在尝试读取html源时又会增加一个额外的复杂性并返回到dart代码(你必须决定哪个视图是活动的等等,然后当你决定将选择器拉到同一个视图中时,当你必须重命名其中一个时,事情变得更加复杂))。基本上为了避免选择器与选择器冲突,我们需要避免类型与IMO类型冲突。有效地将类型作为全局变量可能是使用当前html状态解决此问题的最简洁方法。

Also because single page apps shouldn't be too large, I encourage "flat code" over "nested code" although it's good to have balance. While components of single page apps may be complex with lots of code I don't think you should need so many types. 100's of types should be manageable. Once you get more than that it's probably a good idea to build a new application. Otherwise your single page app is likely to be too complicated for the users and load slowly etc.

另外因为单页应用程序不应该太大,我鼓励“平板代码”而不是“嵌套代码”,尽管平衡很好。虽然单页应用程序的组件可能很复杂而且代码很多,但我认为您不需要这么多类型。 100种类型应该是可管理的。一旦你获得了更多,建立一个新的应用程序可能是个好主意。否则,您的单页应用可能对用户来说过于复杂并且加载缓慢等。


推荐阅读
author-avatar
枫涵笑
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有