xerum 0.1.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 30

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]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
30
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: web, other

Primary library: package:xerum/xerum.dart with components: mirrors.

Health suggestions

Format lib/src/bindings/internal_bindings.dart.

Run dartfmt to format lib/src/bindings/internal_bindings.dart.

Format lib/src/xerum_annotations.dart.

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

Format lib/src/xerum_exceptions.dart.

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

Format lib/xerum.dart.

Run dartfmt to format lib/xerum.dart.

Maintenance issues and suggestions

Add SDK constraint in pubspec.yaml. (-5 points)

For information about setting SDK constraint, please see https://www.dartlang.org/tools/pub/pubspec#sdk-constraints.

Package is getting outdated. (-80 points)

The package was released 93 weeks ago.

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use xerum.dart.