From a7ff068b82bb13ce050b9629b613a62245d9996b Mon Sep 17 00:00:00 2001 From: Eugene Sadovoi Date: Tue, 23 Apr 2019 21:03:53 -0400 Subject: [PATCH] Fixes #107 --- src/Abstracts/IUnityContainerAsync.cs | 12 +++++++++ src/Abstracts/ResolutionOption.cs | 27 +++++++++++++++++++ .../Annotation/DependencyAttribute.cs | 3 --- .../Annotation/OptionalDependencyAttribute.cs | 3 --- .../Injection/Members/InjectionField.cs | 8 +++--- .../Injection/Members/InjectionProperty.cs | 7 +++-- .../GenericResolvedArrayParameter.cs | 1 - .../Parameters/ResolvedArrayParameter.cs | 1 - .../Injection/Parameters/ResolvedParameter.cs | 1 - src/Extensions/Injection/Resolve.cs | 4 +-- 10 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 src/Abstracts/ResolutionOption.cs diff --git a/src/Abstracts/IUnityContainerAsync.cs b/src/Abstracts/IUnityContainerAsync.cs index aa5d6eda..5787d0f7 100644 --- a/src/Abstracts/IUnityContainerAsync.cs +++ b/src/Abstracts/IUnityContainerAsync.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Unity.Injection; using Unity.Lifetime; @@ -97,6 +98,17 @@ public interface IUnityContainerAsync : IDisposable Task Resolve(Type type, string name, params ResolverOverride[] overrides); + /// + /// Resolve an instance of the requested type from the container. + /// + /// of object to get typeFrom the container. + /// Pattern to match names to. Only these with successful + /// will be resolved + /// Any overrides for the resolve call. + /// The retrieved object. + Task> Resolve(Type type, Regex regex, params ResolverOverride[] overrides); + + /// /// The parent of this container. /// diff --git a/src/Abstracts/ResolutionOption.cs b/src/Abstracts/ResolutionOption.cs new file mode 100644 index 00000000..c7bff8fa --- /dev/null +++ b/src/Abstracts/ResolutionOption.cs @@ -0,0 +1,27 @@ + +namespace Unity +{ + /// + /// Options indicating how dependency should be resolved + /// + public enum ResolutionOption + { + /// + /// Required dependency + /// + /// + /// This dependency should be either resolved or error + /// should be reported. + /// + Required, + + /// + /// Optional dependency + /// + /// + /// This dependency should be either resolved or default + /// value will be returned. + /// + Optional + } +} diff --git a/src/Attributes/Annotation/DependencyAttribute.cs b/src/Attributes/Annotation/DependencyAttribute.cs index 273776a9..ad6d4ae9 100644 --- a/src/Attributes/Annotation/DependencyAttribute.cs +++ b/src/Attributes/Annotation/DependencyAttribute.cs @@ -14,9 +14,6 @@ namespace Unity [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.Field)] public sealed class DependencyAttribute : DependencyResolutionAttribute { - [DebuggerBrowsable(DebuggerBrowsableState.Never)] - internal static DependencyAttribute Instance = new DependencyAttribute(); - /// /// Create an instance of with no name. /// diff --git a/src/Attributes/Annotation/OptionalDependencyAttribute.cs b/src/Attributes/Annotation/OptionalDependencyAttribute.cs index 4ca388f9..2a1badf6 100644 --- a/src/Attributes/Annotation/OptionalDependencyAttribute.cs +++ b/src/Attributes/Annotation/OptionalDependencyAttribute.cs @@ -11,9 +11,6 @@ namespace Unity [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property)] public sealed class OptionalDependencyAttribute : DependencyResolutionAttribute { - [DebuggerBrowsable(DebuggerBrowsableState.Never)] - internal static OptionalDependencyAttribute Instance = new OptionalDependencyAttribute(); - /// /// Construct a new object. /// diff --git a/src/Dependency/Injection/Members/InjectionField.cs b/src/Dependency/Injection/Members/InjectionField.cs index dc084fc5..26387789 100644 --- a/src/Dependency/Injection/Members/InjectionField.cs +++ b/src/Dependency/Injection/Members/InjectionField.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Reflection; namespace Unity.Injection @@ -13,10 +12,9 @@ public class InjectionField : MemberInfoBase /// Configure the container to inject the given field name. /// /// Name of property to inject. - /// Tells Unity if this field is optional. - public InjectionField(string name, bool optional = false) - : base(name, optional ? OptionalDependencyAttribute.Instance - : (object)DependencyAttribute.Instance) + /// Tells Unity if this field is required or optional. + public InjectionField(string name, ResolutionOption option = ResolutionOption.Required) + : base(name, option) { } diff --git a/src/Dependency/Injection/Members/InjectionProperty.cs b/src/Dependency/Injection/Members/InjectionProperty.cs index 1dd5cbe5..6062ae05 100644 --- a/src/Dependency/Injection/Members/InjectionProperty.cs +++ b/src/Dependency/Injection/Members/InjectionProperty.cs @@ -18,10 +18,9 @@ public class InjectionProperty : MemberInfoBase /// using the value supplied. /// /// Name of property to inject. - /// Tells Unity if this field is optional. - public InjectionProperty(string name, bool optional = false) - : base(name, optional ? OptionalDependencyAttribute.Instance - : (object)DependencyAttribute.Instance) + /// Tells Unity if this field is optional. + public InjectionProperty(string name, ResolutionOption option = ResolutionOption.Required) + : base(name, option) { } diff --git a/src/Dependency/Injection/Parameters/GenericResolvedArrayParameter.cs b/src/Dependency/Injection/Parameters/GenericResolvedArrayParameter.cs index c98adffe..65e36c49 100644 --- a/src/Dependency/Injection/Parameters/GenericResolvedArrayParameter.cs +++ b/src/Dependency/Injection/Parameters/GenericResolvedArrayParameter.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using System.Reflection; -using Unity.Policy; using Unity.Resolution; namespace Unity.Injection diff --git a/src/Dependency/Injection/Parameters/ResolvedArrayParameter.cs b/src/Dependency/Injection/Parameters/ResolvedArrayParameter.cs index fe7f1194..1a0d4f70 100644 --- a/src/Dependency/Injection/Parameters/ResolvedArrayParameter.cs +++ b/src/Dependency/Injection/Parameters/ResolvedArrayParameter.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using System.Reflection; -using Unity.Policy; using Unity.Resolution; namespace Unity.Injection diff --git a/src/Dependency/Injection/Parameters/ResolvedParameter.cs b/src/Dependency/Injection/Parameters/ResolvedParameter.cs index 0412957f..12e8e2a6 100644 --- a/src/Dependency/Injection/Parameters/ResolvedParameter.cs +++ b/src/Dependency/Injection/Parameters/ResolvedParameter.cs @@ -1,6 +1,5 @@ using System; using System.Reflection; -using Unity.Policy; using Unity.Resolution; namespace Unity.Injection diff --git a/src/Extensions/Injection/Resolve.cs b/src/Extensions/Injection/Resolve.cs index 6d834f5e..4fb399cb 100644 --- a/src/Extensions/Injection/Resolve.cs +++ b/src/Extensions/Injection/Resolve.cs @@ -116,7 +116,7 @@ public static partial class Resolve #if !NET40 [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static InjectionMember OptionalField(string name) => new InjectionField(name, true); + public static InjectionMember OptionalField(string name) => new InjectionField(name, ResolutionOption.Optional); #endregion @@ -131,7 +131,7 @@ public static partial class Resolve #if !NET40 [MethodImpl(MethodImplOptions.AggressiveInlining)] #endif - public static InjectionMember OptionalProperty(string name) => new InjectionProperty(name ?? throw new ArgumentNullException(nameof(name)), true); + public static InjectionMember OptionalProperty(string name) => new InjectionProperty(name ?? throw new ArgumentNullException(nameof(name)), ResolutionOption.Optional); #endregion }