Are the Controllers in Dart singleton

僤鯓⒐⒋嵵緔 提交于 2019-12-12 18:32:57

问题


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.


回答1:


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.




回答2:


Angular 2 Dart

DI maintains a single instance per provider.

DI looks from the providers of a component that requests a dependency (constructor parameter) to the providers of the parent component, then the parent of this parent, up to the root component, and then further to the providers added at bootstrap(AppComponent, [/* providers */]) and the providers provided by Angular itself.

The instance of the first provider found this way is injected.

If there are more providers, there are more instances.

If there are providers registered in @Component(...), then there is a different instance for each provider on every component instance.

Angular 1 Dart

From the same injector you get the same instance every time when you request an instance of a specific type.
But in angular the injectors are hierarchical (similar to scope).
When you request a type it is looked up the hierarchy upwards for an injector that has that type registered and this injector returns the same instance every time.
If you apply the same selector for your controller to multiple DOM elements you get a new controller instance each time because there gets a new injectors instance created which gets some types registered like the controller and the element the controller was applied to. That injector returns its own instance when you request a type it has registered.



来源:https://stackoverflow.com/questions/22880800/are-the-controllers-in-dart-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!