From b78e05455de52dae06650814f53a95234d27c8ba Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sat, 31 May 2014 23:22:29 +0200 Subject: [PATCH] Add removing of registrations --- source/poodinis/container.d | 14 +++++++++----- test/poodinis/containertest.d | 12 ++++++++++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/source/poodinis/container.d b/source/poodinis/container.d index ac5be1f..ee2d530 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -59,22 +59,26 @@ class Container { } } - public ClassType resolve(ClassType)() { - TypeInfo resolveType = typeid(ClassType); + public RegistrationType resolve(RegistrationType)() { + TypeInfo resolveType = typeid(RegistrationType); Registration* registration = resolveType in registrations; if (!registration) { throw new ResolveException("Type not registered.", resolveType); } - ClassType instance = cast(ClassType) registration.getInstance(); - this.autowire!(ClassType)(instance); + RegistrationType instance = cast(RegistrationType) registration.getInstance(); + this.autowire!(RegistrationType)(instance); return instance; } - public void clearRegistrations() { + public void clearAllRegistrations() { registrations.clear(); } + public void removeRegistration(RegistrationType)() { + registrations.remove(typeid(RegistrationType)); + } + public static Container getInstance() { if (instance is null) { instance = new Container(); diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index b5292a8..a10e3e5 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -90,7 +90,7 @@ version(unittest) { unittest { auto container = new Container(); container.register!(TestClass)(); - container.clearRegistrations(); + container.clearAllRegistrations(); assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail"); } @@ -157,5 +157,13 @@ version(unittest) { auto mouse = container.resolve!ComponentMouse; auto cat = container.resolve!ComponentCat; assert(mouse.cat is cat && cat.mouse is mouse, "Circular dependencies should be autowirable"); - } + } + + // Test remove registration + unittest { + auto container = new Container(); + container.register!TestClass; + container.removeRegistration!TestClass; + assertThrown!ResolveException(container.resolve!TestClass); + } } \ No newline at end of file