mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Specify qualifiers in Autowire UDA instead of using separate UDA
This commit is contained in:
parent
1166d2811a
commit
7ee67ee836
|
@ -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)) {
|
qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType);
|
||||||
alias QualifierType = typeof(qualifierAttribute.qualifier);
|
debug {
|
||||||
qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType);
|
qualifiedInstanceTypeString = typeid(QualifierType).toString;
|
||||||
|
|
||||||
debug {
|
|
||||||
qualifiedInstanceTypeString = typeid(QualifierType).toString;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolvedThroughQualifier = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if (!resolvedThroughQualifier) {
|
|
||||||
qualifiedInstance = container.resolve!(typeof(memberReference));
|
qualifiedInstance = container.resolve!(typeof(memberReference));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue