Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed Jan 24, 2023
1 parent 2a9ac33 commit 9d59c9e
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/DivertR/CallArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public object this[int index]
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator CallArguments(object[] args) => new CallArguments(args);
public static implicit operator CallArguments(object[] args) => new(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void DecorateAction()
{
var root = rootFactory.Invoke(provider);

return redirect.RedirectSet.Settings.DiverterProxyDecorator.Decorate(redirect, root);
return redirect.RedirectSet.Settings.DiverterProxyFactory.CreateProxy(redirect, root);
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/DivertR/DiverterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DiverterSettings

public IProxyFactory ProxyFactory { get; }

public IDiverterProxyDecorator DiverterProxyDecorator { get; }
public IDiverterProxyFactory DiverterProxyFactory { get; }

public IRedirectProxyDecorator RedirectProxyDecorator { get; }

Expand Down Expand Up @@ -43,14 +43,14 @@ public static DiverterSettings Global

public DiverterSettings(
IProxyFactory? proxyFactory = null,
IDiverterProxyDecorator? diverterProxyDecorator = null,
IDiverterProxyFactory? diverterProxyFactory = null,
IRedirectProxyDecorator? redirectProxyDecorator = null,
bool defaultWithDummyRoot = true,
IDummyFactory? dummyFactory = null,
ICallInvoker? callInvoker = null)
{
ProxyFactory = proxyFactory ?? new DispatchProxyFactory();
DiverterProxyDecorator = diverterProxyDecorator ?? new DiverterProxyDecorator();
DiverterProxyFactory = diverterProxyFactory ?? new DiverterProxyFactory();
RedirectProxyDecorator = redirectProxyDecorator ?? new RedirectProxyDecorator();
DefaultWithDummyRoot = defaultWithDummyRoot;
DummyFactory = dummyFactory ?? new DummyFactory();
Expand Down
2 changes: 1 addition & 1 deletion src/DivertR/Dummy/Internal/DummyCallHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace DivertR.Dummy.Internal
{
public class DummyCallHandler : ICallHandler
{
private readonly DummyValueFactory _dummyValueFactory = new DummyValueFactory();
private readonly DummyValueFactory _dummyValueFactory = new();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object? Handle(IRedirectCall redirectCall)
Expand Down
2 changes: 1 addition & 1 deletion src/DivertR/Dummy/Internal/DummyValueFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DummyValueFactory()
[typeof(Tuple<,,,,>)] = CreateTupleOf,
[typeof(Tuple<,,,,,>)] = CreateTupleOf,
[typeof(Tuple<,,,,,,>)] = CreateTupleOf,
[typeof(Tuple<,,,,,,,>)] = CreateTupleOf,
[typeof(Tuple<,,,,,,,>)] = CreateTupleOf
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/DivertR/Dummy/Internal/TaskValueFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace DivertR.Dummy.Internal
{
internal class TaskValueFactory
{
private readonly ConcurrentDictionary<Type, IInternalTaskFactory> _taskFactories = new ConcurrentDictionary<Type, IInternalTaskFactory>();
private readonly ConcurrentDictionary<Type, IInternalValueTaskFactory> _valueTaskFactories = new ConcurrentDictionary<Type, IInternalValueTaskFactory>();
private readonly ConcurrentDictionary<Type, IInternalTaskFactory> _taskFactories = new();
private readonly ConcurrentDictionary<Type, IInternalValueTaskFactory> _valueTaskFactories = new();

public Task CreateTaskOf(Type taskType, DummyValueFactory dummyValueFactory)
{
Expand Down
7 changes: 0 additions & 7 deletions src/DivertR/IDiverterProxyDecorator.cs

This file was deleted.

7 changes: 7 additions & 0 deletions src/DivertR/IDiverterProxyFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DivertR
{
public interface IDiverterProxyFactory
{
object? CreateProxy(IRedirect redirect, object? root);
}
}
15 changes: 0 additions & 15 deletions src/DivertR/Internal/DiverterProxyDecorator.cs

This file was deleted.

15 changes: 15 additions & 0 deletions src/DivertR/Internal/DiverterProxyFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace DivertR.Internal
{
internal class DiverterProxyFactory : IDiverterProxyFactory
{
public object? CreateProxy(IRedirect redirect, object? root)
{
if (root == null)
{
return null;
}

return redirect.Proxy(root);
}
}
}
17 changes: 9 additions & 8 deletions src/DivertR/Internal/RedirectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ public IRedirectPlan RedirectPlan
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
// ReSharper disable once InconsistentlySynchronizedField: lock free read optimisation
if (!_planStack.TryPeek(out var redirectPlan))
if (_planStack.TryPeek(out var redirectPlan))
{
lock (_lockObject)
return redirectPlan;
}

lock (_lockObject)
{
if (!_planStack.TryPeek(out redirectPlan))
{
if (!_planStack.TryPeek(out redirectPlan))
{
// This state should never be possible
throw new InvalidOperationException("Unexpected empty redirect plan stack encountered.");
}
// This state should never be possible
throw new InvalidOperationException("Unexpected empty redirect plan stack encountered.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/DivertR/Internal/Relay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DivertR.Internal
{
internal class Relay<TTarget> : IRelay<TTarget> where TTarget : class?
{
private readonly AsyncLocal<ImmutableStack<RelayIndex<TTarget>>> _relayIndexStack = new AsyncLocal<ImmutableStack<RelayIndex<TTarget>>>();
private readonly AsyncLocal<ImmutableStack<RelayIndex<TTarget>>> _relayIndexStack = new();

private readonly ICallInvoker _callInvoker;
private readonly Lazy<TTarget> _next;
Expand Down
2 changes: 1 addition & 1 deletion src/DivertR/Internal/ValueTupleMapperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class ValueTupleMapperFactory
typeof(ValueTupleMapper), typeof(ValueTupleMapper<>), typeof(ValueTupleMapper<,>), typeof(ValueTupleMapper<,,>), typeof(ValueTupleMapper<,,,>), typeof(ValueTupleMapper<,,,,>), typeof(ValueTupleMapper<,,,,,>), typeof(ValueTupleMapper<,,,,,,>), typeof(ValueTupleMapper<,,,,,,,>)
};

private static readonly ConcurrentDictionary<Type, IValueTupleMapper> MapperCache = new ConcurrentDictionary<Type, IValueTupleMapper>();
private static readonly ConcurrentDictionary<Type, IValueTupleMapper> MapperCache = new();

public static IValueTupleMapper Create<TArgs>()
{
Expand Down
21 changes: 0 additions & 21 deletions src/DivertR/MatchCallConstraint.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/DivertR/Redirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ object IRedirect.Proxy()
[return: NotNull]
public TTarget Proxy(object? root)
{
if (root != null && !(root is TTarget))
if (root != null && root is not TTarget)
{
throw new ArgumentException($"Not assignable to {typeof(TTarget).Name}", nameof(root));
}
Expand Down
2 changes: 1 addition & 1 deletion src/DivertR/RedirectId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override int GetHashCode()

public override string ToString()
{
return $"Type:{Type.Name}" + (string.IsNullOrEmpty(Name) ? $" Name:<empty>" : $" Name: {Name}");
return $"Type:{Type.Name}" + (string.IsNullOrEmpty(Name) ? " Name:<empty>" : $" Name: {Name}");
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/DivertR/ViaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ internal static ActionViaBuilder<TTarget> ToSetInternal<TProperty>(Expression<Fu
{
if (memberExpression.Body == null) throw new ArgumentNullException(nameof(memberExpression));

constraintExpression ??= (() => Is<TProperty>.Any);
constraintExpression ??= () => Is<TProperty>.Any;
if (constraintExpression is { Body: null }) throw new ArgumentNullException(nameof(constraintExpression));

if (!(memberExpression.Body is MemberExpression propertyExpression))
if (memberExpression.Body is not MemberExpression propertyExpression)
{
throw new ArgumentException("Must be a property member expression", nameof(memberExpression));
}
Expand Down
2 changes: 1 addition & 1 deletion test/DivertR.UnitTests/MapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public void GivenCallConstraintMap_ShouldRecordAndMapCalls()
.ToList();

var echoes = _redirect
.To(new MatchCallConstraint<IFoo>(call => call.Method.Name == nameof(IFoo.Echo)))
.To(new CallConstraint<IFoo>(call => call.Method.Name == nameof(IFoo.Echo)))
.Record()
.Map(call => new { Input = call.Args[0] });

Expand Down

0 comments on commit 9d59c9e

Please sign in to comment.