lost_dart 1.0.9

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 29

##Lost Dart

Lost Dart is lightweight dependency injection framework for Dart client and server applications.

It helps you split your application into a collection of loosely-coupled pieces and then glue them back together in a flexible manner. Its aim is to make wide range of Dart applications easier to:

  • Manage source code;
  • Encouraging code modularisation;
  • Separations of concerns;
  • Good unit testing practices.

###Set up

Lost Dart is available under the lost_dart pub package. Simply add a dependency to your pubspec.yaml file:

...
dependencies:
  lost_dart: any

Then run pub install and you will have everything you need to get started.

##The core concepts of Lost Dart by examples

###Instantiation

A simple example of using the Lost Dart is that to construct an instance of an object.

import 'package:lost_dart/lost_dart.dart';

// Class Baz
class Baz {
  String name;

  Baz([this.name = ""]);
}

void main() {
  // Create container
  Container container = new Container();

  // Bind Baz
  container.bind(Baz);
  container.bindAs("baz2").to(Baz);

  // Resolve baz by type
  Baz baz = container.get(Baz);
  Baz baz2 = container.getAs("baz2");

  assert(baz.name == "");
  assert(baz2.name == "");
}

###Inheritance

You may get an instance of an interface via implementation class:

import 'package:lost_dart/lost_dart.dart';

// Class Baz
abstract class Baz {
  Baz(String name);
  
  String doBaz(int baz);
}

class Bar implements Baz {
  String name;
  
  Bar(this.name);
  
  String doBaz(int baz) {
    return name += ' ' + baz.toString();
  }
}

void main() {
  // Create container
  Container container = new Container();

  // Bind Baz with Bar implementation
  container.bind(Baz).to(Bar).addConstructorConstArg('Baz');

  // Resolve baz by type
  Baz baz = container.get(Baz);

  assert(baz.doBaz(1) == "Baz 1");
}

###Constructor injection: Constant arguments

Constant values are set an constructor argument:

import 'package:lost_dart/lost_dart.dart';

// Class Baz
class Baz {
  String name;
  int number = 22;
  
  Baz([this.name = ""]);
}

void main() {
  // Create IoC container
  Container container = new Container();
  // Bind Baz
  container.bind(Baz).addConstructorConstArg("Test");
  container.bindAs("baz2").to(Baz).addConstructorConstArg("Test2");

  // Resolve baz
  Baz baz = container.get(Baz);
  Baz baz2 = container.getAs("baz2");

  // Test result
  assert(baz.name == "Test");
  assert(baz2.name == "Test2");
}

###Constructor injection: Dependencies

A tree of dependencies are instantiated and injected using the constructor argument:

import 'package:lost_dart/lost_dart.dart';

// application code
class Baz {
  String name;
  int number = 22;

  Baz([this.name = "99"]);
}

class Bar {
  Baz baz;
  Bar(this.baz);
}


void main() {
  // Create IoC container
  Container container = new Container();

  container.bind(Baz);
  container.bind(Bar).addConstructorTypeArg(Baz);
  container.bindAs("bar2").to(Bar).addConstructorTypeArg(Baz);

  // Resolve bar
  Bar bar = container.get(Bar);
  Bar bar2 = container.getAs("bar2");

  // Test result of bar
  assert(bar != null);
  assert(bar.baz.name == "99");
  assert(bar2 != null);
  assert(bar2.baz.name == "99");
  assert(bar.baz == bar.baz);
}

###Property injection: Constant values

Constant values are set in the property:

import 'package:lost_dart/lost_dart.dart';

// application code
class Baz {
  String name;
  int number = 22;

  Baz([this.name = "99"]);
}

void main() {
  // Create IoC container
  Container container = new Container();

  // Bind Baz
  container.bind(Baz).setConstProperty("number", 33);
  container.bindAs("baz2").to(Baz).setConstProperty("number", 44);

  // Resolve baz
  Baz baz = container.get(Baz);
  Baz baz2 = container.getAs("baz2");
  
  // Test result
  assert(baz.number == 33);
  assert(baz2.number == 44);
}

###Property injection: Dependencies

A tree of dependencies are instantiated and injected using the property:

import 'package:lost_dart/lost_dart.dart';

// application code
class Baz {
  String name;

  Baz([this.name = "99"]);
}

class Bar {
  Baz baz;

  Bar();
}

void main() {

  // Create IoC container
  Container container = new Container();

  // Bind Baz
  container.bind(Baz);

  // Bind Bar
  container.bind(Bar).setTypeProperty("baz", Baz);
  container.bindAs("bar2").to(Bar).setTypeProperty("baz", Baz);

  // Resolve bar
  Bar bar = container.get(Bar);
  Bar bar2 = container.getAs("bar2");
  
  // Test result
  assert(bar.baz.name == bar2.baz.name);
}

###Factory methods

The Factory methods have maximum flexibility to generate any type of injected values:

import 'package:lost_dart/lost_dart.dart';

//application code
class Network {
  String uri;
  
  Network(this.uri);
}

class User {
  String name;
}

class Manager {
  Network network;
  User user;

  Manager(this.user);
}

void main() {
  // Create IoC container
  Container container = new Container();

  // Add host
  container.bindAs("host").toFactory((){
    return "http://127.0.0.1";
  });
  
  // Add Network
  container.bind(Network).addConstructorRefArg("host");
  
  // Add User
  container.bind(User).setConstProperty("name", "Admin");
  
  // Add Manager
  container.bind(Manager).toFactory((){
    Manager bar = new Manager(container.get(User));
    bar.network = container.get(Network);
    return bar;
  });
  
  // Resolve Manager
  Manager manager = container.get(Manager);

  // Test result
  assert(manager.network.uri == "http://127.0.0.1");
  assert(manager.user.name == "Admin");
}

###Object scopes

Lost Dart supports two scopes: singleton and prototype. The first is used by default and does not need to be explicitly specified in binding. You may mark an object definition as prototype with method asPrototype and Container will return a new instance of prototype on each call of method get:

import 'package:lost_dart/lost_dart.dart';

//application code
class Baz {}

void main() {
  // Create IoC container
  Container container = new Container();
  
  // Add Baz
  container.bind(Baz).asPrototype();
  
  // Resolve baz - "prototype" scope.
  Baz baz = container.get(Baz);
  Baz baz1 = container.get(Baz);
  // Test prototype result
  assert(baz != baz1);
}

###Batch binding

To add an array of types in a Container you can use the bindAll method:

import 'package:lost_dart/lost_dart.dart';

class Baz {}

class Bar {}

class Foo {}

void main() {
  Container container = new Container();

  container.bindAll([Baz, Bar, Foo]);

  Foo foo = container.get(Foo);

  assert(foo != null);
}

###Multi injection

Lost Dart allows you to inject multiple objects bound to a particular type or interface. There are Weapon interface, and two implementations Sword and Dagger. The constructor of Samurai class takes an array of Weapon. The toFactory method of Binder class returns the list of Weapons to be used in Samurai constructor:

import 'package:lost_dart/lost_dart.dart';

// Asbtract class Weapon
abstract class Weapon {
   // Hit the [target].
  String Hit(String target);
}

// Sword Weapon
class Sword implements Weapon {
  // Hit the [target].
  String Hit(String target) {
    return "Slice " + target + " in half";
  }
}

// Dagger Weapon
class Dagger implements Weapon {
   // Hit the [target].
  String Hit(String target) {
    return "Stab " + target + " to death";
  }
}

// Samurai fully equipped.
class Samurai {
  List<Weapon> allWeapons;

  Samurai(List<Weapon> this.allWeapons); 

  // Just attack the [target].
  void Attack(String target) {
    for (Weapon weapon in this.allWeapons) {
      print(weapon.Hit(target));
    }
  }
}

void main() {
  Container container = new Container();
  
  // Bind Sword
  container.bind(Sword);
  // Bind Sword
  container.bind(Dagger);
  // Bind weapons as list
  container.bindAs("weapons").toFactory(() {
    return [container.get(Sword), container.get(Dagger)];
  });
  
  
  // Bind Samurai with list of weapons
  container.bind(Samurai).addConstructorRefArg("weapons");
  
  
  // Get samurai 
  Samurai samurai = container.get(Samurai);
  // Atack
  samurai.Attack("your enemy");
}

Lost Dart Changes

###February 23, 2015

  • Migrate project code to follow Dart SDK version 1.8.3.
  • Fix examples in README.md

###April 29, 2013

  • Migrate project code to follow Dart SDK version 1.3.3.
  • Fixed error when Binder does not return his instance in method named.

###December 14, 2013

  • Migrate project code to follow Dart SDK version 1.0.0.10_r30798.

###November 14, 2013

  • Migrate project code to follow Dart SDK version 1.0.0.3_r30188.

###October 9, 2013

  • Migrate project code to follow Dart SDK version 0.8.1.2_r28355.

###October 5, 2013

  • Migrate project code to follow Dart SDK version 0.7.6.4_r28108.

###September 15, 2013

  • Migrate project code to follow Dart SDK version 0.7.2.1_r27268.

###August 28, 2013

  • Migrate project code to follow Dart SDK version 0.6.21.3_r26639

###July 25, 2013

  • Migrate project code to follow Dart SDK version 0.6.9.2_r25388

###July 15, 2013

  • Migrate project code to follow Dart SDK version 0.6.3.3_r24898

###July 1, 2013

  • Library design has been changed and simplified and includes huge number of incompatible changes.

###June 26, 2013

  • Implementation of DI based on JSON configuration.
  • Implemented JSONConfiguration, JSONConfigurationProcessor, JSONObjectDefinition, JSONObjectFactory.
  • DefaultContainerConfiguration and LostDart have been changed.

###June 20, 2013

  • Migrate project code to follow Dart SDK version 0.5.20.2_r24160.

###June 11, 2013

  • Migrate project code to follow Dart SDK version 0.5.16.0_r23799

###June 5, 2013

  • Migrate project code to follow Dart SDK version 0.5.13.1_r23552

###May 30, 2013

  • Migrate project code to follow Dart SDK version 0.5.11.1_r23200

###May 21, 2013

  • Migrate project code to follow Dart SDK version 0.5.9.0_r22879

###May 13, 2013

  • Migrate project code to follow Dart SDK version 0.5.5.0_r22416.

###April 29, 2013

  • Migrate project code to follow Dart SDK version 0.5.0.1_r21823.

###March 31, 2013

  • Migrate project code to follow Dart SDK version 0.4.3.5_r20602

###March 20, 2013

  • Migrate project code to follow Dart SDK version 0.4.2_r20193

###March 2, 2013

  • Migrate project code to follow Dart SDK version 0.4.0.0_r18915

###February 23, 2013

  • Migrate project code to follow Dart SDK version 0.3.7.6_r16717.

###February 6, 2013

  • Migrate project code to follow Dart SDK version 0.3.4.0_r18115.

###January 29, 2013

  • Migrate project code to follow Dart SDK version 0.3.2.0_r17657.

###January 23, 2013

  • Migrate project code to follow Dart SDK version 0.3.1.1_r17328.

###January 22, 2013

  • Removed example folder with content.
  • Removed browser based test enviroument.
  • Prepared to CI tests on Drone.io

###January 19, 2013

  • Code cleaned and follow Dart SDK 0.2.10.1_r16761.

###January 13, 2013

  • Added reference on Browser Library. The lost_dart_test.html pointing now on new dart.js location.
  • Added test_runner.dart to run test execution from Dron.io Continuous Integration web site.

###January 11, 2013

  • Migrate project code to follow Dart SDK version 0.2.10.1_r16761.

###November 27, 2012

  • Just to following Google naming conventions requirements the name of repository and library has been changed from 'lostdart' to 'lost_dart'.

###November 26, 2012

  • Unit tests, code simplification and optimisation.

###November 17, 2012

  • Fixed Scope problem in configuration.

###November 16, 2012

  • Added project documentation.

###November 14, 2012

  • Finished implementation of InCode object definition. Project status moved to version 0.4.

###November 11, 2012

  • Project status moved to version 0.3.

###November 7, 2012

  • Project status moved to version 0.2.

###November 6, 2012

  • Migrate project code to follow Dart SDK version 0.2.2.1_r14458.

###November 4, 2012

  • Initial Draft of project.

###October 10, 2012

  • Prepared base structure of project.

Use this package as a library

1. Depend on it

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


dependencies:
  lost_dart: "^1.0.9"

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:lost_dart/lost_dart.dart';
  
Version Uploaded Documentation Archive
1.0.9 Feb 23, 2015 Go to the documentation of lost_dart 1.0.9 Download lost_dart 1.0.9 archive
1.0.8 Feb 23, 2015 Go to the documentation of lost_dart 1.0.8 Download lost_dart 1.0.8 archive
1.0.7 Jan 13, 2015 Go to the documentation of lost_dart 1.0.7 Download lost_dart 1.0.7 archive
1.0.6 Jan 13, 2015 Go to the documentation of lost_dart 1.0.6 Download lost_dart 1.0.6 archive
1.0.5 Jan 13, 2015 Go to the documentation of lost_dart 1.0.5 Download lost_dart 1.0.5 archive
1.0.4 Apr 29, 2014 Go to the documentation of lost_dart 1.0.4 Download lost_dart 1.0.4 archive
1.0.3 Dec 14, 2013 Go to the documentation of lost_dart 1.0.3 Download lost_dart 1.0.3 archive
1.0.2 Nov 14, 2013 Go to the documentation of lost_dart 1.0.2 Download lost_dart 1.0.2 archive
1.0.1 Nov 14, 2013 Go to the documentation of lost_dart 1.0.1 Download lost_dart 1.0.1 archive
1.0.0 Nov 14, 2013 Go to the documentation of lost_dart 1.0.0 Download lost_dart 1.0.0 archive

All 38 versions...

Analysis

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

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

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

Platforms

Detected platforms: web, other

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

Suggestions

  • Homepage does not exists.

    We were unable to access http://www.lostdart.com at the time of the analysis.

  • Package is too old.

    The package was released more than two years ago.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 10 hints.

    Run dartfmt to format lib/lost_dart.dart.

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

    Similar analysis of the following files failed:

    • lib/src/binder.dart (hint)
    • lib/src/container.dart (hint)
    • lib/src/object_definition.dart (hint)
    • lib/src/object_factory.dart (hint)
    • lib/src/scope.dart (hint)
  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use lost_dart.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.6.0 <2.0.0
Dev dependencies
unittest >=0.11.5 <2.0.0