diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index 37928e6..e494f46 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -68,3 +68,8 @@ class NewInstanceScope : RegistrationScope { return instantiatableType.create(); } } + +public Registration newInstance(Registration registration) { + registration.registationScope = new NewInstanceScope(registration.instantiatableType); + return registration; +} diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index a1fb291..c5db07a 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -103,4 +103,13 @@ version(unittest) { assert(instance1 is instance2); } + // Test resolve new instance for type + unittest { + auto container = new Container(); + container.register!(TestClass)().newInstance(); + auto instance1 = container.resolve!(TestClass); + auto instance2 = container.resolve!(TestClass); + assert(instance1 !is instance2); + } + } \ No newline at end of file diff --git a/test/poodinis/registrationtest.d b/test/poodinis/registrationtest.d index 136939b..ce33aac 100644 --- a/test/poodinis/registrationtest.d +++ b/test/poodinis/registrationtest.d @@ -41,4 +41,14 @@ version(unittest) { assert(instance1 !is instance2, "Registration with new instance scope did not return a different instance"); } + // Test set new instance scope using scope setter + unittest { + Registration registration = new Registration(null, typeid(TestType)); + auto chainedRegistration = registration.newInstance(); + auto instance1 = registration.getInstance(); + auto instance2 = registration.getInstance(); + assert(instance1 !is instance2, "Registration with new instance scope did not return a different 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