mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Import modules of argument types of registration type's template before calling post-constructor
This commit is contained in:
parent
9be2d4fac1
commit
4c25b91c63
|
@ -1,5 +1,8 @@
|
|||
Poodinis Changelog
|
||||
==================
|
||||
**Unreleased**
|
||||
* FIX value injectors failing to resolve in certain situations when they inject structs
|
||||
|
||||
**Version 8.0.0**
|
||||
* ADD value injection. Members with UDA @Value will be attempted to be injected with a value-type. See tutorial and examples for more info.
|
||||
* ADD @PostConstruct UDA for marking methods which should be called after a dependency is resolved and autowired.
|
||||
|
|
|
@ -391,7 +391,8 @@ synchronized class DependencyContainer {
|
|||
|
||||
private void callPostConstructors(Type)(Type instance) {
|
||||
foreach (memberName; __traits(allMembers, Type)) {
|
||||
mixin(`import ` ~ moduleName!Type ~ `;`);
|
||||
mixin(createImportsString!Type);
|
||||
|
||||
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
|
||||
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
|
||||
&& isFunction!(mixin(fullyQualifiedName!Type ~ `.` ~ memberName))
|
||||
|
@ -401,6 +402,19 @@ synchronized class DependencyContainer {
|
|||
}
|
||||
}
|
||||
|
||||
private static string createImportsString(Type)() {
|
||||
string imports = `import ` ~ moduleName!Type ~ `;`;
|
||||
static if (__traits(compiles, TemplateArgsOf!Type)) {
|
||||
foreach(TemplateArgType; TemplateArgsOf!Type) {
|
||||
static if (!isBuiltinType!TemplateArgType) {
|
||||
imports ~= "import " ~ moduleName!TemplateArgType ~ ";";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all dependency registrations managed by this container.
|
||||
*/
|
||||
|
|
|
@ -12,6 +12,22 @@ import std.exception;
|
|||
|
||||
version(unittest) {
|
||||
|
||||
struct LocalStruct {
|
||||
bool wasInjected = false;
|
||||
}
|
||||
|
||||
class LocalStructInjector : ValueInjector!LocalStruct {
|
||||
public override LocalStruct get(string key) {
|
||||
auto data = LocalStruct(true);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class LocalClassWithStruct {
|
||||
@Value("")
|
||||
public LocalStruct localStruct;
|
||||
}
|
||||
|
||||
// Test injection of values
|
||||
unittest {
|
||||
auto container = new shared DependencyContainer();
|
||||
|
@ -105,4 +121,17 @@ version(unittest) {
|
|||
|
||||
assert(injector.config.noms == 8899);
|
||||
}
|
||||
|
||||
// Test resolving locally defined struct injector (github issue #20)
|
||||
unittest {
|
||||
auto container = new shared DependencyContainer();
|
||||
container.register!(ValueInjector!LocalStruct, LocalStructInjector);
|
||||
container.register!LocalClassWithStruct;
|
||||
|
||||
auto injector = container.resolve!(ValueInjector!LocalStruct);
|
||||
assert(injector !is null);
|
||||
|
||||
auto localClass = container.resolve!LocalClassWithStruct;
|
||||
assert(localClass.localStruct.wasInjected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue