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); checkValidity!(InterfaceType)(registeredType, instantiatableType);
} }
Registration newRegistration = { registeredType, instantiatableType }; Registration newRegistration = new Registration(registeredType, instantiatableType);
newRegistration.singleInstance(); newRegistration.singleInstance();
registrations[newRegistration.registeredType] = newRegistration; registrations[registeredType] = newRegistration;
return newRegistration; return newRegistration;
} }

View file

@ -1,10 +1,15 @@
module poodinis.registration; module poodinis.registration;
struct Registration { class Registration {
TypeInfo registeredType = null; TypeInfo registeredType = null;
TypeInfo_Class instantiatableType = null; TypeInfo_Class instantiatableType = null;
RegistrationScope registationScope = null; RegistrationScope registationScope = null;
this(TypeInfo registeredType, TypeInfo_Class instantiatableType) {
this.registeredType = registeredType;
this.instantiatableType = instantiatableType;
}
public Object getInstance() { public Object getInstance() {
if (registationScope is null) { if (registationScope is null) {
throw new NoScopeDefinedException(registeredType); 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); registration.registationScope = new SingleInstanceScope(registration.instantiatableType);
return registration; return registration;
} }

View file

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