From 18cf8e6187cfe7060f66586b81064f44dd140c91 Mon Sep 17 00:00:00 2001 From: David Naylor Date: Tue, 16 May 2023 15:58:56 +0100 Subject: [PATCH] Rename IDiverterBuilder.AddRedirect to WithRedirect --- src/DivertR/DiverterBuilder.cs | 36 +++++++++---------- src/DivertR/IDiverterBuilder.cs | 27 ++++++-------- src/DivertR/INestedDiverterBuilder.cs | 16 ++++----- src/DivertR/Internal/NestedDiverterBuilder.cs | 12 +++---- src/DivertR/Redirect.cs | 17 ++++++++- test/DivertR.UnitTests/DiverterTests.cs | 10 +++--- .../ServiceCollectionTests.cs | 12 +++---- .../TestHarness/WebAppFixture.cs | 2 +- 8 files changed, 70 insertions(+), 62 deletions(-) diff --git a/src/DivertR/DiverterBuilder.cs b/src/DivertR/DiverterBuilder.cs index ba68876..48c645b 100644 --- a/src/DivertR/DiverterBuilder.cs +++ b/src/DivertR/DiverterBuilder.cs @@ -12,6 +12,7 @@ public class DiverterBuilder : IDiverterBuilder private readonly ConcurrentDictionary> _decorators = new(); private readonly ConcurrentDictionary _registeredRedirects = new(); private readonly ConcurrentDictionary> _registeredNested = new(); + private readonly IRedirectSet _redirectSet; /// /// Create a instance. @@ -19,7 +20,7 @@ public class DiverterBuilder : IDiverterBuilder /// Optionally override default DivertR settings. public DiverterBuilder(DiverterSettings? settings = null) { - RedirectSet = new RedirectSet(settings); + _redirectSet = new RedirectSet(settings); } /// @@ -28,22 +29,19 @@ public DiverterBuilder(DiverterSettings? settings = null) /// The instance. public DiverterBuilder(IRedirectSet redirectSet) { - RedirectSet = redirectSet; + _redirectSet = redirectSet; } - - /// - public IRedirectSet RedirectSet { get; } /// - public IDiverterBuilder Register(Action>? nestedRegisterAction = null) where TTarget : class? + public IDiverterBuilder Register(Action>? nestedBuilderAction = null) where TTarget : class? { - return Register(null, nestedRegisterAction); + return Register(null, nestedBuilderAction); } /// - public IDiverterBuilder Register(string? name, Action>? nestedRegisterAction = null) where TTarget : class? + public IDiverterBuilder Register(string? name, Action>? nestedBuilderAction = null) where TTarget : class? { - var redirect = RedirectSet.GetOrCreate(name); + var redirect = _redirectSet.GetOrCreate(name); if (!_registeredRedirects.TryAdd(redirect.RedirectId, redirect)) { @@ -52,7 +50,7 @@ public IDiverterBuilder Register(string? name, Action(redirect, this)); + nestedBuilderAction?.Invoke(new NestedDiverterBuilder(redirect, this)); return this; } @@ -60,7 +58,7 @@ public IDiverterBuilder Register(string? name, Action public IDiverterBuilder Register(Type targetType, string? name = null) { - var redirect = RedirectSet.GetOrCreate(targetType, name); + var redirect = _redirectSet.GetOrCreate(targetType, name); if (!_registeredRedirects.TryAdd(redirect.RedirectId, redirect)) { @@ -180,24 +178,24 @@ public IDiverterBuilder Decorate(string? name, Type serviceType, Func - public IDiverterBuilder AddRedirect(Action>? nestedRegisterAction = null) where TTarget : class? + public IDiverterBuilder WithRedirect(Action>? nestedBuilderAction = null) where TTarget : class? { - return AddRedirect(null, nestedRegisterAction); + return WithRedirect(null, nestedBuilderAction); } /// - public IDiverterBuilder AddRedirect(string? name, Action>? nestedRegisterAction = null) where TTarget : class? + public IDiverterBuilder WithRedirect(string? name, Action>? nestedBuilderAction = null) where TTarget : class? { - var redirect = RedirectSet.GetOrCreate(name); - nestedRegisterAction?.Invoke(new NestedDiverterBuilder(redirect, this)); + var redirect = _redirectSet.GetOrCreate(name); + nestedBuilderAction?.Invoke(new NestedDiverterBuilder(redirect, this)); return this; } /// - public IDiverterBuilder AddRedirect(Type type, string? name = null) + public IDiverterBuilder WithRedirect(Type type, string? name = null) { - RedirectSet.GetOrCreate(type, name); + _redirectSet.GetOrCreate(type, name); return this; } @@ -212,7 +210,7 @@ IEnumerable GetDecorators(string? name = null) : Enumerable.Empty(); } - return new Diverter(RedirectSet, GetDecorators); + return new Diverter(_redirectSet, GetDecorators); } private void AddDecorator(string? name, IDiverterDecorator decorator) diff --git a/src/DivertR/IDiverterBuilder.cs b/src/DivertR/IDiverterBuilder.cs index a6767a1..53cd2f0 100644 --- a/src/DivertR/IDiverterBuilder.cs +++ b/src/DivertR/IDiverterBuilder.cs @@ -11,29 +11,24 @@ namespace DivertR /// public interface IDiverterBuilder { - /// - /// The underlying containing the set of registered instances. - /// - IRedirectSet RedirectSet { get; } - /// /// Register a service to redirect. The redirect is added with default . /// - /// Optional nested builder action. + /// Optional nested builder action. /// The type to register. /// This instance. /// Thrown if the type with with default has already been registered. - IDiverterBuilder Register(Action>? nestedRegisterAction = null) where TTarget : class?; + IDiverterBuilder Register(Action>? nestedBuilderAction = null) where TTarget : class?; /// /// Register a service to redirect. /// /// The decorator group and the of the added . - /// Optional nested builder action. + /// Optional nested builder action. /// The type to register. /// This instance. /// Thrown if the type with matching has already been registered. - IDiverterBuilder Register(string? name, Action>? nestedRegisterAction = null) where TTarget : class?; + IDiverterBuilder Register(string? name, Action>? nestedBuilderAction = null) where TTarget : class?; /// /// Register a service to redirect. @@ -173,21 +168,21 @@ public interface IDiverterBuilder IDiverterBuilder Decorate(string? name, Type serviceType, Func decorator); /// - /// Add a standalone without registering a service decorator. The redirect is added with default . + /// Register a standalone with no dependency injection service redirection. The redirect is added with default . /// - /// Optional nested builder action. + /// Optional nested builder action. /// The target type. /// This instance. - IDiverterBuilder AddRedirect(Action>? nestedRegisterAction = null) where TTarget : class?; + IDiverterBuilder WithRedirect(Action>? nestedBuilderAction = null) where TTarget : class?; /// - /// Add a standalone without registering a service decorator. + /// Register a standalone with no dependency injection service redirection. /// /// The of the added . - /// Optional nested builder action. + /// Optional nested builder action. /// The target type. /// This instance. - IDiverterBuilder AddRedirect(string? name, Action>? nestedRegisterAction = null) where TTarget : class?; + IDiverterBuilder WithRedirect(string? name, Action>? nestedBuilderAction = null) where TTarget : class?; /// /// Add a standalone without registering a service decorator. @@ -195,7 +190,7 @@ public interface IDiverterBuilder /// The target type. /// The of the added . /// This instance. - IDiverterBuilder AddRedirect(Type type, string? name = null); + IDiverterBuilder WithRedirect(Type type, string? name = null); /// /// Create an instance from the current registrations. diff --git a/src/DivertR/INestedDiverterBuilder.cs b/src/DivertR/INestedDiverterBuilder.cs index 0e732c9..8c8b61f 100644 --- a/src/DivertR/INestedDiverterBuilder.cs +++ b/src/DivertR/INestedDiverterBuilder.cs @@ -4,7 +4,7 @@ namespace DivertR { /// - /// A builder interface for nested Diverter builder actions. + /// A nested Diverter builder interface for registering nested actions. /// public interface INestedDiverterBuilder where TTarget : class? { @@ -12,29 +12,29 @@ public interface INestedDiverterBuilder where TTarget : class? /// Redirect calls with return type via an . /// The returned instances are proxied by inserting a persistent on the parent . /// - /// Optional nested register action. + /// Optional nested builder action. /// The return type of calls to redirect. /// This instance. /// Thrown if a nested has already been registered on the parent with matching type and default . - INestedDiverterBuilder AddRedirect(Action>? registerAction = null) where TReturn : class?; + INestedDiverterBuilder AddRedirect(Action>? nestedBuilderAction = null) where TReturn : class?; /// /// Redirect calls with return type via an . /// The returned instances are proxied by inserting a persistent on the parent . /// /// Specify the of the returned . - /// Optional nested register action. + /// Optional nested builder action. /// The return type of calls to redirect. /// This instance. /// Thrown if a nested has already been registered on the parent with matching type and . - INestedDiverterBuilder AddRedirect(string? name, Action>? registerAction = null) where TReturn : class?; + INestedDiverterBuilder AddRedirect(string? name, Action>? nestedBuilderAction = null) where TReturn : class?; /// /// Redirect calls with return type and matching a call constraint via an . /// The returned instances are proxied by inserting a persistent on the parent . /// /// The call constraint expression. - /// Optional nested register action. + /// Optional nested builder action. /// The return type of calls to redirect. /// This instance. INestedDiverterBuilder AddRedirect(Expression> constraintExpression, Action>? registerAction = null) where TReturn : class?; @@ -45,9 +45,9 @@ public interface INestedDiverterBuilder where TTarget : class? /// /// Specify the of the returned . /// The call constraint expression. - /// Optional nested register action. + /// Optional nested builder action. /// The return type of calls to redirect. /// This instance. - INestedDiverterBuilder AddRedirect(string? name, Expression> constraintExpression, Action>? registerAction = null) where TReturn : class?; + INestedDiverterBuilder AddRedirect(string? name, Expression> constraintExpression, Action>? nestedBuilderAction = null) where TReturn : class?; } } \ No newline at end of file diff --git a/src/DivertR/Internal/NestedDiverterBuilder.cs b/src/DivertR/Internal/NestedDiverterBuilder.cs index 0bc3726..d816023 100644 --- a/src/DivertR/Internal/NestedDiverterBuilder.cs +++ b/src/DivertR/Internal/NestedDiverterBuilder.cs @@ -14,12 +14,12 @@ public NestedDiverterBuilder(IRedirect redirect, DiverterBuilder divert _diverterBuilder = diverterBuilder; } - public INestedDiverterBuilder AddRedirect(Action>? registerAction = null) where TReturn : class? + public INestedDiverterBuilder AddRedirect(Action>? nestedBuilderAction = null) where TReturn : class? { - return AddRedirect((string?) null, registerAction); + return AddRedirect((string?) null, nestedBuilderAction); } - public INestedDiverterBuilder AddRedirect(string? name, Action>? registerAction = null) where TReturn : class? + public INestedDiverterBuilder AddRedirect(string? name, Action>? nestedBuilderAction = null) where TReturn : class? { var nestedRedirect = _redirect.RedirectSet.GetOrCreate(name); @@ -34,7 +34,7 @@ public INestedDiverterBuilder AddRedirect(string? name, Action opt.Persist(); }); - registerAction?.Invoke(new NestedDiverterBuilder(nestedRedirect, _diverterBuilder)); + nestedBuilderAction?.Invoke(new NestedDiverterBuilder(nestedRedirect, _diverterBuilder)); return this; } @@ -44,7 +44,7 @@ public INestedDiverterBuilder AddRedirect(Expression AddRedirect(string? name, Expression> constraintExpression, Action>? registerAction = null) where TReturn : class? + public INestedDiverterBuilder AddRedirect(string? name, Expression> constraintExpression, Action>? nestedBuilderAction = null) where TReturn : class? { var nestedRedirect = _redirect .To(constraintExpression) @@ -54,7 +54,7 @@ public INestedDiverterBuilder AddRedirect(string? name, Expres opt.Persist(); }); - registerAction?.Invoke(new NestedDiverterBuilder(nestedRedirect, _diverterBuilder)); + nestedBuilderAction?.Invoke(new NestedDiverterBuilder(nestedRedirect, _diverterBuilder)); return this; } diff --git a/src/DivertR/Redirect.cs b/src/DivertR/Redirect.cs index 1a578fc..860214c 100644 --- a/src/DivertR/Redirect.cs +++ b/src/DivertR/Redirect.cs @@ -14,18 +14,33 @@ public class Redirect : IRedirect where TTarget : class? private readonly Relay _relay; private readonly RedirectProxyCall _redirectProxyCall; private readonly ConditionalWeakTable _proxyCache = new(); - + + /// + /// Constructor - instantiates a instance. + /// + /// The of the . + /// Optionally provide custom otherwise the default is used. + /// Optionally provide a custom . public Redirect(string? name = null, DiverterSettings? diverterSettings = null, IRedirectRepository? redirectRepository = null) : this(RedirectId.From(name), new RedirectSet(diverterSettings), redirectRepository) { ((RedirectSet) RedirectSet).AddRedirect(this); } + /// + /// Constructor - instantiates a instance. + /// + /// Optionally provide custom otherwise the default is used. + /// Optionally provide a custom . public Redirect(DiverterSettings? diverterSettings, IRedirectRepository? redirectRepository = null) : this(name: null, diverterSettings: diverterSettings, redirectRepository: redirectRepository) { } + /// + /// Constructor - instantiates a instance. + /// + /// Optionally provide a custom . public Redirect(IRedirectRepository? redirectRepository) : this(name: null, diverterSettings: null, redirectRepository: redirectRepository) { diff --git a/test/DivertR.UnitTests/DiverterTests.cs b/test/DivertR.UnitTests/DiverterTests.cs index 4284253..0c28cbc 100644 --- a/test/DivertR.UnitTests/DiverterTests.cs +++ b/test/DivertR.UnitTests/DiverterTests.cs @@ -282,7 +282,7 @@ public void GivenDiverterWithNonRegisteredViaRedirect_WhenGetUnregisteredRedirec public void AddRedirect_ShouldAdd() { // ARRANGE - var diverter = _diverterBuilder.AddRedirect().Create(); + var diverter = _diverterBuilder.WithRedirect().Create(); // ACT var foo = diverter.Redirect().Proxy(); @@ -295,7 +295,7 @@ public void AddRedirect_ShouldAdd() public void AddNamedRedirect_ShouldAdd() { // ARRANGE - var diverter = _diverterBuilder.AddRedirect("test").Create(); + var diverter = _diverterBuilder.WithRedirect("test").Create(); // ACT var foo = diverter.Redirect("test").Proxy(); @@ -308,7 +308,7 @@ public void AddNamedRedirect_ShouldAdd() public void AddRedirectByType_ShouldAdd() { // ARRANGE - var diverter = _diverterBuilder.AddRedirect(typeof(IFoo)).Create(); + var diverter = _diverterBuilder.WithRedirect(typeof(IFoo)).Create(); // ACT var foo = diverter.Redirect().Proxy(); @@ -321,7 +321,7 @@ public void AddRedirectByType_ShouldAdd() public void AddNamedRedirectByType_ShouldAdd() { // ARRANGE - var diverter = _diverterBuilder.AddRedirect(typeof(IFoo), "test").Create(); + var diverter = _diverterBuilder.WithRedirect(typeof(IFoo), "test").Create(); // ACT var foo = diverter.Redirect("test").Proxy(); @@ -335,7 +335,7 @@ public void AddNestedRedirect_ShouldProxyChain() { // ARRANGE var diverter = _diverterBuilder - .AddRedirect(foo => foo + .WithRedirect(foo => foo .AddRedirect()) .Create(); diff --git a/test/DivertR.UnitTests/ServiceCollectionTests.cs b/test/DivertR.UnitTests/ServiceCollectionTests.cs index 53c6329..606735b 100644 --- a/test/DivertR.UnitTests/ServiceCollectionTests.cs +++ b/test/DivertR.UnitTests/ServiceCollectionTests.cs @@ -99,7 +99,7 @@ public void ShouldDecorateWithDiverterParam() _services.AddSingleton(); var diverter = new DiverterBuilder() - .AddRedirect() + .WithRedirect() .Decorate((foo, diverter) => { var bar = diverter.Redirect().Proxy(new Bar("test")); @@ -121,7 +121,7 @@ public void ShouldDecorateWithProviderParam() _services.AddSingleton(_ => new Bar("test")); var diverter = new DiverterBuilder() - .AddRedirect() + .WithRedirect() .Decorate((foo, diverter, provider) => { var bar = diverter.Redirect().Proxy(provider.GetRequiredService()); @@ -154,7 +154,7 @@ public void ShouldDecorateWithObjectDelegateAndDiverterParam() _services.AddSingleton(); var diverter = new DiverterBuilder() - .AddRedirect() + .WithRedirect() .Decorate(typeof(IFoo), (foo, diverter) => { var bar = diverter.Redirect().Proxy(new Bar("test")); @@ -176,7 +176,7 @@ public void ShouldDecorateWithObjectDelegateAndProviderParam() _services.AddSingleton(_ => new Bar("test")); var diverter = new DiverterBuilder() - .AddRedirect() + .WithRedirect() .Decorate(typeof(IFoo), (foo, diverter, provider) => { var bar = diverter.Redirect().Proxy(provider.GetRequiredService()); @@ -331,8 +331,8 @@ public void GivenListDecoratorCanRedirectProxyItems() _services.AddTransient>(_ => new List { new Foo("1"), new Foo("2") }); var diverterBuilder = new DiverterBuilder(); var diverter = diverterBuilder - .Decorate>(foos => foos - .Select(x => diverterBuilder.RedirectSet.GetOrCreate().Proxy(x)) + .Decorate>((foos, diverter) => foos + .Select(x => diverter.Redirect().Proxy(x)) .ToList()).Create(); _services.Divert(diverter); diff --git a/test/DivertR.WebAppTests/TestHarness/WebAppFixture.cs b/test/DivertR.WebAppTests/TestHarness/WebAppFixture.cs index c1e7f16..ad099e3 100644 --- a/test/DivertR.WebAppTests/TestHarness/WebAppFixture.cs +++ b/test/DivertR.WebAppTests/TestHarness/WebAppFixture.cs @@ -20,7 +20,7 @@ public class WebAppFixture .Register(x => x .AddRedirect()) // Add nested redirects to divert inner services not resolved by DI e.g. by factories .Register() - .AddRedirect() // Add a standalone (non DI service) redirect for proxying the xUnit ITestOutputHelper logger. + .WithRedirect() // Add a standalone (non DI service) redirect for proxying the xUnit ITestOutputHelper logger. .Create(); private readonly WebApplicationFactory _webApplicationFactory;