Extract autowiring a member from autowire

This commit is contained in:
Mike Bierlee 2015-02-28 15:51:57 +01:00
parent 6fbb313743
commit 58073dd160

View file

@ -77,8 +77,6 @@ struct Autowire(QualifierType = UseMemberType) {
* See_Also: Autowire * See_Also: Autowire
*/ */
public void autowire(Type)(DependencyContainer container, Type instance) { public void autowire(Type)(DependencyContainer container, Type instance) {
// For the love of god, refactor this!
debug(poodinisVerbose) { debug(poodinisVerbose) {
auto instanceType = typeid(Type); auto instanceType = typeid(Type);
auto instanceAddress = &instance; auto instanceAddress = &instance;
@ -86,38 +84,45 @@ public void autowire(Type)(DependencyContainer container, Type instance) {
} }
foreach (member ; __traits(allMembers, Type)) { foreach (member ; __traits(allMembers, Type)) {
static if(__traits(compiles, __traits(getMember, Type, member)) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member)))) { autowireMember!(member)(container, instance);
foreach(autowireAttribute; __traits(getAttributes, __traits(getMember, Type, member))) { }
static if (__traits(isSame, autowireAttribute, Autowire) || is(autowireAttribute == Autowire!T, T)) { }
if (__traits(getMember, instance, member) is null) {
alias memberReference = TypeTuple!(__traits(getMember, instance, member));
alias MemberType = typeof(memberReference)[0];
debug(poodinisVerbose) { private void autowireMember(string member, Type)(DependencyContainer container, Type instance) {
string qualifiedInstanceTypeString = typeid(MemberType).toString; // For the love of god, refactor this! <-- Doing it, bro!
} static if(__traits(compiles, __traits(getMember, Type, member)) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member)))) {
foreach(autowireAttribute; __traits(getAttributes, __traits(getMember, Type, member))) {
static if (__traits(isSame, autowireAttribute, Autowire) || is(autowireAttribute == Autowire!T, T)) {
if (__traits(getMember, instance, member) is null) {
alias memberReference = TypeTuple!(__traits(getMember, instance, member));
alias MemberType = typeof(memberReference)[0];
MemberType qualifiedInstance; debug(poodinisVerbose) {
static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) { string qualifiedInstanceTypeString = typeid(MemberType).toString;
alias QualifierType = typeof(autowireAttribute.qualifier);
qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType);
debug(poodinisVerbose) {
qualifiedInstanceTypeString = typeid(QualifierType).toString;
}
} else {
qualifiedInstance = container.resolve!(typeof(memberReference));
}
__traits(getMember, instance, member) = qualifiedInstance;
debug(poodinisVerbose) {
auto qualifiedInstanceAddress = &qualifiedInstance;
writeln(format("DEBUG: Autowired instance [%s@%s] to [%s@%s].%s", qualifiedInstanceTypeString, qualifiedInstanceAddress, instanceType, instanceAddress, member));
}
} }
break; MemberType qualifiedInstance;
static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) {
alias QualifierType = typeof(autowireAttribute.qualifier);
qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType);
debug(poodinisVerbose) {
qualifiedInstanceTypeString = typeid(QualifierType).toString;
}
} else {
qualifiedInstance = container.resolve!(typeof(memberReference));
}
__traits(getMember, instance, member) = qualifiedInstance;
debug(poodinisVerbose) {
auto instanceType = typeid(Type);
auto instanceAddress = &instance;
auto qualifiedInstanceAddress = &qualifiedInstance;
writeln(format("DEBUG: Autowired instance [%s@%s] to [%s@%s].%s", qualifiedInstanceTypeString, qualifiedInstanceAddress, instanceType, instanceAddress, member));
}
} }
break;
} }
} }
} }