Skip to content

Commit

Permalink
Adding params overloads for AddControllersAsServices
Browse files Browse the repository at this point in the history
- Moved null checks
- Tweaked XML docs
  • Loading branch information
khellang authored and rynowak committed Jan 21, 2016
1 parent 04453a2 commit 6737a1f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Formatters;
using Microsoft.AspNet.Mvc.Internal;
using System.Linq;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -62,8 +63,21 @@ public static IMvcBuilder AddFormatterMappings(
/// discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register in the
/// <paramref name="services"/> and used for controller discovery.</param>
/// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddControllersAsServices(
this IMvcBuilder builder,
params Type[] controllerTypes)
{
return builder.AddControllersAsServices(controllerTypes.AsEnumerable());
}

/// <summary>
/// Register the specified <paramref name="controllerTypes"/> as services and as a source for controller
/// discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddControllersAsServices(
this IMvcBuilder builder,
Expand All @@ -74,17 +88,26 @@ public static IMvcBuilder AddControllersAsServices(
throw new ArgumentNullException(nameof(builder));
}

if (controllerTypes == null)
{
throw new ArgumentNullException(nameof(controllerTypes));
}

ControllersAsServices.AddControllersAsServices(builder.Services, controllerTypes);
return builder;
}

/// <summary>
/// Registers controller types from the specified <paramref name="assemblies"/> as services and as a source
/// Registers controller types from the specified <paramref name="controllerAssemblies"/> as services and as a source
/// for controller discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <param name="controllerAssemblies">Assemblies to scan.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
public static IMvcBuilder AddControllersAsServices(
this IMvcBuilder builder,
params Assembly[] controllerAssemblies)
{
return builder.AddControllersAsServices(controllerAssemblies.AsEnumerable());
}

/// <summary>
/// Registers controller types from the specified <paramref name="controllerAssemblies"/> as services and as a source
/// for controller discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
Expand All @@ -99,11 +122,6 @@ public static IMvcBuilder AddControllersAsServices(
throw new ArgumentNullException(nameof(builder));
}

if (controllerAssemblies == null)
{
throw new ArgumentNullException(nameof(controllerAssemblies));
}

ControllersAsServices.AddControllersAsServices(builder.Services, controllerAssemblies);
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNet.Mvc.Formatters;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Linq;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -99,8 +100,21 @@ internal static void AddAuthorizationServices(IServiceCollection services)
/// discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register in the
/// <paramref name="services"/> and used for controller discovery.</param>
/// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddControllersAsServices(
this IMvcCoreBuilder builder,
params Type[] controllerTypes)
{
return builder.AddControllersAsServices(controllerTypes.AsEnumerable());
}

/// <summary>
/// Register the specified <paramref name="controllerTypes"/> as services and as a source for controller
/// discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddControllersAsServices(
this IMvcCoreBuilder builder,
Expand All @@ -111,17 +125,26 @@ public static IMvcCoreBuilder AddControllersAsServices(
throw new ArgumentNullException(nameof(builder));
}

if (controllerTypes == null)
{
throw new ArgumentNullException(nameof(controllerTypes));
}

ControllersAsServices.AddControllersAsServices(builder.Services, controllerTypes);
return builder;
}

/// <summary>
/// Registers controller types from the specified <paramref name="assemblies"/> as services and as a source
/// Registers controller types from the specified <paramref name="controllerAssemblies"/> as services and as a source
/// for controller discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <param name="controllerAssemblies">Assemblies to scan.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddControllersAsServices(
this IMvcCoreBuilder builder,
params Assembly[] controllerAssemblies)
{
return builder.AddControllersAsServices(controllerAssemblies.AsEnumerable());
}

/// <summary>
/// Registers controller types from the specified <paramref name="controllerAssemblies"/> as services and as a source
/// for controller discovery.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
Expand All @@ -136,11 +159,6 @@ public static IMvcCoreBuilder AddControllersAsServices(
throw new ArgumentNullException(nameof(builder));
}

if (controllerAssemblies == null)
{
throw new ArgumentNullException(nameof(controllerAssemblies));
}

ControllersAsServices.AddControllersAsServices(builder.Services, controllerAssemblies);
return builder;
}
Expand Down
22 changes: 22 additions & 0 deletions src/Microsoft.AspNet.Mvc.Core/Internal/ControllersAsServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ public static class ControllersAsServices
{
public static void AddControllersAsServices(IServiceCollection services, IEnumerable<Type> types)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (types == null)
{
throw new ArgumentNullException(nameof(types));
}

var controllerTypeProvider = new StaticControllerTypeProvider();

foreach (var type in types)
{
services.TryAddTransient(type, type);
Expand All @@ -29,7 +40,18 @@ public static void AddControllersAsServices(IServiceCollection services, IEnumer

public static void AddControllersAsServices(IServiceCollection services, IEnumerable<Assembly> assemblies)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (assemblies == null)
{
throw new ArgumentNullException(nameof(assemblies));
}

var assemblyProvider = new StaticAssemblyProvider();

foreach (var assembly in assemblies)
{
assemblyProvider.CandidateAssemblies.Add(assembly);
Expand Down

0 comments on commit 6737a1f

Please sign in to comment.