Skip to content

Commit

Permalink
Rename IDiverterBuilder.AddRedirect to WithRedirect
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed May 16, 2023
1 parent 15f343b commit 18cf8e6
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 62 deletions.
36 changes: 17 additions & 19 deletions src/DivertR/DiverterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ public class DiverterBuilder : IDiverterBuilder
private readonly ConcurrentDictionary<string, ConcurrentQueue<IDiverterDecorator>> _decorators = new();
private readonly ConcurrentDictionary<RedirectId, IRedirect> _registeredRedirects = new();
private readonly ConcurrentDictionary<RedirectId, ConcurrentDictionary<RedirectId, RedirectId>> _registeredNested = new();
private readonly IRedirectSet _redirectSet;

/// <summary>
/// Create a <see cref="DiverterBuilder"/> instance.
/// </summary>
/// <param name="settings">Optionally override default DivertR settings.</param>
public DiverterBuilder(DiverterSettings? settings = null)
{
RedirectSet = new RedirectSet(settings);
_redirectSet = new RedirectSet(settings);
}

/// <summary>
Expand All @@ -28,22 +29,19 @@ public DiverterBuilder(DiverterSettings? settings = null)
/// <param name="redirectSet">The <see cref="IRedirectSet"/> instance.</param>
public DiverterBuilder(IRedirectSet redirectSet)
{
RedirectSet = redirectSet;
_redirectSet = redirectSet;
}

/// <inheritdoc />
public IRedirectSet RedirectSet { get; }

/// <inheritdoc />
public IDiverterBuilder Register<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?
public IDiverterBuilder Register<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?
{
return Register(null, nestedRegisterAction);
return Register(null, nestedBuilderAction);
}

/// <inheritdoc />
public IDiverterBuilder Register<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?
public IDiverterBuilder Register<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?
{
var redirect = RedirectSet.GetOrCreate<TTarget>(name);
var redirect = _redirectSet.GetOrCreate<TTarget>(name);

if (!_registeredRedirects.TryAdd(redirect.RedirectId, redirect))
{
Expand All @@ -52,15 +50,15 @@ public IDiverterBuilder Register<TTarget>(string? name, Action<INestedDiverterBu

AddDecorator(name, new RedirectDecorator(redirect));

nestedRegisterAction?.Invoke(new NestedDiverterBuilder<TTarget>(redirect, this));
nestedBuilderAction?.Invoke(new NestedDiverterBuilder<TTarget>(redirect, this));

return this;
}

/// <inheritdoc />
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))
{
Expand Down Expand Up @@ -180,24 +178,24 @@ public IDiverterBuilder Decorate(string? name, Type serviceType, Func<object, ID
}

/// <inheritdoc />
public IDiverterBuilder AddRedirect<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?
public IDiverterBuilder WithRedirect<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?
{
return AddRedirect(null, nestedRegisterAction);
return WithRedirect(null, nestedBuilderAction);
}

/// <inheritdoc />
public IDiverterBuilder AddRedirect<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?
public IDiverterBuilder WithRedirect<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?
{
var redirect = RedirectSet.GetOrCreate<TTarget>(name);
nestedRegisterAction?.Invoke(new NestedDiverterBuilder<TTarget>(redirect, this));
var redirect = _redirectSet.GetOrCreate<TTarget>(name);
nestedBuilderAction?.Invoke(new NestedDiverterBuilder<TTarget>(redirect, this));

return this;
}

/// <inheritdoc />
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;
}
Expand All @@ -212,7 +210,7 @@ IEnumerable<IDiverterDecorator> GetDecorators(string? name = null)
: Enumerable.Empty<IDiverterDecorator>();
}

return new Diverter(RedirectSet, GetDecorators);
return new Diverter(_redirectSet, GetDecorators);
}

private void AddDecorator(string? name, IDiverterDecorator decorator)
Expand Down
27 changes: 11 additions & 16 deletions src/DivertR/IDiverterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,24 @@ namespace DivertR
/// </summary>
public interface IDiverterBuilder
{
/// <summary>
/// The underlying <see cref="IRedirectSet"/> containing the set of registered <see cref="IRedirect"/> instances.
/// </summary>
IRedirectSet RedirectSet { get; }

/// <summary>
/// Register a service to redirect. The redirect is added with default <see cref="RedirectId.Name" />.
/// </summary>
/// <param name="nestedRegisterAction">Optional nested builder action.</param>
/// <param name="nestedBuilderAction">Optional nested builder action.</param>
/// <typeparam name="TTarget">The type to register.</typeparam>
/// <returns>This <see cref="IDiverterBuilder"/> instance.</returns>
/// <exception cref="DiverterException">Thrown if the <typeparamref name="TTarget"/> type with with default <see cref="RedirectId.Name" /> has already been registered.</exception>
IDiverterBuilder Register<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?;
IDiverterBuilder Register<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?;

/// <summary>
/// Register a service to redirect.
/// </summary>
/// <param name="name">The decorator group and the <see cref="RedirectId.Name" /> of the added <see cref="IRedirect"/>.</param>
/// <param name="nestedRegisterAction">Optional nested builder action.</param>
/// <param name="nestedBuilderAction">Optional nested builder action.</param>
/// <typeparam name="TTarget">The type to register.</typeparam>
/// <returns>This <see cref="IDiverterBuilder"/> instance.</returns>
/// <exception cref="DiverterException">Thrown if the <typeparamref name="TTarget"/> type with matching <paramref name="name"/> has already been registered.</exception>
IDiverterBuilder Register<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?;
IDiverterBuilder Register<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?;

/// <summary>
/// Register a service to redirect.
Expand Down Expand Up @@ -173,29 +168,29 @@ public interface IDiverterBuilder
IDiverterBuilder Decorate(string? name, Type serviceType, Func<object, IDiverter, IServiceProvider, object> decorator);

/// <summary>
/// Add a standalone <see cref="IRedirect{TTarget}"/> without registering a service decorator. The redirect is added with default <see cref="RedirectId.Name" />.
/// Register a standalone <see cref="IRedirect{TTarget}"/> with no dependency injection service redirection. The redirect is added with default <see cref="RedirectId.Name" />.
/// </summary>
/// <param name="nestedRegisterAction">Optional nested builder action.</param>
/// <param name="nestedBuilderAction">Optional nested builder action.</param>
/// <typeparam name="TTarget">The <see cref="IRedirect{TTarget}"/> target type.</typeparam>
/// <returns>This <see cref="IDiverterBuilder"/> instance.</returns>
IDiverterBuilder AddRedirect<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?;
IDiverterBuilder WithRedirect<TTarget>(Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?;

/// <summary>
/// Add a standalone <see cref="IRedirect{TTarget}"/> without registering a service decorator.
/// Register a standalone <see cref="IRedirect{TTarget}"/> with no dependency injection service redirection.
/// </summary>
/// <param name="name">The <see cref="RedirectId.Name" /> of the added <see cref="IRedirect"/>.</param>
/// <param name="nestedRegisterAction">Optional nested builder action.</param>
/// <param name="nestedBuilderAction">Optional nested builder action.</param>
/// <typeparam name="TTarget">The <see cref="IRedirect{TTarget}"/> target type.</typeparam>
/// <returns>This <see cref="IDiverterBuilder"/> instance.</returns>
IDiverterBuilder AddRedirect<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedRegisterAction = null) where TTarget : class?;
IDiverterBuilder WithRedirect<TTarget>(string? name, Action<INestedDiverterBuilder<TTarget>>? nestedBuilderAction = null) where TTarget : class?;

/// <summary>
/// Add a standalone <see cref="IRedirect{TTarget}"/> without registering a service decorator.
/// </summary>
/// <param name="type">The <see cref="IRedirect{TTarget}"/> target type.</param>
/// <param name="name">The <see cref="RedirectId.Name" /> of the added <see cref="IRedirect"/>.</param>
/// <returns>This <see cref="IDiverterBuilder"/> instance.</returns>
IDiverterBuilder AddRedirect(Type type, string? name = null);
IDiverterBuilder WithRedirect(Type type, string? name = null);

/// <summary>
/// Create an <see cref="IDiverter"/> instance from the current registrations.
Expand Down
16 changes: 8 additions & 8 deletions src/DivertR/INestedDiverterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
namespace DivertR
{
/// <summary>
/// A builder interface for nested Diverter builder actions.
/// A nested Diverter builder interface for registering nested actions.
/// </summary>
public interface INestedDiverterBuilder<TTarget> where TTarget : class?
{
/// <summary>
/// Redirect calls with return type <typeparamref name="TReturn"/> via an <see cref="IRedirect{TReturn}"/>.
/// The returned instances are proxied by inserting a persistent <see cref="IVia"/> on the parent <see cref="IRedirect{TTarget}"/>.
/// </summary>
/// <param name="registerAction">Optional nested register action.</param>
/// <param name="nestedBuilderAction">Optional nested builder action.</param>
/// <typeparam name="TReturn">The return type of calls to redirect.</typeparam>
/// <returns>This <see cref="INestedDiverterBuilder{TTarget}"/> instance.</returns>
/// <exception cref="DiverterException">Thrown if a nested <see cref="IRedirect{TReturn}"/> has already been registered on the parent with matching <typeparamref name="TReturn"/> type and default <see cref="RedirectId.Name" />.</exception>
INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(Action<INestedDiverterBuilder<TReturn>>? registerAction = null) where TReturn : class?;
INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(Action<INestedDiverterBuilder<TReturn>>? nestedBuilderAction = null) where TReturn : class?;

/// <summary>
/// Redirect calls with return type <typeparamref name="TReturn"/> via an <see cref="IRedirect{TReturn}"/>.
/// The returned instances are proxied by inserting a persistent <see cref="IVia"/> on the parent <see cref="IRedirect{TTarget}"/>.
/// </summary>
/// <param name="name">Specify the <see cref="DivertR.RedirectId.Name" /> of the returned <see cref="IRedirect{TReturn}"/>.</param>
/// <param name="registerAction">Optional nested register action.</param>
/// <param name="nestedBuilderAction">Optional nested builder action.</param>
/// <typeparam name="TReturn">The return type of calls to redirect.</typeparam>
/// <returns>This <see cref="INestedDiverterBuilder{TTarget}"/> instance.</returns>
/// <exception cref="DiverterException">Thrown if a nested <see cref="IRedirect{TReturn}"/> has already been registered on the parent with matching <typeparamref name="TReturn"/> type and <paramref name="name"/>.</exception>
INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Action<INestedDiverterBuilder<TReturn>>? registerAction = null) where TReturn : class?;
INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Action<INestedDiverterBuilder<TReturn>>? nestedBuilderAction = null) where TReturn : class?;

/// <summary>
/// Redirect calls with return type <typeparamref name="TReturn"/> and matching a call constraint via an <see cref="IRedirect{TReturn}"/>.
/// The returned instances are proxied by inserting a persistent <see cref="IVia"/> on the parent <see cref="IRedirect{TTarget}"/>.
/// </summary>
/// <param name="constraintExpression">The call constraint expression.</param>
/// <param name="registerAction">Optional nested register action.</param>
/// <param name="registerAction">Optional nested builder action.</param>
/// <typeparam name="TReturn">The return type of calls to redirect.</typeparam>
/// <returns>This <see cref="INestedDiverterBuilder{TTarget}"/> instance.</returns>
INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(Expression<Func<TTarget, TReturn>> constraintExpression, Action<INestedDiverterBuilder<TReturn>>? registerAction = null) where TReturn : class?;
Expand All @@ -45,9 +45,9 @@ public interface INestedDiverterBuilder<TTarget> where TTarget : class?
/// </summary>
/// <param name="name">Specify the <see cref="DivertR.RedirectId.Name" /> of the returned <see cref="IRedirect{TReturn}"/>.</param>
/// <param name="constraintExpression">The call constraint expression.</param>
/// <param name="registerAction">Optional nested register action.</param>
/// <param name="nestedBuilderAction">Optional nested builder action.</param>
/// <typeparam name="TReturn">The return type of calls to redirect.</typeparam>
/// <returns>This <see cref="INestedDiverterBuilder{TTarget}"/> instance.</returns>
INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Expression<Func<TTarget, TReturn>> constraintExpression, Action<INestedDiverterBuilder<TReturn>>? registerAction = null) where TReturn : class?;
INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Expression<Func<TTarget, TReturn>> constraintExpression, Action<INestedDiverterBuilder<TReturn>>? nestedBuilderAction = null) where TReturn : class?;
}
}
12 changes: 6 additions & 6 deletions src/DivertR/Internal/NestedDiverterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public NestedDiverterBuilder(IRedirect<TTarget> redirect, DiverterBuilder divert
_diverterBuilder = diverterBuilder;
}

public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(Action<INestedDiverterBuilder<TReturn>>? registerAction = null) where TReturn : class?
public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(Action<INestedDiverterBuilder<TReturn>>? nestedBuilderAction = null) where TReturn : class?
{
return AddRedirect((string?) null, registerAction);
return AddRedirect((string?) null, nestedBuilderAction);
}

public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Action<INestedDiverterBuilder<TReturn>>? registerAction = null) where TReturn : class?
public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Action<INestedDiverterBuilder<TReturn>>? nestedBuilderAction = null) where TReturn : class?
{
var nestedRedirect = _redirect.RedirectSet.GetOrCreate<TReturn>(name);

Expand All @@ -34,7 +34,7 @@ public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Action
opt.Persist();
});

registerAction?.Invoke(new NestedDiverterBuilder<TReturn>(nestedRedirect, _diverterBuilder));
nestedBuilderAction?.Invoke(new NestedDiverterBuilder<TReturn>(nestedRedirect, _diverterBuilder));

return this;
}
Expand All @@ -44,7 +44,7 @@ public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(Expression<Func<TTar
return AddRedirect(null, constraintExpression, registerAction);
}

public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Expression<Func<TTarget, TReturn>> constraintExpression, Action<INestedDiverterBuilder<TReturn>>? registerAction = null) where TReturn : class?
public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Expression<Func<TTarget, TReturn>> constraintExpression, Action<INestedDiverterBuilder<TReturn>>? nestedBuilderAction = null) where TReturn : class?
{
var nestedRedirect = _redirect
.To(constraintExpression)
Expand All @@ -54,7 +54,7 @@ public INestedDiverterBuilder<TTarget> AddRedirect<TReturn>(string? name, Expres
opt.Persist();
});

registerAction?.Invoke(new NestedDiverterBuilder<TReturn>(nestedRedirect, _diverterBuilder));
nestedBuilderAction?.Invoke(new NestedDiverterBuilder<TReturn>(nestedRedirect, _diverterBuilder));

return this;
}
Expand Down
Loading

0 comments on commit 18cf8e6

Please sign in to comment.