Skip to content

Instantly share code, notes, and snippets.

@chrisbu
Created January 29, 2014 17:22
Show Gist options
  • Save chrisbu/8692670 to your computer and use it in GitHub Desktop.
Save chrisbu/8692670 to your computer and use it in GitHub Desktop.
Injecting foos and bars with di
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