Skip to content

Commit

Permalink
Added additional constructor to DependencyOverride
Browse files Browse the repository at this point in the history
Fixed #158
  • Loading branch information
ENikS committed Jun 3, 2020
1 parent 8712886 commit a085539
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/Dependency/Resolution/Overrides/DependencyOverride.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ namespace Unity.Resolution
/// given type, regardless of where it appears in the object graph.
/// </summary>
public class DependencyOverride : ResolverOverride,
IEquatable<NamedType>,
IResolve
IEquatable<NamedType>
{
#region Fields

protected readonly object Value;
protected readonly Type Type;

#endregion

Expand All @@ -27,9 +26,9 @@ public class DependencyOverride : ResolverOverride,
/// <param name="type">Type of the dependency.</param>
/// <param name="value">Value to override with</param>
public DependencyOverride(Type type, object value)
: base(null, type, null)
: base(null, value)
{
Value = value;
Type = type;
}

/// <summary>
Expand All @@ -39,9 +38,8 @@ public DependencyOverride(Type type, object value)
/// <param name="name">Name of the dependency</param>
/// <param name="value">Value to override with</param>
public DependencyOverride(string name, object value)
: base(null, null, name)
: base(name, value)
{
Value = value;
}


Expand All @@ -53,23 +51,23 @@ public DependencyOverride(string name, object value)
/// <param name="type">Type of the dependency.</param>
/// <param name="value">Value to override with</param>
public DependencyOverride(Type type, string name, object value)
: base(null, type, name)
: base(name, value)
{
Value = value;
Type = type;
}

/// <summary>
/// Create an instance of <see cref="DependencyOverride"/> to override
/// dependency on cpecific type matching the given type and a name
/// </summary>
/// <param name="target">Target type</param>
/// <param name="target">Target type to override dependency on</param>
/// <param name="name">Name of the dependency</param>
/// <param name="type">Type of the dependency.</param>
/// <param name="value">Value to override with</param>
public DependencyOverride(Type target, Type type, string name, object value)
: base(target, type, name)
: base(target, name, value)
{
Value = value;
Type = type;
}

#endregion
Expand Down Expand Up @@ -105,26 +103,6 @@ public bool Equals(NamedType other)
}

#endregion


#region IResolverPolicy

public object Resolve<TContext>(ref TContext context)
where TContext : IResolveContext
{
if (Value is IResolve policy)
return policy.Resolve(ref context);

if (Value is IResolverFactory<Type> factory)
{
var resolveDelegate = factory.GetResolver<TContext>(Type);
return resolveDelegate(ref context);
}

return Value;
}

#endregion
}

/// <summary>
Expand All @@ -134,6 +112,24 @@ public object Resolve<TContext>(ref TContext context)
/// <typeparam name="T">Type of the dependency to override.</typeparam>
public class DependencyOverride<T> : DependencyOverride
{
/// <summary>
/// Create an instance of <see cref="DependencyOverride"/> to override
/// dependencies matching the given type and a name
/// </summary>
/// <remarks>
/// This constructor creates an override that will match with any
/// target type as long as the dependency type and name match. To
/// target specific type use <see cref="ResolverOverride.OnType(Type)"/>
/// method.
/// </remarks>
/// <param name="target">Target type to override dependency on</param>
/// <param name="name">Name of the dependency</param>
/// <param name="value">Override value</param>
public DependencyOverride(Type target, string name, object value)
: base(target, typeof(T), name, value)
{
}

/// <summary>
/// Create an instance of <see cref="DependencyOverride"/> to override
/// dependencies matching the given type and a name
Expand Down

0 comments on commit a085539

Please sign in to comment.