Add resolving of registered types

This commit is contained in:
Mike Bierlee 2014-05-10 14:52:29 +02:00
parent 20e78b8d93
commit 5df222f509
2 changed files with 17 additions and 2 deletions

View file

@ -2,18 +2,26 @@ module poodinis.container;
struct Registration {
TypeInfo_Class registratedType = null;
public Object getInstance() {
return registratedType.create();
}
}
class Container {
private static Registration[] registrations;
private static Registration[TypeInfo_Class] registrations;
private this() {
}
public static Registration register(T)() {
Registration newRegistration = { typeid(T) };
registrations ~= newRegistration;
registrations[newRegistration.registratedType] = newRegistration;
return newRegistration;
}
public static T resolve(T)() {
return cast(T) registrations[typeid(T)].getInstance();
}
}

View file

@ -12,4 +12,11 @@ version(unittest) {
assert(registration.registratedType == typeid(TestClass), "Type of registered type not the same");
}
unittest {
// Test resolve registered type
Container.register!(TestClass)();
TestClass actualInstance = Container.resolve!(TestClass)();
assert(actualInstance !is null, "Resolved type is null");
assert(typeid(actualInstance) == typeid(TestClass), "Resolved class is not the same type as expected");
}
}