dioc 0.0.1

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

dioc

Inversion of control based on dependency injection through containers.

Usage

A simple usage example:

final container = new Container();
container.register(Service, (c) => new MockService());
container.register(Controller, (c) => new 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 = new Container();
container.register(Service, (c) => new WebService());
container.register(Service, (c) => new MockService(), name : "demo");

final web = container.create(Service);
final demo = container.create(Service, factory: "demo");

It is also possible to have multiple named singleton instances :

final container = new Container();
container.register(Service, (c) => new WebService());
container.register(Service, (c) => new 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 = new Container();
    container.register(OtherService, (c) => new OtherService(c.singleton(Service)));
    return container;
  }

  Container development() {
    final container = base();
    container.register(Service, (c) => new MockService());
    return container;
  }

  Container production() {
    final container = base();
    container.register(Service, (c) => new WebService());
    return container;
  }
}

class AppBootsrapperBuilder  {
  static final AppBootsrapper instance = build();
  static AppBootsrapper build() => new _AppBootsrapper();
}

// Main

main() {

}

1. Depend on it

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


dependencies:
  dioc: "^0.0.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 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.0.1 Jan 24, 2018 Go to the documentation of dioc 0.0.1 Download dioc 0.0.1 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
14 / 100
Health:
Code health derived from static analysis. [more]
97 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
56

Platforms

Detected platforms: Flutter, web, other

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

Suggestions

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0
Dev dependencies
test ^0.12.0