From 64e25715cf82f1a398016fa431e7dde27739bd83 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Tue, 20 May 2014 22:12:14 +0200 Subject: [PATCH] Add scope setter for single instance scopes --- source/poodinis/container.d | 3 ++- source/poodinis/registration.d | 5 +++++ test/poodinis/registrationtest.d | 12 +++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 64ff6e0..23bb80b 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -44,7 +44,8 @@ class Container { checkValidity!(InterfaceType)(registeredType, instantiatableType); } - Registration newRegistration = { registeredType, instantiatableType, new SingleInstanceScope(instantiatableType) }; + Registration newRegistration = { registeredType, instantiatableType }; + newRegistration.singleInstance(); registrations[newRegistration.registeredType] = newRegistration; return newRegistration; } diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index f4b1a58..e438441 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -46,3 +46,8 @@ class SingleInstanceScope : RegistrationScope { return instance; } } + +public Registration singleInstance(ref Registration registration) { + registration.registationScope = new SingleInstanceScope(registration.instantiatableType); + return registration; +} diff --git a/test/poodinis/registrationtest.d b/test/poodinis/registrationtest.d index 54b10cf..d74781f 100644 --- a/test/poodinis/registrationtest.d +++ b/test/poodinis/registrationtest.d @@ -16,10 +16,20 @@ version(unittest) { // Test getting instance from single instance scope unittest { Registration registration = Registration(); - registration.registeredType = typeid(TestType); registration.registationScope = new SingleInstanceScope(typeid(TestType)); auto instance1 = registration.getInstance(); auto instance2 = registration.getInstance(); assert(instance1 is instance2, "Registration with single instance scope did not return the same instance"); } + + // Test set single instance scope using scope setter + unittest { + Registration registration = Registration(); + registration.instantiatableType = typeid(TestType); + auto chainedRegistration = registration.singleInstance(); + auto instance1 = registration.getInstance(); + auto instance2 = registration.getInstance(); + assert(instance1 is instance2, "Registration with single instance scope did not return the same instance"); + assert(registration is chainedRegistration, "Registration returned by scope setting is not the same as the registration being set"); + } } \ No newline at end of file