Skip to content

Commit 82f2174

Browse files
committed
Refactor implicits out of core injector logic. Partial add of qualifiers.
1 parent de81230 commit 82f2174

File tree

14 files changed

+276
-228
lines changed

14 files changed

+276
-228
lines changed

IfInjector/IfInjector.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Distinguishing features are small size, speed and attribute based configuration.
9191
<Compile Include="source\Resolver\IBindingResolver.cs" />
9292
<Compile Include="source\Resolver\GenericBindingResolver.cs" />
9393
<Compile Include="source\Resolver\ImplicitBindingResolver.cs" />
94+
<Compile Include="source\Bindings\Config\BindingAttributeUtils.cs" />
9495
</ItemGroup>
9596
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
9697
<ItemGroup>

IfInjector/TODO.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
- Need for IDisposable???
33
- Implicit injection rules:
44
- Auto initialize types need error checking
5-
- Upcast bindings:
6-
- For iFace / concrete bindings - implicit bind concrete type?
7-
=> probably not: issue is that it would break 'decorators' feature
85

96
- KEY MISSING FEATURES:
107
- Decorators
@@ -17,7 +14,6 @@
1714
- TESTING:
1815
- Add platform tests for Android, iPhone
1916
- Need tests for error conditions in implicit bindings
20-
- Replace benchmarks with IoCPerformance suite
2117

2218
- WISH LIST:
2319
- Extension feature to allow for interceptors
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace IfInjector.Bindings.Config
5+
{
6+
internal static class BindingAttributeUtils
7+
{
8+
/// <summary>
9+
/// Gets if implemented by.
10+
/// </summary>
11+
/// <returns>The if implemented by.</returns>
12+
/// <param name="bindingType">Binding type.</param>
13+
internal static Type GetImplementedBy(Type bindingType) {
14+
var implTypeAttr = bindingType.GetCustomAttributes(typeof(ImplementedByAttribute), false).FirstOrDefault();
15+
if (implTypeAttr != null) {
16+
return (implTypeAttr as ImplementedByAttribute).Implementor;
17+
}
18+
19+
return null;
20+
}
21+
}
22+
}
23+

IfInjector/source/Bindings/Config/BindingKey.cs

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ internal class BindingKey : IEquatable<BindingKey> {
2424
private const int HASHCODE_MULTIPLIER = 33;
2525

2626
private readonly Type bindingType;
27-
private readonly bool member;
27+
private readonly bool isMember;
28+
private readonly bool isImplicit;
2829
private readonly string qualifier;
2930

30-
protected BindingKey(Type bindingType, bool member, string qualifier) {
31+
protected BindingKey(Type bindingType, bool isMember, bool isImplicit, string qualifier) {
3132
this.bindingType = bindingType;
32-
this.member = member;
33+
this.isMember = isMember;
34+
this.isImplicit = isImplicit;
3335
this.qualifier = (qualifier == null) ? NoQualifierValue : qualifier;
3436
}
3537

@@ -47,9 +49,19 @@ public Type BindingType {
4749
/// Gets a value indicating whether this <see cref="IfInjector.IfBinding.BindingKey"/> is a property-only binding.
4850
/// </summary>
4951
/// <value><c>true</c> if member; otherwise, <c>false</c>.</value>
50-
public bool Member {
52+
public bool IsMember {
5153
get {
52-
return member;
54+
return isMember;
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Gets a value indicating whether this instance is implicit. This facet does not affect the key for equals or hashing purposes.
60+
/// </summary>
61+
/// <value><c>true</c> if this instance is implicit; otherwise, <c>false</c>.</value>
62+
public bool IsImplicit {
63+
get {
64+
return isImplicit;
5365
}
5466
}
5567

@@ -71,7 +83,7 @@ public string Qualifier {
7183
/// </summary>
7284
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a hash table.</returns>
7385
public override int GetHashCode() {
74-
return HASHCODE_MULTIPLIER * BindingType.GetHashCode () + Member.GetHashCode () + qualifier.GetHashCode();
86+
return HASHCODE_MULTIPLIER * BindingType.GetHashCode () + IsMember.GetHashCode () + qualifier.GetHashCode();
7587
}
7688

7789
/// <summary>
@@ -97,57 +109,72 @@ public bool Equals(BindingKey obj) {
97109
return true;
98110
} else {
99111
return bindingType.Equals (obj.bindingType) &&
100-
member == obj.member &&
112+
isMember == obj.isMember &&
101113
qualifier.Equals(obj.qualifier);
102114
}
103115
}
104116

105117
/// <summary>
106-
/// Get the instance injector.
118+
/// Get the implicit version of the current binding key.
107119
/// </summary>
108-
/// <param name="keyType">Key type.</param>
109-
internal static BindingKey Get(Type keyType) {
110-
return Get (keyType, null);
120+
/// <returns>The implicit.</returns>
121+
internal BindingKey ToImplicit() {
122+
if (IsImplicit) {
123+
return this;
124+
}
125+
126+
return new BindingKey (
127+
bindingType: BindingType,
128+
isMember: IsMember,
129+
isImplicit: true,
130+
qualifier: Qualifier);
111131
}
112132

113133
/// <summary>
114134
/// Gets a key.
115135
/// </summary>
116136
/// <returns>The internal.</returns>
117-
/// <param name="keyType">Key type.</param>
137+
/// <param name="bindingType">bindingType.</param>
118138
/// <param name="isMember">If set to <c>true</c> is member.</param>
119-
internal static BindingKey Get(Type keyType, bool isMember) {
120-
return new BindingKey (keyType, isMember, null);
139+
/// <param name="qualifier">Qualifier.</param>
140+
internal static BindingKey Get(Type bindingType, string qualifier = null) {
141+
return new BindingKey (
142+
bindingType: bindingType,
143+
isMember: false,
144+
isImplicit: false,
145+
qualifier: qualifier);
121146
}
122147

123148
/// <summary>
124-
/// Gets the internal.
149+
/// Get the specified qualifier.
125150
/// </summary>
126-
/// <returns>The internal.</returns>
127-
/// <param name="keyType">Key type.</param>
128-
/// <param name="isMember">If set to <c>true</c> is member.</param>
129151
/// <param name="qualifier">Qualifier.</param>
130-
internal static BindingKey Get(Type keyType, string qualifier) {
131-
return new BindingKey (keyType, false, qualifier);
152+
/// <typeparam name="BType">The 1st type parameter.</typeparam>
153+
internal static BindingKey Get<BType>(string qualifier = null) where BType : class {
154+
return Get (typeof(BType), qualifier);
132155
}
133-
}
134-
135-
/// <summary>
136-
/// Typed binding key.
137-
/// </summary>
138-
internal class BindingKey<BType> : BindingKey where BType : class {
139-
internal static readonly BindingKey<BType> InstanceKey = new BindingKey<BType> (false, null);
140-
internal static readonly BindingKey<BType> MembersKey = new BindingKey<BType> (true, null);
141156

142-
private BindingKey(bool member, string qualifier) : base(typeof(BType), member, qualifier) { }
157+
/// <summary>
158+
/// Gets a key.
159+
/// </summary>
160+
/// <returns>The internal.</returns>
161+
/// <param name="concreteType">Concrete type.</param>
162+
/// <param name="isMember">If set to <c>true</c> is member.</param>
163+
internal static BindingKey GetMember(Type concreteType) {
164+
return new BindingKey (
165+
bindingType: concreteType,
166+
isMember: true,
167+
isImplicit: false,
168+
qualifier: null);
169+
}
143170

144171
/// <summary>
145-
/// Get the instance injector.
172+
/// Gets the member key.
146173
/// </summary>
147-
/// <param name="keyType">Key type.</param>
148-
/// <param name="qualifier">Qualifier.</param>
149-
internal static BindingKey Get(string qualifier) {
150-
return new BindingKey<BType> (false, qualifier);
174+
/// <returns>The member.</returns>
175+
/// <typeparam name="CType">The 1st type parameter.</typeparam>
176+
internal static BindingKey GetMember<CType>() {
177+
return GetMember (typeof(CType));
151178
}
152179
}
153180
}

IfInjector/source/Bindings/Fluent/Concrete/BoundBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal class BoundBinding<BType, CType> : IBoundBinding<BType, CType>, IBindin
2020

2121
internal BoundBinding() {
2222
BindingConfig = new BindingConfig(typeof(CType));
23-
BindingKey = BindingKey<BType>.InstanceKey;
23+
BindingKey = BindingKey.Get<BType> ();
2424
}
2525

2626
public IBoundBinding<BType, CType> SetLifestyle (Lifestyle lifestyle) {

IfInjector/source/Bindings/Fluent/Members/BoundMembersBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class BoundMembersBinding<CType> : IBoundMembersBinding<CType>, IBindin
1717

1818
internal BoundMembersBinding() {
1919
BindingConfig = new BindingConfig(typeof(CType));
20-
BindingKey = BindingKey<CType>.MembersKey;
20+
BindingKey = BindingKey.GetMember<CType>();
2121
}
2222

2323
public IBoundMembersBinding<CType> InjectMember<TPropertyType> (Expression<Func<CType, TPropertyType>> memberExpression)

0 commit comments

Comments
 (0)