Remove static context from container

This commit is contained in:
Mike Bierlee 2014-05-20 00:23:25 +02:00
parent da1f604ef7
commit 27a50a53cf
2 changed files with 37 additions and 35 deletions

View file

@ -26,30 +26,27 @@ class ResolveException : Exception {
class Container {
private static Registration[TypeInfo] registrations;
private Registration[TypeInfo] registrations;
private static bool _globalTypeValidityCheckEnabled = true;
private bool _typeValidityCheckEnabled = true;
@property public static void globalTypeValidityCheckEnabled(bool enabled) {
_globalTypeValidityCheckEnabled = enabled;
@property public void typeValidityCheckEnabled(bool enabled) {
_typeValidityCheckEnabled = enabled;
}
@property public static bool globalTypeValidityCheckEnabled() {
return _globalTypeValidityCheckEnabled;
@property public bool typeValidityCheckEnabled() {
return _typeValidityCheckEnabled;
}
private this() {
}
public static Registration register(ConcreteType)() {
public Registration register(ConcreteType)() {
return register!(ConcreteType, ConcreteType)();
}
public static Registration register(InterfaceType, ConcreteType)(bool checkTypeValidity = true) {
public Registration register(InterfaceType, ConcreteType)(bool checkTypeValidity = true) {
TypeInfo registeredType = typeid(InterfaceType);
TypeInfo_Class instantiatableType = typeid(ConcreteType);
if (globalTypeValidityCheckEnabled && checkTypeValidity) {
if (typeValidityCheckEnabled && checkTypeValidity) {
checkValidity!(InterfaceType)(registeredType, instantiatableType);
}
@ -58,7 +55,7 @@ class Container {
return newRegistration;
}
private static void checkValidity(InterfaceType)(TypeInfo registeredType, TypeInfo_Class instanceType) {
private void checkValidity(InterfaceType)(TypeInfo registeredType, TypeInfo_Class instanceType) {
InterfaceType instanceCanBeCastToInterface = cast(InterfaceType) instanceType.create();
if (!instanceCanBeCastToInterface) {
string errorMessage = format("%s cannot be cast to %s.", instanceType.name, registeredType.toString());
@ -66,7 +63,7 @@ class Container {
}
}
public static ClassType resolve(ClassType)() {
public ClassType resolve(ClassType)() {
TypeInfo resolveType = typeid(ClassType);
Registration* registration = resolveType in registrations;
if (!registration) {
@ -75,7 +72,7 @@ class Container {
return cast(ClassType) registration.getInstance();
}
public static void clearRegistrations() {
public void clearRegistrations() {
registrations.clear();
}
}

View file

@ -14,55 +14,60 @@ version(unittest) {
unittest {
// Test register concrete type
Container.clearRegistrations();
auto registration = Container.register!(TestClass)();
auto container = new Container();
auto registration = container.register!(TestClass)();
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same");
}
unittest {
// Test resolve registered type
Container.clearRegistrations();
Container.register!(TestClass)();
TestClass actualInstance = Container.resolve!(TestClass)();
auto container = new Container();
container.register!(TestClass)();
TestClass actualInstance = container.resolve!(TestClass)();
assert(actualInstance !is null, "Resolved type is null");
assert(cast(TestClass) actualInstance, "Resolved class is not the same type as expected");
}
unittest {
// Test register interface
Container.clearRegistrations();
Container.register!(TestInterface, TestClass)();
TestInterface actualInstance = Container.resolve!(TestInterface)();
auto container = new Container();
container.register!(TestInterface, TestClass)();
TestInterface actualInstance = container.resolve!(TestInterface)();
assert(actualInstance !is null, "Resolved type is null");
assert(cast(TestInterface) actualInstance, "Resolved class is not the same type as expected");
}
unittest {
// Test register unrelated types fails
Container.clearRegistrations();
assertThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types does not fail");
auto container = new Container();
assertThrown!RegistrationException(container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types does not fail");
}
unittest {
// Test register unrelated types with disabled check on registration
Container.clearRegistrations();
assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(false), "Registering unrelated types while disabling type validity fails");
auto container = new Container();
assertNotThrown!RegistrationException(container.register!(UnrelatedClass, TestClass)(false), "Registering unrelated types while disabling type validity fails");
}
unittest {
// Test resolve non-registered type
Container.clearRegistrations();
assertThrown!ResolveException(Container.resolve!(TestClass)(), "Resolving non-registered type does not fail");
auto container = new Container();
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving non-registered type does not fail");
}
unittest {
// Test register unrelated class with disable global type validity disabled
bool currentSetting = Container.globalTypeValidityCheckEnabled;
Container.globalTypeValidityCheckEnabled = false;
auto container = new Container();
container.typeValidityCheckEnabled = false;
assertNotThrown!RegistrationException(container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types while disabling global type validity fails");
}
assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types while disabling global type validity fails");
Container.globalTypeValidityCheckEnabled = currentSetting;
unittest {
// Test clear registrations
auto container = new Container();
container.register!(TestClass)();
container.clearRegistrations();
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail");
}
}