Make ServiceCollectionExtensions consistent

This commit is contained in:
jacalvar 2016-02-25 12:32:41 -08:00
parent b7cde3e58f
commit 12f33de92c
6 changed files with 76 additions and 28 deletions

View File

@ -3,8 +3,14 @@
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// An interface for configuring MVC services.
/// </summary>
public interface IMvcBuilder
{
/// <summary>
/// Gets the <see cref="IServiceCollection"/> where MVC services are configured.
/// </summary>
IServiceCollection Services { get; }
}
}

View File

@ -3,8 +3,14 @@
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// An interface for configuring essential MVC services.
/// </summary>
public interface IMvcCoreBuilder
{
/// <summary>
/// Gets the <see cref="IServiceCollection"/> where essential MVC services are configured.
/// </summary>
IServiceCollection Services { get; }
}
}

View File

@ -22,8 +22,16 @@ using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for setting up essential MVC services in an <see cref="IServiceCollection" />.
/// </summary>
public static class MvcCoreServiceCollectionExtensions
{
/// <summary>
/// Adds essential MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>An <see cref="IMvcCoreBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)
{
if (services == null)
@ -31,9 +39,18 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(services));
}
return AddMvcCore(services, setupAction: null);
ConfigureDefaultServices(services);
AddMvcCoreServices(services);
return new MvcCoreBuilder(services);
}
/// <summary>
/// Adds essential MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
/// <returns>An <see cref="IMvcCoreBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcCoreBuilder AddMvcCore(
this IServiceCollection services,
Action<MvcOptions> setupAction)
@ -43,16 +60,15 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(services));
}
ConfigureDefaultServices(services);
AddMvcCoreServices(services);
if (setupAction != null)
if (setupAction == null)
{
services.Configure(setupAction);
throw new ArgumentNullException(nameof(setupAction));
}
return new MvcCoreBuilder(services);
var builder = services.AddMvcCore();
services.Configure(setupAction);
return builder;
}
// To enable unit testing

View File

@ -6,8 +6,15 @@ using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc.Internal
{
/// <summary>
/// Allows fine grained configuration of MVC services.
/// </summary>
public class MvcBuilder : IMvcBuilder
{
/// <summary>
/// Initializes a new <see cref="MvcBuilder"/> instance.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
public MvcBuilder(IServiceCollection services)
{
if (services == null)
@ -18,6 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
Services = services;
}
/// <inheritdoc />
public IServiceCollection Services { get; }
}
}

View File

@ -6,8 +6,15 @@ using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc.Internal
{
/// <summary>
/// Allows fine grained configuration of essential MVC services.
/// </summary>
public class MvcCoreBuilder : IMvcCoreBuilder
{
/// <summary>
/// Initializes a new instance of <see cref="MvcCoreBuilder"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
public MvcCoreBuilder(IServiceCollection services)
{
if (services == null)
@ -18,6 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal
Services = services;
}
/// <inheritdoc />
public IServiceCollection Services { get; }
}
}

View File

@ -16,7 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcBuilder AddMvc(this IServiceCollection services)
{
if (services == null)
@ -24,22 +24,6 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(services));
}
return AddMvc(services, setupAction: null);
}
/// <summary>
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">An action delegate to configure the provided <see cref="MvcOptions"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
var builder = services.AddMvcCore();
builder.AddApiExplorer();
@ -60,11 +44,31 @@ namespace Microsoft.Extensions.DependencyInjection
builder.AddCors();
if (setupAction != null)
return new MvcBuilder(builder.Services);
}
/// <summary>
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
{
if (services == null)
{
builder.Services.Configure(setupAction);
throw new ArgumentNullException(nameof(services));
}
return new MvcBuilder(services);
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
var builder = services.AddMvc();
builder.Services.Configure(setupAction);
return builder;
}
}
}