From bb0a8d57461e38e3f62dff3d0a9c2ecd8959f4bb Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Tue, 20 May 2014 23:54:45 +0200 Subject: [PATCH] Add registration scope for existing instances --- source/poodinis/registration.d | 12 ++++++++++++ test/poodinis/registrationtest.d | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index e494f46..f2ea281 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -73,3 +73,15 @@ public Registration newInstance(Registration registration) { registration.registationScope = new NewInstanceScope(registration.instantiatableType); return registration; } + +class ExistingInstanceScope : RegistrationScope { + Object instance = null; + + this(Object instance) { + this.instance = instance; + } + + public Object getInstance() { + return instance; + } +} diff --git a/test/poodinis/registrationtest.d b/test/poodinis/registrationtest.d index ce33aac..7bb3ad1 100644 --- a/test/poodinis/registrationtest.d +++ b/test/poodinis/registrationtest.d @@ -51,4 +51,13 @@ version(unittest) { assert(registration is chainedRegistration, "Registration returned by scope setting is not the same as the registration being set"); } + // Test getting instance from existing instance scope + unittest { + Registration registration = new Registration(null, null); + TestType expectedInstance = new TestType(); + registration.registationScope = new ExistingInstanceScope(expectedInstance); + auto actualInstance = registration.getInstance(); + assert(expectedInstance is actualInstance, "Registration with existing instance did not return given instance"); + } + } \ No newline at end of file