diff --git a/ngdart/lib/src/meta/di_arguments.dart b/ngdart/lib/src/meta/di_arguments.dart index 5900f05ed..740e5a699 100644 --- a/ngdart/lib/src/meta/di_arguments.dart +++ b/ngdart/lib/src/meta/di_arguments.dart @@ -7,22 +7,26 @@ import '../di/injector.dart'; /// ## Example /// /// ``` -/// class Engine {} +/// class Engine { +/// const Engine(); +/// } +/// +/// const engine = Engine(); /// -/// @Injectable() /// class Car { +/// Car(@Inject(Engine) this.engine); +/// /// final Engine engine; -/// Car(@Inject("MyEngine") this.engine); /// } /// -/// var engine = Engine(); -/// -/// var injector = Injector.resolveAndCreate([ -/// Provider("MyEngine", useValue: engine), -/// Car -/// ]); +/// @GenerateInjector([ +/// ValueProvider(Engine, engine), +/// ClassProvider(Car), +/// ]) +/// final InjectorFactory injector = ng.injector$Injector; /// -/// expect(injector.get(Car).engine, same(engine)); +/// Car car = injector(root).get(Car); +/// expect(car.engine, same(engine)); /// ``` /// /// When `@Inject()` is not present, [Injector] will use the type annotation of @@ -33,13 +37,18 @@ import '../di/injector.dart'; /// ``` /// class Engine {} /// -/// @Injectable() /// class Car { /// Car(Engine engine) {} // same as Car(@Inject(Engine) Engine engine) /// } /// -/// var injector = Injector.resolveAndCreate([Engine, Car]); -/// expect(injector.get(Car).engine, isInstanceOf()); +/// @GenerateInjector([ +/// ClassProvider(Engine), +/// ClassProvider(Car), +/// ]) +/// final InjectorFactory injector = ng.injector$Injector; +/// +/// Car car = injector(root).get(Car); +/// expect(car.engine, isA()); /// ``` @Target({TargetKind.parameter}) class Inject { @@ -100,7 +109,7 @@ class Inject { /// /// ``` /// // Could be put anywhere DI providers are allowed. -/// const FactoryProvider(MyService, createMyService); +/// FactoryProvider(MyService, createMyService); /// /// // A `Provide` may now use `createMyService` via `useFactory`. /// @Injectable() @@ -115,6 +124,7 @@ class Inject { TargetKind.function, TargetKind.method, }) +@Deprecated('It does nothing but throw on invalid uses.') class Injectable { const Injectable(); } @@ -128,14 +138,16 @@ class Injectable { /// ``` /// class Engine {} /// -/// @Injectable() /// class Car { -/// final Engine engine; +/// final Engine? engine; /// Car(@Optional() this.engine); /// } /// -/// var injector = Injector.resolveAndCreate([Car]); -/// expect(injector.get(Car).engine, isNull); +/// @GenerateInjector([ClassProvider(Car)]) +/// final InjectorFactory injector = ng.injector$Injector; +/// +/// Car car = injector(root).get(Car); +/// expect(car.engine, isNull); /// ``` @Target({TargetKind.parameter}) class Optional { @@ -149,20 +161,31 @@ class Optional { /// ``` /// class Dependency {} /// -/// @Injectable() /// class NeedsDependency { /// final Dependency dependency; /// NeedsDependency(@Self() this.dependency); /// } /// -/// var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]); -/// var nd = inj.get(NeedsDependency); +/// @GenerateInjector([ +/// ClassProvider(Dependency), +/// ClassProvider(NeedsDependency), +/// ]) +/// final InjectorFactory injector = ng.injector$Injector; +/// +/// NeedsDependency needsDependency = injector(root).get(NeedsDependency); +/// expect(needsDependency.dependency, isA()); +/// +/// @GenerateInjector([ +/// ClassProvider(Dependency), +/// ]) +/// final InjectorFactory parent = ng.parent$Injector; /// -/// expect(nd.dependency, new isInstanceOf()); +/// @GenerateInjector([ +/// ClassProvider(NeedsDependency), +/// ]) +/// final InjectorFactory child = ng.child$Injector; /// -/// inj = Injector.resolveAndCreate([Dependency]); -/// var child = inj.resolveAndCreateChild([NeedsDependency]); -/// expect(() => child.get(NeedsDependency), throws); +/// expect(() => child(parent(root)).get(NeedsDependency), throwsA(anything)); /// ``` @Target({TargetKind.parameter}) class Self { @@ -177,19 +200,31 @@ class Self { /// ``` /// class Dependency {} /// -/// @Injectable() /// class NeedsDependency { /// final Dependency dependency; /// NeedsDependency(@SkipSelf() this.dependency); /// } /// -/// var parent = Injector.resolveAndCreate([Dependency]); -/// var child = parent.resolveAndCreateChild([NeedsDependency]); -/// expect(child.get(NeedsDependency).dependency, new -/// isInstanceOf()); +/// @GenerateInjector([ +/// ClassProvider(Dependency), +/// ]) +/// final InjectorFactory parent = ng.parent$Injector; +/// +/// @GenerateInjector([ +/// ClassProvider(NeedsDependency), +/// ]) +/// final InjectorFactory child = ng.child$Injector; +/// +/// NeedsDependency needsDependency = child(parent(root)).get(NeedsDependency) +/// expect(needsDependency.dependency, isA()); /// -/// var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]); -/// expect(() => inj.get(NeedsDependency), throws); +/// @GenerateInjector([ +/// ClassProvider(Dependency), +/// ClassProvider(NeedsDependency), +/// ]) +/// final InjectorFactory injector = ng.injector$Injector; +/// +/// expect(() => injector(root).get(NeedsDependency), throwsA(anything)); /// ``` @Target({TargetKind.parameter}) class SkipSelf { @@ -211,43 +246,46 @@ class SkipSelf { /// `HostService` is defined at `ParentCmp`, and `OtherService` is defined at /// `App`. /// -///```dart +///``` /// class OtherService {} /// class HostService {} /// /// @Directive( -/// selector: 'child-directive' +/// selector: 'child-directive', /// ) /// class ChildDirective { /// ChildDirective( -/// @Optional() @Host() OtherService os, -/// @Optional() @Host() HostService hs) { -/// print("os is null", os); -/// print("hs is NOT null", hs); +/// @Optional() @Host() OtherService? os, +/// @Optional() @Host() HostService? hs, +/// ) { +/// print('os is null: $os'); +/// print('hs is NOT null: $hs'); /// } /// } /// /// @Component( /// selector: 'parent-cmp', +/// directives: [ChildDirective], /// providers: [HostService], /// template: ''' /// Dir: /// ''', -/// directives: [ChildDirective] /// ) /// class ParentCmp {} /// /// @Component( /// selector: 'app', +/// directives: [ParentCmp] /// providers: [OtherService], /// template: ''' /// Parent: /// ''', -/// directives: [ParentCmp] /// ) /// class App {} /// -/// bootstrap(App); +/// void main() { +/// runApp(ng.AppNgFactory); +/// } ///``` @Target({TargetKind.parameter}) class Host {