From b9039e869e6c1a16bba02ddd727c904b778e8706 Mon Sep 17 00:00:00 2001 From: Pavel Jbanov Date: Fri, 25 Apr 2014 21:42:52 -0400 Subject: [PATCH] chore(readme): updated README.md --- README.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7b3c045..2609ba0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,77 @@ [![Build Status](https://drone.io/github.com/angular/di.dart/status.png)](https://drone.io/github.com/angular/di.dart/latest) -# A prototype of Dependency Injection framework for Dart +# Dependency Injection (DI) framework -Influenced by Pico Container, AngularJS DI, Node DI, Guice, Dagger and what not. +## Installation + +Add dependency to your pubspec.yaml. + + dependencies: + di: ">=0.0.39 <0.1.0" + +Then, run `pub install`. + +Import di. + + import 'package:di/di.dart'; + import 'package:di/auto_injector.dart'; + +## Example + +```dart +import 'package:di/di.dart'; +import 'package:di/auto_injector.dart'; + +abstract class Engine { + go(); +} + +class V8Engine implements Engine { + go() { + print('Vroom...'); + } +} + +class ElectricEngine implements Engine { + go() { + print('Hum...'); + } +} + +// Annotation +class Electric { + const Electric(); +} + +class GenericCar { + Engine engine; + + GenericCar(this.engine); + + drive() { + engine.go(); + } +} + +class ElectricCar { + Engine engine; + + ElectricCar(@Electric() this.engine); + + drive() { + engine.go(); + } +} + +void main() { + var injector = defaultInjector(modules: [new Module() + ..bind(GenericCar) + ..bind(ElectricCar) + ..bind(Engine, toFactory: (i) => new V8Engine()) + ..bind(Engine, toImplementation: ElectricEngine, withAnnotation: Electric) + ]); + injector.get(GenericCar).drive(); + injector.get(ElectricCar).drive(); +} +``` -For example usage see [the tests](https://github.com/angular/di.dart/blob/master/test/main.dart).