Skip to content

Instantly share code, notes, and snippets.

@nicholashagen
Created April 30, 2013 20:48
Show Gist options
  • Save nicholashagen/5491828 to your computer and use it in GitHub Desktop.
Save nicholashagen/5491828 to your computer and use it in GitHub Desktop.
Simple Dependency Injection (DI) in Dart. This is my first Dart script and attempt at playing with the language, so there are probably many better ways to do this. I'll tweak this as I learn more.
import 'dart:mirrors';
class System {
static final Map<String, TController> _controllers = new Map();
static final Map<String, TService> _services = new Map();
static TController loadController(Symbol name) {
TController controller = _controllers[name];
if (controller == null) {
MirrorSystem ms = currentMirrorSystem();
for (LibraryMirror lm in ms.libraries.values) {
ClassMirror cm = lm.classes[name];
if (cm != null && TController.isController(cm)) {
InstanceMirror instance = cm.newInstance(new Symbol(''), []);
controller = _inject(instance.reflectee);
_controllers[name] = controller;
}
}
}
return controller;
}
static TService loadService(Symbol name) {
TService service = _services[name];
if (service == null) {
MirrorSystem ms = currentMirrorSystem();
for (LibraryMirror lm in ms.libraries.values) {
ClassMirror cm = lm.classes[name];
if (cm != null && TService.isService(cm)) {
InstanceMirror instance = cm.newInstance(new Symbol(''), []);
service = instance.reflectee;
_services[name] = service;
}
}
}
return service;
}
static TController _inject(TController controller) {
InstanceMirror im = reflect(controller);
ClassMirror cm = im.type;
for (var m in cm.members.values) {
if (m is VariableMirror && m.type is ClassMirror && TService.isService(m.type)) {
im.setField(m.simpleName, loadService(m.type.simpleName));
}
}
return controller;
}
}
class TService {
static final Symbol SELF = new Symbol('TService');
static final Symbol OBJECT = new Symbol('Object');
static bool isService(ClassMirror mirror) {
if (mirror == null || mirror.simpleName == OBJECT) { return false; }
else if (mirror.simpleName == SELF) { return true; }
else if (isService(mirror.superclass)) { return true; }
else {
for (iface in mirror.superinterfaces) {
if (isService(iface)) { return true; }
}
return false;
}
}
}
class TController {
static final Symbol SELF = new Symbol('TController');
static final Symbol OBJECT = new Symbol('Object');
static bool isController(ClassMirror mirror) {
if (mirror == null || mirror.simpleName == OBJECT) { return false; }
else if (mirror.simpleName == SELF) { return true; }
else if (isController(mirror.superclass)) { return true; }
else {
for (iface in mirror.superinterfaces) {
if (isController(iface)) { return true; }
}
return false;
}
}
}
////////////////////////////////////////////////////////////////////////////////
class MyService extends TService {
String getGreeting() {
return "Hello";
}
}
class MyController extends TController {
MyService myService;
void sayHello(String user) {
String greeting = myService.getGreeting();
print("${greeting} ${user}");
}
}
/////////////////////////////////////////////////////////////////////////////////
void main() {
MyController c = System.loadController(new Symbol("MyController"));
c.sayHello("World");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment