mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add removing of registrations
This commit is contained in:
parent
60b1149aea
commit
b78e05455d
|
@ -59,22 +59,26 @@ class Container {
|
|||
}
|
||||
}
|
||||
|
||||
public ClassType resolve(ClassType)() {
|
||||
TypeInfo resolveType = typeid(ClassType);
|
||||
public RegistrationType resolve(RegistrationType)() {
|
||||
TypeInfo resolveType = typeid(RegistrationType);
|
||||
Registration* registration = resolveType in registrations;
|
||||
if (!registration) {
|
||||
throw new ResolveException("Type not registered.", resolveType);
|
||||
}
|
||||
|
||||
ClassType instance = cast(ClassType) registration.getInstance();
|
||||
this.autowire!(ClassType)(instance);
|
||||
RegistrationType instance = cast(RegistrationType) registration.getInstance();
|
||||
this.autowire!(RegistrationType)(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void clearRegistrations() {
|
||||
public void clearAllRegistrations() {
|
||||
registrations.clear();
|
||||
}
|
||||
|
||||
public void removeRegistration(RegistrationType)() {
|
||||
registrations.remove(typeid(RegistrationType));
|
||||
}
|
||||
|
||||
public static Container getInstance() {
|
||||
if (instance is null) {
|
||||
instance = new Container();
|
||||
|
|
|
@ -90,7 +90,7 @@ version(unittest) {
|
|||
unittest {
|
||||
auto container = new Container();
|
||||
container.register!(TestClass)();
|
||||
container.clearRegistrations();
|
||||
container.clearAllRegistrations();
|
||||
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail");
|
||||
}
|
||||
|
||||
|
@ -157,5 +157,13 @@ version(unittest) {
|
|||
auto mouse = container.resolve!ComponentMouse;
|
||||
auto cat = container.resolve!ComponentCat;
|
||||
assert(mouse.cat is cat && cat.mouse is mouse, "Circular dependencies should be autowirable");
|
||||
}
|
||||
}
|
||||
|
||||
// Test remove registration
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
container.register!TestClass;
|
||||
container.removeRegistration!TestClass;
|
||||
assertThrown!ResolveException(container.resolve!TestClass);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue