Autowire upon resolving instances

This commit is contained in:
Mike Bierlee 2014-05-30 00:42:58 +02:00
parent 9e59dc0a37
commit 52936379e4
2 changed files with 23 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import std.string;
import std.array;
public import poodinis.registration;
public import poodinis.autowire;
class RegistrationException : Exception {
this(string message, TypeInfo registeredType, TypeInfo_Class instantiatableType) {
@ -64,7 +65,10 @@ class Container {
if (!registration) {
throw new ResolveException("Type not registered.", resolveType);
}
return cast(ClassType) registration.getInstance();
ClassType instance = cast(ClassType) registration.getInstance();
this.autowire!(ClassType)(instance);
return instance;
}
public void clearRegistrations() {

View file

@ -18,6 +18,14 @@ version(unittest) {
}
}
class AutowiredClass {
}
class ComponentClass {
@Autowire
public AutowiredClass autowiredClass;
}
// Test register concrete type
unittest {
auto container = new Container();
@ -121,4 +129,14 @@ version(unittest) {
assert(expectedInstance is actualInstance, "Resolved instance from existing instance scope is not the same as the registered instance");
}
// Test autowire resolved instances
unittest {
auto container = new Container();
container.register!AutowiredClass;
container.register!ComponentClass;
auto componentInstance = container.resolve!ComponentClass;
auto autowiredInstance = container.resolve!AutowiredClass;
assert(componentInstance.autowiredClass is autowiredInstance, "Member is not autowired upon resolving");
}
}