mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add resolving of registered types
This commit is contained in:
parent
20e78b8d93
commit
5df222f509
|
@ -2,18 +2,26 @@ module poodinis.container;
|
||||||
|
|
||||||
struct Registration {
|
struct Registration {
|
||||||
TypeInfo_Class registratedType = null;
|
TypeInfo_Class registratedType = null;
|
||||||
|
|
||||||
|
public Object getInstance() {
|
||||||
|
return registratedType.create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Container {
|
class Container {
|
||||||
|
|
||||||
private static Registration[] registrations;
|
private static Registration[TypeInfo_Class] registrations;
|
||||||
|
|
||||||
private this() {
|
private this() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Registration register(T)() {
|
public static Registration register(T)() {
|
||||||
Registration newRegistration = { typeid(T) };
|
Registration newRegistration = { typeid(T) };
|
||||||
registrations ~= newRegistration;
|
registrations[newRegistration.registratedType] = newRegistration;
|
||||||
return newRegistration;
|
return newRegistration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static T resolve(T)() {
|
||||||
|
return cast(T) registrations[typeid(T)].getInstance();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,11 @@ version(unittest) {
|
||||||
assert(registration.registratedType == typeid(TestClass), "Type of registered type not the same");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue