dioc 0.1.4

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 86

dioc

Inversion of control based on dependency injection through containers.

Usage

A simple usage example:

final container = Container();
container.register<Service>((c) => MockService());
container.register<Service>((c) => Controller(c.create(Service)));

final created = container.create<Controller>();
final singleton = container.singleton<Controller>();

You can also name registrations if multiple instances or factories of the same type are needed. The default behaviour when registering is InjectMode.Singleton (the instance will be resolved on first access and returned on successive calls).

final container = Container();
container.register<Service>((c) => WebService());
container.register<Service>((c) => MockService(), name : "demo");

final web = container<Service>(); 
final demo = container<Service>(creator: "demo");

The default get behaviour can be changed at registration time with defaultMode :

final container = Container();
container.register<Service>((c) => WebService(), defaultMode: InjectMode.Create);

final web = container<Service>(); 
final web2 = container<Service>(); // A new instance is created on each call.

It is also possible to call explicitely singleton or create method for a specific resolution.

final container = Container();
container.register<Service>((c) => WebService());

final web = container.singleton<Service>();
final web2 = container.singleton<Service>();
final web3 = container.create<Service>();
// web == web2 but web != web3

If you want to reset your container use the unregister or reset methods.

Code generation

A dioc_generator package is also available for simplifying injections based on constructor analysis.

Notes

All dependency resolution isn't based on mirrors because it is intended to be used with strong mode.

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

0.0.1

  • Initial version, created by Stagehand

example/dioc_example.dart

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() {

}

Use this package as a library

1. Depend on it

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


dependencies:
  dioc: ^0.1.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:dioc/dioc.dart';
  
Version Uploaded Documentation Archive
0.1.4 Nov 3, 2018 Go to the documentation of dioc 0.1.4 Download dioc 0.1.4 archive
0.1.2 Oct 16, 2018 Go to the documentation of dioc 0.1.2 Download dioc 0.1.2 archive
0.1.1 Sep 5, 2018 Go to the documentation of dioc 0.1.1 Download dioc 0.1.1 archive
0.1.0 Sep 5, 2018 Go to the documentation of dioc 0.1.0 Download dioc 0.1.0 archive
0.0.1 Jan 24, 2018 Go to the documentation of dioc 0.0.1 Download dioc 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
72
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]
86
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

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

Health suggestions

Format lib/src/built_container.dart.

Run dartfmt to format lib/src/built_container.dart.

Format lib/src/container.dart.

Run dartfmt to format lib/src/container.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >2.0.0-dev <3.0.0
Dev dependencies
test any