Add registration scope for existing instances

This commit is contained in:
Mike Bierlee 2014-05-20 23:54:45 +02:00
parent abdcd39e9e
commit bb0a8d5746
2 changed files with 21 additions and 0 deletions

View file

@ -73,3 +73,15 @@ public Registration newInstance(Registration registration) {
registration.registationScope = new NewInstanceScope(registration.instantiatableType); registration.registationScope = new NewInstanceScope(registration.instantiatableType);
return registration; return registration;
} }
class ExistingInstanceScope : RegistrationScope {
Object instance = null;
this(Object instance) {
this.instance = instance;
}
public Object getInstance() {
return instance;
}
}

View file

@ -51,4 +51,13 @@ version(unittest) {
assert(registration is chainedRegistration, "Registration returned by scope setting is not the same as the registration being set"); 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");
}
} }