Skip to content

Commit a7ff068

Browse files
committed
Fixes #107
1 parent eacad57 commit a7ff068

File tree

10 files changed

+47
-20
lines changed

10 files changed

+47
-20
lines changed

src/Abstracts/IUnityContainerAsync.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
34
using System.Threading.Tasks;
45
using Unity.Injection;
56
using Unity.Lifetime;
@@ -97,6 +98,17 @@ public interface IUnityContainerAsync : IDisposable
9798
Task<object> Resolve(Type type, string name, params ResolverOverride[] overrides);
9899

99100

101+
/// <summary>
102+
/// Resolve an instance of the requested type from the container.
103+
/// </summary>
104+
/// <param name="type"><see cref="Type"/> of object to get typeFrom the container.</param>
105+
/// <param name="regex">Pattern to match names to. Only these with successful
106+
/// <see cref="Regex.IsMatch(string name)"/> will be resolved</param>
107+
/// <param name="overrides">Any overrides for the resolve call.</param>
108+
/// <returns>The retrieved object.</returns>
109+
Task<IEnumerable<object>> Resolve(Type type, Regex regex, params ResolverOverride[] overrides);
110+
111+
100112
/// <summary>
101113
/// The parent of this container.
102114
/// </summary>

src/Abstracts/ResolutionOption.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+

2+
namespace Unity
3+
{
4+
/// <summary>
5+
/// Options indicating how dependency should be resolved
6+
/// </summary>
7+
public enum ResolutionOption
8+
{
9+
/// <summary>
10+
/// Required dependency
11+
/// </summary>
12+
/// <remarks>
13+
/// This dependency should be either resolved or error
14+
/// should be reported.
15+
/// </remarks>
16+
Required,
17+
18+
/// <summary>
19+
/// Optional dependency
20+
/// </summary>
21+
/// <remarks>
22+
/// This dependency should be either resolved or default
23+
/// value will be returned.
24+
/// </remarks>
25+
Optional
26+
}
27+
}

src/Attributes/Annotation/DependencyAttribute.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ namespace Unity
1414
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.Field)]
1515
public sealed class DependencyAttribute : DependencyResolutionAttribute
1616
{
17-
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
18-
internal static DependencyAttribute Instance = new DependencyAttribute();
19-
2017
/// <summary>
2118
/// Create an instance of <see cref="DependencyAttribute"/> with no name.
2219
/// </summary>

src/Attributes/Annotation/OptionalDependencyAttribute.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ namespace Unity
1111
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property)]
1212
public sealed class OptionalDependencyAttribute : DependencyResolutionAttribute
1313
{
14-
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
15-
internal static OptionalDependencyAttribute Instance = new OptionalDependencyAttribute();
16-
1714
/// <summary>
1815
/// Construct a new <see cref="OptionalDependencyAttribute"/> object.
1916
/// </summary>

src/Dependency/Injection/Members/InjectionField.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Reflection;
54

65
namespace Unity.Injection
@@ -13,10 +12,9 @@ public class InjectionField : MemberInfoBase<FieldInfo>
1312
/// Configure the container to inject the given field name.
1413
/// </summary>
1514
/// <param name="name">Name of property to inject.</param>
16-
/// <param name="optional">Tells Unity if this field is optional.</param>
17-
public InjectionField(string name, bool optional = false)
18-
: base(name, optional ? OptionalDependencyAttribute.Instance
19-
: (object)DependencyAttribute.Instance)
15+
/// <param name="option">Tells Unity if this field is required or optional.</param>
16+
public InjectionField(string name, ResolutionOption option = ResolutionOption.Required)
17+
: base(name, option)
2018
{
2119
}
2220

src/Dependency/Injection/Members/InjectionProperty.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ public class InjectionProperty : MemberInfoBase<PropertyInfo>
1818
/// using the value supplied.
1919
/// </summary>
2020
/// <param name="name">Name of property to inject.</param>
21-
/// <param name="optional">Tells Unity if this field is optional.</param>
22-
public InjectionProperty(string name, bool optional = false)
23-
: base(name, optional ? OptionalDependencyAttribute.Instance
24-
: (object)DependencyAttribute.Instance)
21+
/// <param name="option">Tells Unity if this field is optional.</param>
22+
public InjectionProperty(string name, ResolutionOption option = ResolutionOption.Required)
23+
: base(name, option)
2524
{
2625
}
2726

src/Dependency/Injection/Parameters/GenericResolvedArrayParameter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4-
using Unity.Policy;
54
using Unity.Resolution;
65

76
namespace Unity.Injection

src/Dependency/Injection/Parameters/ResolvedArrayParameter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4-
using Unity.Policy;
54
using Unity.Resolution;
65

76
namespace Unity.Injection

src/Dependency/Injection/Parameters/ResolvedParameter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Reflection;
3-
using Unity.Policy;
43
using Unity.Resolution;
54

65
namespace Unity.Injection

src/Extensions/Injection/Resolve.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static partial class Resolve
116116
#if !NET40
117117
[MethodImpl(MethodImplOptions.AggressiveInlining)]
118118
#endif
119-
public static InjectionMember OptionalField(string name) => new InjectionField(name, true);
119+
public static InjectionMember OptionalField(string name) => new InjectionField(name, ResolutionOption.Optional);
120120

121121
#endregion
122122

@@ -131,7 +131,7 @@ public static partial class Resolve
131131
#if !NET40
132132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
133133
#endif
134-
public static InjectionMember OptionalProperty(string name) => new InjectionProperty(name ?? throw new ArgumentNullException(nameof(name)), true);
134+
public static InjectionMember OptionalProperty(string name) => new InjectionProperty(name ?? throw new ArgumentNullException(nameof(name)), ResolutionOption.Optional);
135135

136136
#endregion
137137
}

0 commit comments

Comments
 (0)