From 6737a1f243a35e7daed119c40ed6b026382d8fae Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Mon, 4 Jan 2016 16:56:38 +0100 Subject: [PATCH] Adding params overloads for AddControllersAsServices - Moved null checks - Tweaked XML docs --- .../MvcCoreMvcBuilderExtensions.cs | 44 +++++++++++++------ .../MvcCoreMvcCoreBuilderExtensions.cs | 44 +++++++++++++------ .../Internal/ControllersAsServices.cs | 22 ++++++++++ 3 files changed, 84 insertions(+), 26 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs index d95d90b385..a0279e8ca5 100644 --- a/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcBuilderExtensions.cs @@ -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 { @@ -62,8 +63,21 @@ public static IMvcBuilder AddFormatterMappings( /// discovery. /// /// The . - /// A sequence of controller s to register in the - /// and used for controller discovery. + /// A sequence of controller s to register. + /// The . + public static IMvcBuilder AddControllersAsServices( + this IMvcBuilder builder, + params Type[] controllerTypes) + { + return builder.AddControllersAsServices(controllerTypes.AsEnumerable()); + } + + /// + /// Register the specified as services and as a source for controller + /// discovery. + /// + /// The . + /// A sequence of controller s to register. /// The . public static IMvcBuilder AddControllersAsServices( this IMvcBuilder builder, @@ -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; } /// - /// Registers controller types from the specified as services and as a source + /// Registers controller types from the specified as services and as a source + /// for controller discovery. + /// + /// The . + /// Assemblies to scan. + /// The . + public static IMvcBuilder AddControllersAsServices( + this IMvcBuilder builder, + params Assembly[] controllerAssemblies) + { + return builder.AddControllersAsServices(controllerAssemblies.AsEnumerable()); + } + + /// + /// Registers controller types from the specified as services and as a source /// for controller discovery. /// /// The . @@ -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; } diff --git a/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs index b8359abbb5..7ee3f5b8bc 100644 --- a/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreMvcCoreBuilderExtensions.cs @@ -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 { @@ -99,8 +100,21 @@ internal static void AddAuthorizationServices(IServiceCollection services) /// discovery. /// /// The . - /// A sequence of controller s to register in the - /// and used for controller discovery. + /// A sequence of controller s to register. + /// The . + public static IMvcCoreBuilder AddControllersAsServices( + this IMvcCoreBuilder builder, + params Type[] controllerTypes) + { + return builder.AddControllersAsServices(controllerTypes.AsEnumerable()); + } + + /// + /// Register the specified as services and as a source for controller + /// discovery. + /// + /// The . + /// A sequence of controller s to register. /// The . public static IMvcCoreBuilder AddControllersAsServices( this IMvcCoreBuilder builder, @@ -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; } /// - /// Registers controller types from the specified as services and as a source + /// Registers controller types from the specified as services and as a source + /// for controller discovery. + /// + /// The . + /// Assemblies to scan. + /// The . + public static IMvcCoreBuilder AddControllersAsServices( + this IMvcCoreBuilder builder, + params Assembly[] controllerAssemblies) + { + return builder.AddControllersAsServices(controllerAssemblies.AsEnumerable()); + } + + /// + /// Registers controller types from the specified as services and as a source /// for controller discovery. /// /// The . @@ -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; } diff --git a/src/Microsoft.AspNet.Mvc.Core/Internal/ControllersAsServices.cs b/src/Microsoft.AspNet.Mvc.Core/Internal/ControllersAsServices.cs index 873b075991..a85de4d081 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Internal/ControllersAsServices.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Internal/ControllersAsServices.cs @@ -16,7 +16,18 @@ public static class ControllersAsServices { public static void AddControllersAsServices(IServiceCollection services, IEnumerable 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); @@ -29,7 +40,18 @@ public static void AddControllersAsServices(IServiceCollection services, IEnumer public static void AddControllersAsServices(IServiceCollection services, IEnumerable 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);