scope 2.0.0 copy "scope: ^2.0.0" to clipboard
scope: ^2.0.0 copied to clipboard

outdated

Dependency injection for Dart

example/main.dart

/// Example of how a server side framework might use this package to pass
/// configuration and dependencies around via dependency injection.
///
/// Class names borrowed from [Aqueduct](https://aqueduct.io).
library scope.example;

import 'package:scope/scope.dart';

//
// Persistence layer interface
//

final persistentStoreScopeKey =
    ScopeKey<PersistentStore>('zone_di.example.persistentStoreScopeKey');

abstract class PersistentStore {}

//
// Postgres implementation of persistence layer
//

final databaseCredentialsScopeKey = ScopeKey<DatabaseConfiguration>(
    'zone_di.example.databaseCredentialsScopeKey');

class DatabaseConfiguration {
  DatabaseConfiguration(this.username, this.password);
  final String username;
  final String password;
}

class PostgreSQLPersistentStore implements PersistentStore {
  PostgreSQLPersistentStore() {
    final credentials = use(databaseCredentialsScopeKey);
    connect(credentials.username, credentials.password);
  }

  void connect(String username, String password) {/* ... */}
}

//
// Bootstrapping
//

class App {
  App() : persistentStore = use(persistentStoreScopeKey);

  final PersistentStore? persistentStore;

  void run() {/* ... */}
}

// void main() {
//   provideFactories({
//     persistentStoreScopeKey: () => PostgreSQLPersistentStore(),
//     databaseCredentialsScopeKey: () =>
//         DatabaseConfiguration('pschiffmann', 'dolphins')
//   }, () {
//     App().run();
//   });
// }
11
likes
0
pub points
99%
popularity

Publisher

verified publisheronepub.dev

Dependency injection for Dart

Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

Dependencies

meta

More

Packages that depend on scope