Add scope setter for new instance scope

This commit is contained in:
Mike Bierlee 2014-05-20 23:36:33 +02:00
parent 8b5122ad91
commit abdcd39e9e
3 changed files with 24 additions and 0 deletions

View file

@ -68,3 +68,8 @@ class NewInstanceScope : RegistrationScope {
return instantiatableType.create(); return instantiatableType.create();
} }
} }
public Registration newInstance(Registration registration) {
registration.registationScope = new NewInstanceScope(registration.instantiatableType);
return registration;
}

View file

@ -103,4 +103,13 @@ version(unittest) {
assert(instance1 is instance2); 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);
}
} }

View file

@ -41,4 +41,14 @@ version(unittest) {
assert(instance1 !is instance2, "Registration with new instance scope did not return a different instance"); 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");
}
} }