xerum 0.1.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 0

xerum #

IoC / DI framework for Dart #

This is a lightweight, mirrors-bassed, DI framework for any kind of Dart application, with support for both singleton and transient instances, as well as a couple of extra-perks.

Installing xerum #

From Pub

dependencies:
  xerum: any

From GitHub (master) - most recent stable builds

dependencies:
  xerum:
    git: git://github.com/UizzUW/xerum.git

From Github (dev) - bleeding edge builds

dependencies:
  xerum:
    git:
      url: git://github.com/UizzUW/xerum.git
      ref: dev

Usage #

Basic IoC registering / resolving #

// contract
abstract class IFuel {
  int quantity;
  String get type;
}

// concrete
class Gasoline implements IFuel {
  @override
  int quantity = 50;
  
  @override
  String get type => "gasoline";
}

// register and resolve

main() {
    var xerum = new XerumContainer();
    xerum.register(new XerumBinding<IFuel, Gasoline>());
    print(xerum.resolve(IFuel)).type);
}

Transient vs Singleton #

Transient

main() {
    var xerum = new XerumContainer();
    xerum.register(new XerumBinding<IFuel, Gasoline>());

    var fuel = xerum.resolve(IFuel);
    print(fuel.quantity); // 50
    fuel.quantity--;

    var fuel = xerum.resolve(IFuel);
    print(fuel.quantity); // different instance so still 50
}

Singleton

main() {
    var xerum = new XerumContainer();
    xerum.register(new XerumBinding<IFuel, Gasoline>(), asSingleton: true);

    var fuel = xerum.resolve(IFuel);
    print(fuel.quantity); // 50
    fuel.quantity--;

    var fuel = xerum.resolve(IFuel);
    print(fuel.quantity); // same instance, so 49
}

Specific constructor injection #

Out of the box, xerum will use reflection on your concretes to scan all constructors and see if it can find one to use based on the already-registered services (or throw an exception if it doesn't find any).

While it is not required for the process to work, if you would like to have multiple constructors on your concretes for some reason, you can speed up the registration process by annotating the constructor you would like to be used by xerum with @xerumInject. Keep in mind that this only speeds up the registration process. When you resolve a service, xerum already has a reference (if any) to the required constructor.

// contract
abstract class IEngine {
  void go();
}

// concrete with no specific constructor
class ElectricEngine implements IEngine {
  IFuel _fuel;

  ElectricEngine(this._fuel);

  @override
  void go() {
    print("Hum... just hum I guess.");
  }
}

// concrete with specific constructor
class V8Engine implements IEngine {
  IFuel _fuel;

  @xerumInject
  V8Engine(this._fuel);

  @override
  void go() {
    print("Vroom, here comes dat ${_fuel.type}!!");
  }
}

Multiple containers support #

xerum can create as many containers as your RAM can hold. Each individual container has its own dependencies tree and service instances.

main() {
    var xerum1 = new XerumContainer();
    var xerum2 = new XerumContainer();
    xerum1.register(new XerumBinding<IFuel, Gasoline>(), asSingleton: true);
    xerum2.register(new XerumBinding<IFuel, Gasoline>(), asSingleton: true);
    print(identical(xerum1.resolve(IFuel), xerum2.resolve(IFuel))); // false
}

[Experimental] Replacing registered instances

main() {
  var xerum = new XerumContainer();
  xerum.register(new XerumBinding<IFuel, Gasoline>(), asSingleton: true);
  xerum.register(new XerumBinding<IEngine, V8Engine>());
  xerum.register(new XerumBinding<ICar, GenericCar>());
  xerum.resolve(ICar).drive(); // Vroom, here comes dat gasoline!!
  xerum.register(new XerumBinding<IEngine, ElectricEngine>(), replace: true);
  xerum.resolve(ICar).drive(); // Hum... just hum I guess.
}

Changelog #

0.1.0 #

Initial release

Use this package as a library

1. Depend on it

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


dependencies:
  xerum: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:xerum/xerum.dart';
  
Version Uploaded Documentation Archive
0.1.1 Dec 22, 2016 Go to the documentation of xerum 0.1.1 Download xerum 0.1.1 archive
0.1.0 Dec 1, 2016 Go to the documentation of xerum 0.1.0 Download xerum 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.