mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Autowire upon resolving instances
This commit is contained in:
parent
9e59dc0a37
commit
52936379e4
|
@ -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() {
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue