mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add global autowire function
This commit is contained in:
parent
7dbf5ace61
commit
edb39c6d54
|
@ -133,8 +133,7 @@ class ComponentF {
|
||||||
public ComponentA componentA;
|
public ComponentA componentA;
|
||||||
|
|
||||||
public this() {
|
public this() {
|
||||||
auto container = Container.getInstance();
|
globalAutowire!(typeof(this))(this);
|
||||||
container.autowire!(typeof(this))(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// or use:
|
// or use:
|
||||||
|
|
|
@ -1,46 +1,49 @@
|
||||||
/**
|
/**
|
||||||
* Poodinis Dependency Injection Framework
|
* Poodinis Dependency Injection Framework
|
||||||
* Copyright 2014 Mike Bierlee
|
* Copyright 2014 Mike Bierlee
|
||||||
* This software is licensed under the terms of the MIT license.
|
* This software is licensed under the terms of the MIT license.
|
||||||
* The full terms of the license can be found in the LICENSE file.
|
* The full terms of the license can be found in the LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module poodinis.autowire;
|
module poodinis.autowire;
|
||||||
|
|
||||||
public import poodinis.container;
|
public import poodinis.container;
|
||||||
|
|
||||||
import std.typetuple;
|
import std.typetuple;
|
||||||
|
|
||||||
debug {
|
debug {
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.string;
|
import std.string;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Autowire{};
|
class Autowire{};
|
||||||
|
|
||||||
public void autowire(Type)(Container container, Type instance) {
|
public void autowire(Type)(Container container, Type instance) {
|
||||||
foreach (member ; __traits(allMembers, Type)) {
|
foreach (member ; __traits(allMembers, Type)) {
|
||||||
foreach (attribute; mixin(`__traits(getAttributes, Type.` ~ member ~ `)`) ) {
|
foreach (attribute; mixin(`__traits(getAttributes, Type.` ~ member ~ `)`) ) {
|
||||||
if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){
|
if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){
|
||||||
alias TypeTuple!(__traits(getMember, instance, member)) memberReference;
|
alias TypeTuple!(__traits(getMember, instance, member)) memberReference;
|
||||||
auto autowirableInstance = container.resolve!(typeof(memberReference));
|
auto autowirableInstance = container.resolve!(typeof(memberReference));
|
||||||
debug {
|
debug {
|
||||||
auto autowirableType = typeid(typeof(memberReference[0]));
|
auto autowirableType = typeid(typeof(memberReference[0]));
|
||||||
auto autowireableAddress = &autowirableInstance;
|
auto autowireableAddress = &autowirableInstance;
|
||||||
auto memberType = typeid(Type);
|
auto memberType = typeid(Type);
|
||||||
auto instanceAddress = &instance;
|
auto instanceAddress = &instance;
|
||||||
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member));
|
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member));
|
||||||
}
|
}
|
||||||
|
|
||||||
__traits(getMember, instance, member) = autowirableInstance;
|
__traits(getMember, instance, member) = autowirableInstance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mixin template AutowireConstructor() {
|
mixin template AutowireConstructor() {
|
||||||
public this() {
|
public this() {
|
||||||
auto __container = Container.getInstance();
|
globalAutowire!(typeof(this))(this);
|
||||||
__container.autowire!(typeof(this))(this);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public void globalAutowire(Type)(Type instance) {
|
||||||
|
Container.getInstance().autowire!(typeof(instance))(instance);
|
||||||
|
}
|
Loading…
Reference in a new issue