Created
January 29, 2014 17:22
-
-
Save chrisbu/8692670 to your computer and use it in GitHub Desktop.
Injecting foos and bars with di
This file contains hidden or 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
import 'package:di/di.dart'; | |
import 'package:di/dynamic_injector.dart'; | |
void main() { | |
var modules = [new SimpleModule(), new EnterpriseModule()]; // last one wins! | |
var di = new DynamicInjector(modules:modules); | |
var foo = di.get(Foo); | |
var bar = di.get(Bar); | |
print(foo); | |
print(bar); | |
} | |
class SimpleModule extends Module { | |
SimpleModule() { | |
type(Bar); | |
type(Foo); | |
} | |
} | |
class EnterpriseModule extends Module { | |
EnterpriseModule() { | |
type(Foo); | |
factory(Bar, (_) => new EnterpriseBar()); | |
} | |
} | |
class Bar { | |
static int barCount = 0; | |
Bar() { | |
barCount++; | |
print("Creating bar: $barCount"); | |
} | |
toString() => "I am bar: $barCount"; | |
} | |
class Foo { | |
static int fooCount = 0; | |
Bar bar; | |
Foo(Bar this.bar) { | |
fooCount++; | |
print("Created Foo: $fooCount"); | |
} | |
toString() => "I am a foo: $fooCount. My bar is: $bar"; | |
} | |
class EnterpriseBar implements Bar { | |
static int barCount = 0; | |
EnterpriseBar() { | |
barCount++; | |
print("Created Enterprise Bar: $barCount"); | |
} | |
toString() => "I am a an EnterpriseBar: $barCount. See me ROAR"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment