AngualrDart&Flutter 应用控制器
AppController for AngualrDart&Flutter
我正在 AngularDart
和 flutter
中编写一个项目,它们共享通用代码。我有AppController
,它将处理所有逻辑业务代码,在大多数组件中使用:
class AppController {
AppController(this.serviceOne, this.serviceTwo...);
}
现在写AngularDart
。我有两个选择:
- 使用 DI,将 AppController、serviceOne、serviceTwo 作为提供者传递给 bootstrap。我不确定是否应该将这些 类 标记为可注射。而且我听说 flutter 不太支持 injectable。我是依赖注入的新手,不知道如何实现它。
代码 1.1:
bootstrap(AppComponent, [
new Provider(
AppController,
useValue: new AppController(
new ServiceA(),
new ServiceB(),
....
),
),
]);
代码 1.2:
bootstrap(AppComponent, [
AppController,
ServiceA,
ServiceB,
]);
代码 1.3:
// From Günter Zöchbauer's answer
createAppFactory(ServiceA sa, ServiceB sb) => new AppController(sa, sb);
bootstrap(AppComponent, [
new Provider<AppController>(
useFactory: createAppFactory,
deps: [ServiceA, ServiceB]
),
]);
- 作为
@input
逐级传递到 AppComponent 和子组件。这很简单,但看起来不优雅。
class AppComponent(AppController controller) {
}
class subComponent() {
@input
AppController _controller
}
- 其他更好的选择?
而且我也在想,如果我们使用appController来处理业务逻辑层,让平台代码只实现widget。服务在公共代码中定义。我可以只在 appController 中创建服务并且不要让平台代码接触它吗?换句话说,平台代码仅通过appController使用公共代码,而服务是在appController内部创建的。
如果添加 @Injectable()
注解,则无法再在 Flutter 中使用代码,因为它通过 Angular.
将代码绑定到 dart:html
您需要在没有 @Injectable()
的情况下创建服务,然后为 Angular 添加带有注释的包装器。
在shared_code/lib/foo_service.dart
class FooService {
BarService bar;
FooService(this.bar);
}
在angular_code/lib/foo_service.dart
import 'package:shared_code/foo_service.dart' as shared;
@Injectable()
class FooService extends shared.FooService {
FooService(BarService bar) : super(bar);
}
我好像看到它提到 Angular 团队正在讨论他们是否可以摆脱 @Injectable()
。这会让这件事变得容易得多。
另一种方法是使用工厂提供者不需要 @Injectable()
FooService createFooService(BarService bar) => new FooService(bar);
FooService createBarService() => new BarService();
providers: const [
const FactoryProvider(FooService, createFooService, deps: const [BarService]),
const FactoryProvider(BarService, createBarService)]
我正在 AngularDart
和 flutter
中编写一个项目,它们共享通用代码。我有AppController
,它将处理所有逻辑业务代码,在大多数组件中使用:
class AppController {
AppController(this.serviceOne, this.serviceTwo...);
}
现在写AngularDart
。我有两个选择:
- 使用 DI,将 AppController、serviceOne、serviceTwo 作为提供者传递给 bootstrap。我不确定是否应该将这些 类 标记为可注射。而且我听说 flutter 不太支持 injectable。我是依赖注入的新手,不知道如何实现它。
代码 1.1:
bootstrap(AppComponent, [
new Provider(
AppController,
useValue: new AppController(
new ServiceA(),
new ServiceB(),
....
),
),
]);
代码 1.2:
bootstrap(AppComponent, [
AppController,
ServiceA,
ServiceB,
]);
代码 1.3:
// From Günter Zöchbauer's answer
createAppFactory(ServiceA sa, ServiceB sb) => new AppController(sa, sb);
bootstrap(AppComponent, [
new Provider<AppController>(
useFactory: createAppFactory,
deps: [ServiceA, ServiceB]
),
]);
- 作为
@input
逐级传递到 AppComponent 和子组件。这很简单,但看起来不优雅。
class AppComponent(AppController controller) {
}
class subComponent() {
@input
AppController _controller
}
- 其他更好的选择?
而且我也在想,如果我们使用appController来处理业务逻辑层,让平台代码只实现widget。服务在公共代码中定义。我可以只在 appController 中创建服务并且不要让平台代码接触它吗?换句话说,平台代码仅通过appController使用公共代码,而服务是在appController内部创建的。
如果添加 @Injectable()
注解,则无法再在 Flutter 中使用代码,因为它通过 Angular.
dart:html
您需要在没有 @Injectable()
的情况下创建服务,然后为 Angular 添加带有注释的包装器。
在shared_code/lib/foo_service.dart
class FooService {
BarService bar;
FooService(this.bar);
}
在angular_code/lib/foo_service.dart
import 'package:shared_code/foo_service.dart' as shared;
@Injectable()
class FooService extends shared.FooService {
FooService(BarService bar) : super(bar);
}
我好像看到它提到 Angular 团队正在讨论他们是否可以摆脱 @Injectable()
。这会让这件事变得容易得多。
另一种方法是使用工厂提供者不需要 @Injectable()
FooService createFooService(BarService bar) => new FooService(bar);
FooService createBarService() => new BarService();
providers: const [
const FactoryProvider(FooService, createFooService, deps: const [BarService]),
const FactoryProvider(BarService, createBarService)]