Add global setting for disabling type validity

This commit is contained in:
Mike Bierlee 2014-05-20 00:16:44 +02:00
parent 423f21230a
commit da1f604ef7
2 changed files with 78 additions and 58 deletions

View file

@ -28,6 +28,16 @@ class Container {
private static Registration[TypeInfo] registrations; private static Registration[TypeInfo] registrations;
private static bool _globalTypeValidityCheckEnabled = true;
@property public static void globalTypeValidityCheckEnabled(bool enabled) {
_globalTypeValidityCheckEnabled = enabled;
}
@property public static bool globalTypeValidityCheckEnabled() {
return _globalTypeValidityCheckEnabled;
}
private this() { private this() {
} }
@ -39,7 +49,7 @@ class Container {
TypeInfo registeredType = typeid(InterfaceType); TypeInfo registeredType = typeid(InterfaceType);
TypeInfo_Class instantiatableType = typeid(ConcreteType); TypeInfo_Class instantiatableType = typeid(ConcreteType);
if (checkTypeValidity) { if (globalTypeValidityCheckEnabled && checkTypeValidity) {
checkValidity!(InterfaceType)(registeredType, instantiatableType); checkValidity!(InterfaceType)(registeredType, instantiatableType);
} }

View file

@ -55,4 +55,14 @@ version(unittest) {
assertThrown!ResolveException(Container.resolve!(TestClass)(), "Resolving non-registered type does not fail"); 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;
assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types while disabling global type validity fails");
Container.globalTypeValidityCheckEnabled = currentSetting;
}
} }