Skip to content

Commit

Permalink
Expose ViaSet from IDiverter instance
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed Jan 30, 2022
1 parent 8c4fe0b commit b58b5c9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ which also lets you customise the DI configuration, e.g. to substitute test doub
DivertR was born out of the need to efficiently modify DI configurations between tests running against the same TestServer instance.
It has grown into a framework that facilitates testing of wired up systems, bringing a familiar unit/mocking testing style into the realm of component and integration testing,
by providing features to conveniently substitute dependency behaviour (including error conditions) and verify inputs and outputs from recorded call information.
For a demonstration of usage view this [WebApp Testing Sample](https://github.com/devodo/DivertR/tree/main/test/DivertR.WebAppTests) or continue below

For a demonstration of usage view this [WebApp Testing Sample](https://github.com/devodo/DivertR/blob/main/test/DivertR.WebAppTests/WebAppTests.cs) or continue below
for a quickstart and code overview.

# Quickstart
Expand Down
10 changes: 4 additions & 6 deletions src/DivertR/DependencyInjection/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public static IServiceCollection Divert(this IServiceCollection services, IDiver

private static Dictionary<Type, IEnumerable<Action>> CreateDecorateActions(IServiceCollection services, IDiverter diverter, string? name)
{
var diverterTypes = new HashSet<Type>(diverter.RegisteredVias(name).Select(x => x.ViaId.Type));
var registeredVias = diverter.RegisteredVias(name).ToDictionary(x => x.ViaId.Type);

return services
.Select((descriptor, index) =>
{
if (!diverterTypes.Contains(descriptor.ServiceType))
if (!registeredVias.TryGetValue(descriptor.ServiceType, out var via))
{
return null;
}

var proxyFactory = CreateViaProxyFactory(descriptor, diverter, name);
var proxyFactory = CreateViaProxyFactory(descriptor, via);
void DecorateAction() => services[index] = ServiceDescriptor.Describe(descriptor.ServiceType, proxyFactory, descriptor.Lifetime);

return new
Expand All @@ -58,10 +58,8 @@ private static Dictionary<Type, IEnumerable<Action>> CreateDecorateActions(IServ
grp => grp.Select(x => x!.Action));
}

private static Func<IServiceProvider, object> CreateViaProxyFactory(ServiceDescriptor descriptor, IDiverter diverter, string? name)
private static Func<IServiceProvider, object> CreateViaProxyFactory(ServiceDescriptor descriptor, IVia via)
{
var via = diverter.Via(descriptor.ServiceType, name);

object ProxyFactory(IServiceProvider provider)
{
var instance = GetServiceInstance(provider, descriptor);
Expand Down
25 changes: 17 additions & 8 deletions src/DivertR/Diverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@ namespace DivertR
public class Diverter : IDiverter
{
private readonly ConcurrentDictionary<ViaId, IVia> _registeredVias = new ConcurrentDictionary<ViaId, IVia>();
private readonly IViaSet _viaSet;
public IViaSet ViaSet { get; }

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

/// <summary>
/// Create a <see cref="Diverter"/> instance using an external <see cref="IViaSet"/>.
/// </summary>
/// <param name="viaSet">The <see cref="IViaSet"/> instance.</param>
public Diverter(IViaSet viaSet)
{
ViaSet = viaSet;
}

public IDiverter Register<TTarget>(string? name = null) where TTarget : class
{
var via = _viaSet.Via<TTarget>(name);
var via = ViaSet.Via<TTarget>(name);

if (!_registeredVias.TryAdd(via.ViaId, via))
{
Expand All @@ -34,7 +43,7 @@ public IDiverter Register<TTarget>(string? name = null) where TTarget : class

public IDiverter Register(Type targetType, string? name = null)
{
var via = _viaSet.Via(targetType, name);
var via = ViaSet.Via(targetType, name);

if (!_registeredVias.TryAdd(via.ViaId, via))
{
Expand Down Expand Up @@ -85,29 +94,29 @@ public IVia Via(ViaId id)

public IDiverter StrictAll()
{
_viaSet.StrictAll();
ViaSet.StrictAll();

return this;
}


public IDiverter Strict(string? name = null)
{
_viaSet.Strict(name);
ViaSet.Strict(name);

return this;
}

public IDiverter ResetAll()
{
_viaSet.ResetAll();
ViaSet.ResetAll();

return this;
}

public IDiverter Reset(string? name = null)
{
_viaSet.Reset(name);
ViaSet.Reset(name);

return this;
}
Expand Down
5 changes: 5 additions & 0 deletions src/DivertR/IDiverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace DivertR
/// </summary>
public interface IDiverter
{
/// <summary>
/// The <see cref="IViaSet"/> instance containing all <see cref="IVia"/> instances used by this <see cref="IDiverter"/>.
/// </summary>
IViaSet ViaSet { get; }

/// <summary>
/// Register an <see cref="IVia{TTarget}"/> for a given type.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/DivertR/ViaId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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}");
}

public static ViaId From(Type type, string? name = null)
Expand Down
7 changes: 6 additions & 1 deletion src/DivertR/ViaSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public IViaSet StrictAll()

return this;
}


/// <summary>
/// Add a <see cref="IVia{TTarget}"/> to this <see cref="IViaSet"/>. For internal use only to allow empty <see cref="IVia{TTarget}"/> constructor.
/// </summary>
/// <param name="via">The <see cref="IVia{TTarget}"/> instance to add.</param>
/// <exception cref="DiverterException">Thrown if <see cref="IVia{TTarget}"/> already exists in this <see cref="IViaSet"/></exception>
internal void AddVia(IVia via)
{
var viaGroup = GetViaGroup(via.ViaId.Name);
Expand Down
4 changes: 2 additions & 2 deletions test/DivertR.WebAppTests/WebAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public WebAppTests(WebAppFixture webAppFixture, ITestOutputHelper output)
}

[Fact]
public async Task GivenFooExists_WhenGetFoo_ThenReturnFooContent_WithOk200()
public async Task GivenFooExistsInRepo_WhenGetFoo_ThenReturnFooContent_WithOk200()
{
// ARRANGE
var foo = new Foo
Expand Down Expand Up @@ -196,7 +196,7 @@ public async Task GiveFooNotExists_WhenCreateFooRequest_ThenInsertsFoo_RecordMap
}

[Fact]
public async Task GivenFooRepositoryThrowsException_WhenCreateFooRequest_ThenReturns500InternalServerError()
public async Task GivenFooRepositoryInsertFails_WhenCreateFooRequest_ThenReturns500InternalServerError()
{
// ARRANGE
var createFooRequest = new CreateFooRequest
Expand Down

0 comments on commit b58b5c9

Please sign in to comment.