是Dart中的控制器单例 [英] Are the Controllers in Dart singleton

查看:129
本文介绍了是Dart中的控制器单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以使用Dart中的工厂创建单例类。但我偶然回忆起在某处读到的类,使用类型(MyController)或类型(MyServiceClass)注册的类本身就是singleton。



这是真的吗?如果是这样,它只适用于类型(MyController)注册的类,或者它使用注释@NgController等。这如何影响我们写的服务和工厂类。



此外,我在哪里可以找到解释相同的文档或链接。

解决方案

单身。长回答讨论了层次依赖注入和AngularDart模板编译器。



依赖注入(DI)



DI的基本设置是为注册到DI系统的每个类型创建单个实例。在创建注入器之前,通过模块注册类型。一旦创建注入器,它将按需创建实例。

  var injector = new Injector([
new模块()
..type(SomeClass)
..value(AnotherClass,new AnotherClass('toplevel'))]);

分层DI



AngularDart使用分级DI系统,它允许我们通过创建子注入器来注入注入器中的类型。考虑

  var childInjector = new Injector.fromParent([
new Module().. type(AnotherClass,implementedBy: OtherSubclass)],inject);

如果从第一个注入器获取AnotherClass类型,您将获得toplevelAnotherClass。从childInjector,你会得到一个AnotherSubclass的实例。但是,两个注入器共享第一个注册类型SomeClass的相同实例



AngularDart的编译器 >

当Angular实例化模板时,它会遍历模板的DOM,寻找与指令选择器匹配的元素。当它找到一个匹配一个或多个指令的元素时,它将为该元素创建一个新的子注入器。然后使用新的子注入器创建指令。



这意味着您有一个分层的注入器结构,它反映了DOM结构。当为两个不同的元素创建相同的指令时,它们在两个不同的注入器中创建。



然而,由于指令可以从注入器请求其他指令),如果第二个指令是在共享父注入器上创建的,则两个指令可能共享一秒的同一个实例。



指令选择器和有效



创建编译器时,它会获取所有注册指令及其选择器的列表。它通过向DI系统请求用@NgAnnotation注释的所有类型来执行此操作。 @NgDirective,@NgComponent,@NgController都是@NgAnnotation的子类型,因此它们包含在该列表中。



这就是为什么你可能有两个类型,MyController和MyService ,在同一个模块中,但只有一个,MyController,由编译器使用。


I know we can create singleton classes using the factory in Dart. But i happen to recall reading somewhere, classes registered using type(MyController) or type(MyServiceClass) happen to be singleton on their own.

Is that true? If so, does it apply to just classes registered with type(MyController) or does it use the annotation @NgController, etc. How does that impact the service and factory classes we write.

Also, where can i find a doc or link explaining the same.

解决方案

Short answer: No, NgControllers are not singletons. The long answer discussed hierarchial dependency injection and the AngularDart template compiler.

Dependency Injection (DI)

The basic setup for DI is to create a single instance for each type registered with the DI system. Types are registered through modules before the injector is created. Once the injector is created, it will create instances on-demand.

var injector = new Injector([
    new Module()
       ..type(SomeClass)
       ..value(AnotherClass, new AnotherClass('toplevel'))]);

Hierarchical DI

AngularDart uses a hierarchical DI system, which allows us to "shadow" types in the injector by creating child injectors. Consider

var childInjector = new Injector.fromParent([
    new Module()..type(AnotherClass, implementedBy: AnotherSubclass)], injector);

If you get the AnotherClass type from the first injector, you will get the "toplevel" AnotherClass. From the childInjector, you will get an instance of AnotherSubclass. However, both injectors share the same instance of the first registered type, SomeClass.

AngularDart's Compiler

When Angular instantiates a template, it walks the template's DOM looking for elements that match directive selectors. When it finds an element which matches one or more directives, it will create a new child injector for that element. It then uses the new child injector to create directives.

This means that you have a hierarchical injector structure which mirrors the DOM structure. When the same directive is created for two different elements, they are created in two different injectors.

However, since directives can request other directives from the injector (say in their constructor), it is possible for two directives to share the same instance of a second if the second directive was created on a shared parent injector.

Directive selectors and significant types

When the compiler is created, it gets a list of all registered directives and their selectors. It does this by asking the DI system for all types annotated with @NgAnnotation. @NgDirective, @NgComponent, @NgController are all subtypes of @NgAnnotation, so they are included in that list.

This is why you may have two types, MyController and MyService, in the same module but only have one, MyController, be used by the compiler.

这篇关于是Dart中的控制器单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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