zone_di 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 51

zone_di #

Dependency injection with a functional API that works without code generation, mirrors or passing around Injector objects.

Usage #

The three main exports of this package are Token, provide() and inject(). Use tokens to declare dependencies that can be injected. Then associate values to your tokens with provide(), and read them with inject().

import 'package:zone_di/zone_di.dart';

final fooToken = Token<String>('my_package.fooToken');

class MyConsumer {
  final String foo;

  MyConsumer() : foo = inject(fooToken).toUpperCase();
}

void main() {
  provide(fooToken, 'Hello world', () {
    final consumer = MyConsumer();
    print(consumer.foo);
  });
}

There's also provideFactories() that constructs token values from the given factory functions and handles dependencies between the factories.

How it works #

To make provide() arguments available in inject(), this package uses zones. That means it can avoid global mutable state and the edge cases that typically come with it. For a longer explanation, I wrote a blog post about it here.

CHANGELOG #

1.0.0 #

Initial release.

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 zone_di.example;

import 'package:zone_di/zone_di.dart';

//
// Persistence layer interface
//

final persistentStoreToken =
    Token<PersistentStore>('zone_di.example.persistentStoreToken');

abstract class PersistentStore {}

//
// Postgres implementation of persistence layer
//

final databaseCredentialsToken =
    Token<DatabaseConfiguration>('zone_di.example.databaseCredentialsToken');

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

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

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

//
// Bootstrapping
//

class App {
  App() : persistentStore = inject(persistentStoreToken);

  final PersistentStore persistentStore;

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

void main() {
  provideFactories({
    persistentStoreToken: () => PostgreSQLPersistentStore(),
    databaseCredentialsToken: () =>
        DatabaseConfiguration('pschiffmann', 'dolphins')
  }, () {
    final app = App();
    app.run();
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  zone_di: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:zone_di/zone_di.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
3
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
51
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:zone_di/zone_di.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.7 1.1.7
Dev dependencies
pedantic ^1.8.0
test ^1.6.7