Skip to content

Commit

Permalink
Let CrossWire extension methods return input object to allow a fluent…
Browse files Browse the repository at this point in the history
… coding style. #639
  • Loading branch information
dotnetjunkie committed May 8, 2019
1 parent 9d464cb commit 124c0b2
Showing 1 changed file with 11 additions and 5 deletions.
Expand Up @@ -26,6 +26,7 @@ namespace SimpleInjector
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SimpleInjector.Diagnostics;
using SimpleInjector.Integration.ServiceCollection;
using SimpleInjector.Lifestyles;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static class SimpleInjectorServiceCollectionExtensions

// This stores the options, which includes the IServiceCollection. IServiceCollection is required
// when calling UseSimpleInjector to enable auto cross wiring.
AddOptions(container, options);
AddSimpleInjectorOptions(container, options);

// Set lifestyle before calling setupAction. Code in the delegate might depend on that.
TrySetDefaultScopedLifestyle(container);
Expand Down Expand Up @@ -133,12 +134,13 @@ public static class SimpleInjectorServiceCollectionExtensions
/// </summary>
/// <typeparam name="TService">The type of service object to cross-wire.</typeparam>
/// <param name="options">The options.</param>
/// <returns>The supplied <paramref name="options"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when the parameter is a null reference.
/// </exception>
public static void CrossWire<TService>(this SimpleInjectorUseOptions options)
public static SimpleInjectorUseOptions CrossWire<TService>(this SimpleInjectorUseOptions options)
where TService : class
{
CrossWire(options, typeof(TService));
return CrossWire(options, typeof(TService));
}

/// <summary>
Expand All @@ -147,9 +149,11 @@ public static void CrossWire<TService>(this SimpleInjectorUseOptions options)
/// </summary>
/// <param name="options">The options.</param>
/// <param name="serviceType">The type of service object to ross-wire.</param>
/// <returns>The supplied <paramref name="options"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when one of the parameters is a null reference.
/// </exception>
public static void CrossWire(this SimpleInjectorUseOptions options, Type serviceType)
public static SimpleInjectorUseOptions CrossWire(
this SimpleInjectorUseOptions options, Type serviceType)
{
if (options is null)
{
Expand All @@ -168,6 +172,8 @@ public static void CrossWire(this SimpleInjectorUseOptions options, Type service
DetermineLifestyle(serviceType, options.Services));

options.Container.AddRegistration(serviceType, registration);

return options;
}

private static void RegisterServiceScope(IServiceProvider provider, Container container)
Expand Down Expand Up @@ -314,7 +320,7 @@ private static Lifestyle ToLifestyle(ServiceLifetime lifetime)
}
}

private static void AddOptions(Container container, SimpleInjectorAddOptions builder)
private static void AddSimpleInjectorOptions(Container container, SimpleInjectorAddOptions builder)
{
var current = container.ContainerScope.GetItem(SimpleInjectorAddOptionsKey);

Expand Down

0 comments on commit 124c0b2

Please sign in to comment.