dioc 0.1.4 dioc: ^0.1.4 copied to clipboard
Inversion of control based on dependency injection through containers.
import 'dart:async';
import 'package:dioc/dioc.dart';
abstract class Service {
Future<String> getContent(String id);
}
class MockService implements Service {
@override
Future<String> getContent(String id) async => "TEST";
}
class OtherService {
final Service dependency;
OtherService(this.dependency);
}
class WebService implements Service {
@override
Future<String> getContent(String id) async => "TEST";
}
@Provide.implemented(OtherService)
abstract class AppBootstrapper extends Bootstrapper {
@Provide(Service, MockService)
Container development();
@Provide(Service, WebService)
Container production();
}
/// Could be generated by <dioc_generator>
class _AppBootstrapper implements AppBootstrapper {
Container base() {
final container = Container();
container.register<OtherService>((c) => OtherService(c.singleton<Service>()));
return container;
}
Container development() {
final container = base();
container.register<Service>((c) => MockService());
return container;
}
Container production() {
final container = base();
container.register<Service>((c) => WebService());
return container;
}
}
class AppBootstrapperBuilder {
static final AppBootstrapper instance = build();
static AppBootstrapper build() => _AppBootstrapper();
}
// Main
main() {
}