Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DummyFactory to insert redirects using a DummyBuilder #8

Merged
merged 2 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions src/DivertR/DiverterSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using DivertR.DispatchProxy;
using DivertR.Dummy;
using DivertR.Dummy.Internal;
using DivertR.Internal;

namespace DivertR
{
Expand All @@ -15,10 +13,8 @@ public class DiverterSettings
public bool DefaultWithDummyRoot { get; }

public IDummyFactory DummyFactory { get; }

public IRedirectRepository DummyRedirectRepository { get; }


public static DiverterSettings Global
{
get
Expand All @@ -41,20 +37,11 @@ public static DiverterSettings Global
public DiverterSettings(
IProxyFactory? proxyFactory = null,
bool defaultWithDummyRoot = true,
IRedirectRepository? dummyRedirectRepository = null,
IDummyFactory? defaultRootFactory = null)
IDummyFactory? dummyFactory = null)
{
ProxyFactory = proxyFactory ?? new DispatchProxyFactory();
DefaultWithDummyRoot = defaultWithDummyRoot;
DummyRedirectRepository = dummyRedirectRepository ?? CreateDummyRepository();
DummyFactory = defaultRootFactory ?? new DummyFactory();
}

private static IRedirectRepository CreateDummyRepository()
{
var redirect = new Redirect(new DummyCallHandler());

return new RedirectRepository(new[] { redirect });
DummyFactory = dummyFactory ?? new DummyFactory();
}
}
}
44 changes: 44 additions & 0 deletions src/DivertR/Dummy/DummyFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Linq.Expressions;
using DivertR.Dummy.Internal;
using DivertR.Internal;

namespace DivertR.Dummy
{
public class DummyFactory : IDummyFactory
{
public DummyFactory()
{
var redirect = new Redirect(new DummyCallHandler());
RedirectRepository = new RedirectRepository(new[] { redirect });
}

public DummyFactory(IRedirectRepository redirectRepository)
{
RedirectRepository = redirectRepository;
}

public TTarget Create<TTarget>(DiverterSettings diverterSettings) where TTarget : class
{
var via = new Via<TTarget>(diverterSettings, RedirectRepository);

return via.Proxy(false);
}

public IRedirectRepository RedirectRepository { get; }

public IDummyBuilder<TReturn> To<TReturn>(Expression<Func<TReturn>> constraintExpression)
{
var redirectBuilder = RedirectBuilder.To(constraintExpression);

return new DummyBuilder<TReturn>(RedirectRepository, redirectBuilder);
}

public IDummyBuilder To(ICallConstraint? callConstraint = null)
{
var redirectBuilder = RedirectBuilder.To(callConstraint);

return new DummyBuilder(RedirectRepository, redirectBuilder);
}
}
}
22 changes: 22 additions & 0 deletions src/DivertR/Dummy/IDummyBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace DivertR.Dummy
{
public interface IDummyBuilder<TReturn>
{
IDummyBuilder<TReturn> AddConstraint(ICallConstraint callConstraint);
IDummyBuilder<TReturn> Redirect(TReturn instance, Action<IRedirectOptionsBuilder>? optionsAction = null);
IDummyBuilder<TReturn> Redirect(Func<TReturn> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
IDummyBuilder<TReturn> Redirect(Func<IFuncRedirectCall<TReturn>, TReturn> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
IDummyBuilder<TReturn> Redirect(Func<IFuncRedirectCall<TReturn>, CallArguments, TReturn> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
}

public interface IDummyBuilder
{
IDummyBuilder AddConstraint(ICallConstraint callConstraint);
IDummyBuilder Redirect(object instance, Action<IRedirectOptionsBuilder>? optionsAction = null);
IDummyBuilder Redirect(Func<object> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
IDummyBuilder Redirect(Func<IRedirectCall, object> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
IDummyBuilder Redirect(Func<IRedirectCall, CallArguments, object> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
}
}
106 changes: 106 additions & 0 deletions src/DivertR/Dummy/Internal/DummyBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;

namespace DivertR.Dummy.Internal
{
internal class DummyBuilder<TReturn> : IDummyBuilder<TReturn>
{
private readonly IRedirectRepository _redirectRepository;
private readonly IFuncRedirectBuilder<TReturn> _redirectBuilder;

public DummyBuilder(IRedirectRepository redirectRepository, IFuncRedirectBuilder<TReturn> redirectBuilder)
{
_redirectRepository = redirectRepository;
_redirectBuilder = redirectBuilder;
}

public IDummyBuilder<TReturn> AddConstraint(ICallConstraint callConstraint)
{
_redirectBuilder.AddConstraint(callConstraint);

return this;
}

public IDummyBuilder<TReturn> Redirect(TReturn instance, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(instance, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}

public IDummyBuilder<TReturn> Redirect(Func<TReturn> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(redirectDelegate, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}

public IDummyBuilder<TReturn> Redirect(Func<IFuncRedirectCall<TReturn>, TReturn> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(redirectDelegate, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}

public IDummyBuilder<TReturn> Redirect(Func<IFuncRedirectCall<TReturn>, CallArguments, TReturn> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(redirectDelegate, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}
}

internal class DummyBuilder : IDummyBuilder
{
private readonly IRedirectRepository _redirectRepository;
private readonly IRedirectBuilder _redirectBuilder;

public DummyBuilder(IRedirectRepository redirectRepository, IRedirectBuilder redirectBuilder)
{
_redirectRepository = redirectRepository;
_redirectBuilder = redirectBuilder;
}

public IDummyBuilder AddConstraint(ICallConstraint callConstraint)
{
_redirectBuilder.AddConstraint(callConstraint);

return this;
}

public IDummyBuilder Redirect(object instance, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(instance, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}

public IDummyBuilder Redirect(Func<object> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(redirectDelegate, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}

public IDummyBuilder Redirect(Func<IRedirectCall, object> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(redirectDelegate, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}

public IDummyBuilder Redirect(Func<IRedirectCall, CallArguments, object> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null)
{
var redirect = _redirectBuilder.Build(redirectDelegate, optionsAction);
_redirectRepository.InsertRedirect(redirect);

return this;
}
}
}
12 changes: 0 additions & 12 deletions src/DivertR/Dummy/Internal/DummyFactory.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/DivertR/IActionViaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace DivertR
{
public interface IActionViaBuilder<TTarget> : IDelegateViaBuilder<TTarget> where TTarget : class
public interface IActionViaBuilder<TTarget> : IViaBuilder<TTarget> where TTarget : class
{
new IActionRedirectBuilder<TTarget> RedirectBuilder { get; }

new IActionViaBuilder<TTarget> AddConstraint(ICallConstraint<TTarget> callConstraint);

new IActionViaBuilder<TTarget> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IActionViaBuilder<TTarget> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IActionViaBuilder<TTarget> Redirect(Action redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IActionViaBuilder<TTarget> Redirect(Action<IActionRedirectCall<TTarget>> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IActionViaBuilder<TTarget> Redirect(Action<IActionRedirectCall<TTarget>, CallArguments> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
Expand Down
11 changes: 0 additions & 11 deletions src/DivertR/IDelegateViaBuilder.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/DivertR/IFuncViaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace DivertR
{
public interface IFuncViaBuilder<TTarget, TReturn> : IDelegateViaBuilder<TTarget> where TTarget : class
public interface IFuncViaBuilder<TTarget, TReturn> : IViaBuilder<TTarget> where TTarget : class
{
new IFuncRedirectBuilder<TTarget, TReturn> RedirectBuilder { get; }

new IFuncViaBuilder<TTarget, TReturn> AddConstraint(ICallConstraint<TTarget> callConstraint);

new IFuncViaBuilder<TTarget, TReturn> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IFuncViaBuilder<TTarget, TReturn> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IFuncViaBuilder<TTarget, TReturn> Redirect(TReturn instance, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IFuncViaBuilder<TTarget, TReturn> Redirect(Func<TReturn> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IFuncViaBuilder<TTarget, TReturn> Redirect(Func<IFuncRedirectCall<TTarget, TReturn>, TReturn> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
Expand Down
10 changes: 8 additions & 2 deletions src/DivertR/IRedirectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ namespace DivertR
public interface IRedirectBuilder<TTarget> where TTarget : class
{
IRedirectBuilder<TTarget> AddConstraint(ICallConstraint<TTarget> callConstraint);
IRedirect Build(TTarget target, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IRedirect Build(object? instance, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IRedirect Build(Func<object?> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IRedirect Build(Func<IRedirectCall<TTarget>, object?> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IRedirect Build(Func<IRedirectCall<TTarget>, CallArguments, object?> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IRedirect Build(ICallHandler<TTarget> callHandler, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IRedirect Build(ICallHandler<TTarget> callHandler, IRedirectOptions redirectOptions);
IRecordRedirect<TTarget> Record(Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
Expand All @@ -15,7 +18,10 @@ public interface IRedirectBuilder<TTarget> where TTarget : class
public interface IRedirectBuilder
{
IRedirectBuilder AddConstraint(ICallConstraint callConstraint);
IRedirect Build(object target, Action<IRedirectOptionsBuilder>? optionsAction = null);
IRedirect Build(object? instance, Action<IRedirectOptionsBuilder>? optionsAction = null);
IRedirect Build(Func<object?> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
IRedirect Build(Func<IRedirectCall, object?> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
IRedirect Build(Func<IRedirectCall, CallArguments, object?> redirectDelegate, Action<IRedirectOptionsBuilder>? optionsAction = null);
IRedirect Build(ICallHandler callHandler, Action<IRedirectOptionsBuilder>? optionsAction = null);
IRedirect Build(ICallHandler callHandler, IRedirectOptions redirectOptions);
IRecordRedirect Record(Action<IRedirectOptionsBuilder>? optionsAction = null);
Expand Down
4 changes: 4 additions & 0 deletions src/DivertR/IViaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public interface IViaBuilder<TTarget> where TTarget : class
IRedirectBuilder<TTarget> RedirectBuilder { get; }

IViaBuilder<TTarget> AddConstraint(ICallConstraint<TTarget> callConstraint);
IViaBuilder<TTarget> Redirect(object? instance, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IViaBuilder<TTarget> Redirect(Func<object?> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IViaBuilder<TTarget> Redirect(Func<IRedirectCall<TTarget>, object?> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IViaBuilder<TTarget> Redirect(Func<IRedirectCall<TTarget>, CallArguments, object?> redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IViaBuilder<TTarget> Retarget(TTarget target, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
IRecordStream<TTarget> Record(Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null);
}
Expand Down
7 changes: 4 additions & 3 deletions src/DivertR/Internal/ActionViaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DivertR.Internal
{
internal class ActionViaBuilder<TTarget> : DelegateViaBuilder<TTarget>, IActionViaBuilder<TTarget> where TTarget : class
internal class ActionViaBuilder<TTarget> : ViaBuilder<TTarget>, IActionViaBuilder<TTarget> where TTarget : class
{
public ActionViaBuilder(IVia<TTarget> via, IActionRedirectBuilder<TTarget> redirectBuilder)
: base(via, redirectBuilder)
Expand All @@ -21,9 +21,10 @@ public ActionViaBuilder(IVia<TTarget> via, IActionRedirectBuilder<TTarget> redir
return this;
}

public new IActionViaBuilder<TTarget> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null)
public IActionViaBuilder<TTarget> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null)
{
base.Redirect(redirectDelegate, optionsAction);
var redirect = RedirectBuilder.Build(redirectDelegate, optionsAction);
Via.RedirectRepository.InsertRedirect(redirect);

return this;
}
Expand Down
23 changes: 0 additions & 23 deletions src/DivertR/Internal/DelegateViaBuilder.cs

This file was deleted.

7 changes: 4 additions & 3 deletions src/DivertR/Internal/FuncViaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DivertR.Internal
{
internal class FuncViaBuilder<TTarget, TReturn> : DelegateViaBuilder<TTarget>, IFuncViaBuilder<TTarget, TReturn> where TTarget : class
internal class FuncViaBuilder<TTarget, TReturn> : ViaBuilder<TTarget>, IFuncViaBuilder<TTarget, TReturn> where TTarget : class
{
public FuncViaBuilder(IVia<TTarget> via, IFuncRedirectBuilder<TTarget, TReturn> redirectBuilder)
: base(via, redirectBuilder)
Expand All @@ -21,9 +21,10 @@ public FuncViaBuilder(IVia<TTarget> via, IFuncRedirectBuilder<TTarget, TReturn>
return this;
}

public new IFuncViaBuilder<TTarget, TReturn> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null)
public IFuncViaBuilder<TTarget, TReturn> Redirect(Delegate redirectDelegate, Action<IRedirectOptionsBuilder<TTarget>>? optionsAction = null)
{
base.Redirect(redirectDelegate, optionsAction);
var redirect = RedirectBuilder.Build(redirectDelegate, optionsAction);
Via.RedirectRepository.InsertRedirect(redirect);

return this;
}
Expand Down
Loading