diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 74d9c07..be6f446 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -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(); + } } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index fa07360..ac86cfb 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -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"); + } } \ No newline at end of file