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 this() {
|
||||
auto container = Container.getInstance();
|
||||
container.autowire!(typeof(this))(this);
|
||||
globalAutowire!(typeof(this))(this);
|
||||
}
|
||||
|
||||
// or use:
|
||||
|
|
|
@ -1,46 +1,49 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014 Mike Bierlee
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
module poodinis.autowire;
|
||||
|
||||
public import poodinis.container;
|
||||
|
||||
import std.typetuple;
|
||||
|
||||
debug {
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
}
|
||||
|
||||
class Autowire{};
|
||||
|
||||
public void autowire(Type)(Container container, Type instance) {
|
||||
foreach (member ; __traits(allMembers, Type)) {
|
||||
foreach (attribute; mixin(`__traits(getAttributes, Type.` ~ member ~ `)`) ) {
|
||||
if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){
|
||||
alias TypeTuple!(__traits(getMember, instance, member)) memberReference;
|
||||
auto autowirableInstance = container.resolve!(typeof(memberReference));
|
||||
debug {
|
||||
auto autowirableType = typeid(typeof(memberReference[0]));
|
||||
auto autowireableAddress = &autowirableInstance;
|
||||
auto memberType = typeid(Type);
|
||||
auto instanceAddress = &instance;
|
||||
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member));
|
||||
}
|
||||
|
||||
__traits(getMember, instance, member) = autowirableInstance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mixin template AutowireConstructor() {
|
||||
public this() {
|
||||
auto __container = Container.getInstance();
|
||||
__container.autowire!(typeof(this))(this);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Copyright 2014 Mike Bierlee
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
module poodinis.autowire;
|
||||
|
||||
public import poodinis.container;
|
||||
|
||||
import std.typetuple;
|
||||
|
||||
debug {
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
}
|
||||
|
||||
class Autowire{};
|
||||
|
||||
public void autowire(Type)(Container container, Type instance) {
|
||||
foreach (member ; __traits(allMembers, Type)) {
|
||||
foreach (attribute; mixin(`__traits(getAttributes, Type.` ~ member ~ `)`) ) {
|
||||
if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){
|
||||
alias TypeTuple!(__traits(getMember, instance, member)) memberReference;
|
||||
auto autowirableInstance = container.resolve!(typeof(memberReference));
|
||||
debug {
|
||||
auto autowirableType = typeid(typeof(memberReference[0]));
|
||||
auto autowireableAddress = &autowirableInstance;
|
||||
auto memberType = typeid(Type);
|
||||
auto instanceAddress = &instance;
|
||||
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member));
|
||||
}
|
||||
|
||||
__traits(getMember, instance, member) = autowirableInstance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mixin template AutowireConstructor() {
|
||||
public this() {
|
||||
globalAutowire!(typeof(this))(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void globalAutowire(Type)(Type instance) {
|
||||
Container.getInstance().autowire!(typeof(instance))(instance);
|
||||
}
|
Loading…
Reference in a new issue