dioc 0.1.1

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

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 multiples instances or factories of the same type are needed.

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

final web = container.create<Service>();  // or container<Service>() since Container is callable
final demo = container.create<Service>(factory: "demo");

It is also possible to have multiple named singleton instances :

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

final web = container.singleton<Service>();
final demo = container.singleton<Service>(name: "demo", factory: "demo");

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.

Currently, the package is intended to be used with Dart 1, but it will be migrated to Dart 2 very soon to use strong typed generics instead.

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 AppBootsrapper extends Bootsrapper {
  @Provide(Service, MockService)
  Container development();

  @Provide(Service, WebService)
  Container production();
}

/// Could be generated by <dioc_generator>

class _AppBootsrapper implements AppBootsrapper {
  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 AppBootsrapperBuilder  {
  static final AppBootsrapper instance = build();
  static AppBootsrapper build() => _AppBootsrapper();
}

// 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.1

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.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]
71
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]
85
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.4

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