This repository has been archived by the owner on Dec 6, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
..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). |
I like the new syntax
Do you think it would be possible to do something like
bind(...).toFactory (...)