mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Change reference into class, makes it more easily passed as reference
This commit is contained in:
parent
64e25715cf
commit
2bced68e43
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in a new issue