Specify qualifiers in Autowire UDA instead of using separate UDA

This commit is contained in:
Mike Bierlee 2014-12-13 23:15:43 +01:00
parent 1166d2811a
commit 7ee67ee836
2 changed files with 15 additions and 27 deletions

View file

@ -16,11 +16,11 @@ debug {
import std.string; import std.string;
} }
class Autowire{}; struct UseMemberType {};
struct Qualifier(T) { struct Autowire(QualifierType = UseMemberType) {
T qualifier; QualifierType qualifier;
} };
alias Autowired = Autowire; alias Autowired = Autowire;
@ -36,31 +36,22 @@ 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)))) { static if(__traits(compiles, __traits(getMember, Type, member)) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member)))) {
foreach(autowireAttribute; __traits(getAttributes, __traits(getMember, Type, member))) { foreach(autowireAttribute; __traits(getAttributes, __traits(getMember, Type, member))) {
static if (is(autowireAttribute : Autowire)) { static if (__traits(isSame, autowireAttribute, Autowire) || is(autowireAttribute == Autowire!T, T)) {
if (__traits(getMember, instance, member) is null) { if (__traits(getMember, instance, member) is null) {
alias TypeTuple!(__traits(getMember, instance, member)) memberReference; alias memberReference = TypeTuple!(__traits(getMember, instance, member));
alias MemberType = typeof(memberReference)[0]; alias MemberType = typeof(memberReference)[0];
debug { debug {
string qualifiedInstanceTypeString = typeid(MemberType).toString; string qualifiedInstanceTypeString = typeid(MemberType).toString;
} }
MemberType qualifiedInstance; MemberType qualifiedInstance;
auto resolvedThroughQualifier = false; static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) {
foreach (qualifierAttribute; __traits(getAttributes, __traits(getMember, Type, member))) { alias QualifierType = typeof(autowireAttribute.qualifier);
static if (is(qualifierAttribute == Qualifier!T, T)) {
alias QualifierType = typeof(qualifierAttribute.qualifier);
qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType); qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType);
debug { debug {
qualifiedInstanceTypeString = typeid(QualifierType).toString; qualifiedInstanceTypeString = typeid(QualifierType).toString;
} }
} else {
resolvedThroughQualifier = true;
break;
}
}
if (!resolvedThroughQualifier) {
qualifiedInstance = container.resolve!(typeof(memberReference)); qualifiedInstance = container.resolve!(typeof(memberReference));
} }

View file

@ -62,18 +62,15 @@ version(unittest) {
class ComponentX : InterfaceA {} class ComponentX : InterfaceA {}
class MonkeyShine { class MonkeyShine {
@Autowire @Autowire!ComponentX
@Qualifier!ComponentX
public InterfaceA component; public InterfaceA component;
} }
class BootstrapBootstrap { class BootstrapBootstrap {
@Autowire @Autowire!ComponentX
@Qualifier!ComponentX
public InterfaceA componentX; public InterfaceA componentX;
@Autowire @Autowire!ComponentC
@Qualifier!ComponentC
public InterfaceA componentC; public InterfaceA componentC;
} }