Change reference into class, makes it more easily passed as reference

This commit is contained in:
Mike Bierlee 2014-05-20 23:06:27 +02:00
parent 64e25715cf
commit 2bced68e43
3 changed files with 12 additions and 8 deletions

View file

@ -44,9 +44,9 @@ class Container {
checkValidity!(InterfaceType)(registeredType, instantiatableType);
}
Registration newRegistration = { registeredType, instantiatableType };
Registration newRegistration = new Registration(registeredType, instantiatableType);
newRegistration.singleInstance();
registrations[newRegistration.registeredType] = newRegistration;
registrations[registeredType] = newRegistration;
return newRegistration;
}

View file

@ -1,10 +1,15 @@
module poodinis.registration;
struct Registration {
class Registration {
TypeInfo registeredType = null;
TypeInfo_Class instantiatableType = null;
RegistrationScope registationScope = null;
this(TypeInfo registeredType, TypeInfo_Class instantiatableType) {
this.registeredType = registeredType;
this.instantiatableType = instantiatableType;
}
public Object getInstance() {
if (registationScope is null) {
throw new NoScopeDefinedException(registeredType);
@ -47,7 +52,7 @@ class SingleInstanceScope : RegistrationScope {
}
}
public Registration singleInstance(ref Registration registration) {
public Registration singleInstance(Registration registration) {
registration.registationScope = new SingleInstanceScope(registration.instantiatableType);
return registration;
}

View file

@ -8,14 +8,14 @@ version(unittest) {
// Test getting instance without scope defined throws exception
unittest {
Registration registration = Registration();
Registration registration = new Registration(null, null);
registration.registeredType = typeid(TestType);
assertThrown!(NoScopeDefinedException)(registration.getInstance());
}
// Test getting instance from single instance scope
unittest {
Registration registration = Registration();
Registration registration = new Registration(null, null);
registration.registationScope = new SingleInstanceScope(typeid(TestType));
auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance();
@ -24,8 +24,7 @@ version(unittest) {
// Test set single instance scope using scope setter
unittest {
Registration registration = Registration();
registration.instantiatableType = typeid(TestType);
Registration registration = new Registration(null, typeid(TestType));
auto chainedRegistration = registration.singleInstance();
auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance();