Skip to content

Commit

Permalink
minor fix to defaults in the properties container, Added test demonst…
Browse files Browse the repository at this point in the history
…rating IgnoreDefaultValues
  • Loading branch information
david-driscoll committed Jul 1, 2019
1 parent ee5550b commit 88a1cde
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 13 deletions.
100 changes: 100 additions & 0 deletions src/Conventions.Abstractions/ConventionHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
namespace Rocket.Surgery.Conventions
{
/// <summary>
/// Base convention extensions
/// </summary>
public static class ConventionHostBuilderExtensions
{
/// <summary>
/// Get a value by type from the context
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context">The context</param>
/// <returns>T.</returns>
public static T Get<T>(this IConventionHostBuilder context)
{
return (T)context.ServiceProperties[typeof(T)];
}

/// <summary>
/// Get a value by key from the context
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context">The context</param>
/// <param name="key">The key where the value is saved</param>
/// <returns>T.</returns>
public static T Get<T>(this IConventionHostBuilder context, string key)
{
return (T)context.ServiceProperties[key];
}

/// <summary>
/// Get a value by type from the context
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="context">The context</param>
/// <param name="value">The value to save</param>
public static void Set<T>(this IConventionHostBuilder context, T value)
{
context.ServiceProperties[typeof(T)] = value;
}

/// <summary>
/// Get a value by type from the context
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="context">The context</param>
/// <param name="key">The key where the value is saved</param>
/// <param name="value">The value to save</param>
public static void Set<T>(this IConventionHostBuilder context, string key, T value)
{
context.ServiceProperties[key] = value;
}

/// <summary>
/// Get a value by type from the context
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serviceProviderDictionary">The context</param>
/// <returns>T.</returns>
public static T Get<T>(this IServiceProviderDictionary serviceProviderDictionary)
{
return (T)serviceProviderDictionary[typeof(T)];
}

/// <summary>
/// Get a value by key from the context
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serviceProviderDictionary">The context</param>
/// <param name="key">The key where the value is saved</param>
/// <returns>T.</returns>
public static T Get<T>(this IServiceProviderDictionary serviceProviderDictionary, string key)
{
return (T)serviceProviderDictionary[key];
}

/// <summary>
/// Get a value by type from the context
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="serviceProviderDictionary">The context</param>
/// <param name="value">The value to save</param>
public static void Set<T>(this IServiceProviderDictionary serviceProviderDictionary, T value)
{
serviceProviderDictionary[typeof(T)] = value;
}

/// <summary>
/// Get a value by type from the context
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="serviceProviderDictionary">The context</param>
/// <param name="key">The key where the value is saved</param>
/// <param name="value">The value to save</param>
public static void Set<T>(this IServiceProviderDictionary serviceProviderDictionary, string key, T value)
{
serviceProviderDictionary[key] = value;
}
}
}
20 changes: 12 additions & 8 deletions src/Conventions/ConventionHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Rocket.Surgery.Conventions
{
Expand Down Expand Up @@ -38,16 +39,19 @@ IServiceProviderDictionary serviceProperties
ServiceProperties = serviceProperties ?? new ServiceProviderDictionary();

if (!Properties.TryGetValue(typeof(IConventionScanner), out var _))
Properties[typeof(IConventionScanner)] = Scanner;
Properties[typeof(IConventionScanner)] = scanner;

if (!Properties.TryGetValue(typeof(IAssemblyProvider), out var _))
Properties[typeof(IAssemblyProvider)] = AssemblyProvider;
Properties[typeof(IAssemblyProvider)] = assemblyProvider;

if (!Properties.TryGetValue(typeof(IAssemblyCandidateFinder), out var _))
Properties[typeof(IAssemblyCandidateFinder)] = AssemblyCandidateFinder;
Properties[typeof(IAssemblyCandidateFinder)] = assemblyCandidateFinder;

if (!Properties.TryGetValue(typeof(DiagnosticSource), out var _))
Properties[typeof(DiagnosticSource)] = DiagnosticSource;
Properties[typeof(DiagnosticSource)] = diagnosticSource;

if (!Properties.TryGetValue(typeof(ILogger), out var _))
Properties[typeof(ILogger)] = new DiagnosticLogger(diagnosticSource);
}

/// <summary>
Expand Down Expand Up @@ -76,22 +80,22 @@ public virtual object this[object item]
/// Gets the scanner.
/// </summary>
/// <value>The scanner.</value>
public virtual IConventionScanner Scanner { get; }
public IConventionScanner Scanner { get; }
/// <summary>
/// Gets the assembly candidate finder.
/// </summary>
/// <value>The assembly candidate finder.</value>
public virtual IAssemblyCandidateFinder AssemblyCandidateFinder { get; }
public IAssemblyCandidateFinder AssemblyCandidateFinder { get; }
/// <summary>
/// Gets the assembly provider.
/// </summary>
/// <value>The assembly provider.</value>
public virtual IAssemblyProvider AssemblyProvider { get; }
public IAssemblyProvider AssemblyProvider { get; }
/// <summary>
/// Gets the diagnostic source.
/// </summary>
/// <value>The diagnostic source.</value>
public virtual DiagnosticSource DiagnosticSource { get; }
public DiagnosticSource DiagnosticSource { get; }

/// <summary>
/// Adds a set of conventions to the scanner
Expand Down
6 changes: 1 addition & 5 deletions src/Conventions/ServiceProviderDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ public class ServiceProviderDictionary : IServiceProviderDictionary
/// <returns>System.Object.</returns>
public object this[object key]
{
get
{
return key is Type t && _services.TryGetValue(t, out var v) ? v : _values[key];

}
get => key is Type t && _services.TryGetValue(t, out var v) ? v : _values.TryGetValue(key, out var b) ? b : null;
set
{
if (key is Type t)
Expand Down
36 changes: 36 additions & 0 deletions test/Conventions.Tests/ConventionScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ public void Register(IServiceConventionContext context)
}
}

class F_WithDefault : IServiceConvention
{
public IConventionScanner Scanner { get; }
public IService Service { get; }

public F_WithDefault(IConventionScanner scanner, IService service = null)
{
this.Scanner = scanner;
Service = service;
}

public void Register(IServiceConventionContext context)
{
}
}

[Fact]
public void ShouldConstruct()
{
Expand Down Expand Up @@ -512,6 +528,26 @@ public void ShouldResolveConventionsUsingTheServiceProvider()
(item as F).Service.Should().BeSameAs(fakeService);
}

[Fact]
public void ShouldResolveConventionsUsingTheServiceProvider_IgnoringDefaultValues()
{
var properties = new ServiceProviderDictionary();
AutoFake.Provide<IServiceProvider>(properties);
var scanner = AutoFake.Resolve<Scanner>();
properties.Add(typeof(IConventionScanner), scanner);

scanner.AppendConvention<F_WithDefault>();

var result = scanner.BuildProvider().Get<IServiceConvention, ServiceConventionDelegate>();

var item = result.First().Convention;

item.Should().BeOfType<F_WithDefault>();
(item as F_WithDefault).Scanner.Should().NotBeNull();
(item as F_WithDefault).Scanner.Should().BeSameAs(scanner);
(item as F_WithDefault).Service.Should().BeNull();
}

[Fact]
public void ShouldExcludeScannedItemsIfAddedManually()
{
Expand Down

0 comments on commit 88a1cde

Please sign in to comment.