Skip to content

Commit 12f33de

Browse files
committed
Make ServiceCollectionExtensions consistent
1 parent b7cde3e commit 12f33de

File tree

6 files changed

+76
-28
lines changed

6 files changed

+76
-28
lines changed

src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/IMvcBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
namespace Microsoft.Extensions.DependencyInjection
55
{
6+
/// <summary>
7+
/// An interface for configuring MVC services.
8+
/// </summary>
69
public interface IMvcBuilder
710
{
11+
/// <summary>
12+
/// Gets the <see cref="IServiceCollection"/> where MVC services are configured.
13+
/// </summary>
814
IServiceCollection Services { get; }
915
}
1016
}

src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/IMvcCoreBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
namespace Microsoft.Extensions.DependencyInjection
55
{
6+
/// <summary>
7+
/// An interface for configuring essential MVC services.
8+
/// </summary>
69
public interface IMvcCoreBuilder
710
{
11+
/// <summary>
12+
/// Gets the <see cref="IServiceCollection"/> where essential MVC services are configured.
13+
/// </summary>
814
IServiceCollection Services { get; }
915
}
1016
}

src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,35 @@
2222

2323
namespace Microsoft.Extensions.DependencyInjection
2424
{
25+
/// <summary>
26+
/// Extension methods for setting up essential MVC services in an <see cref="IServiceCollection" />.
27+
/// </summary>
2528
public static class MvcCoreServiceCollectionExtensions
2629
{
30+
/// <summary>
31+
/// Adds essential MVC services to the specified <see cref="IServiceCollection" />.
32+
/// </summary>
33+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
34+
/// <returns>An <see cref="IMvcCoreBuilder"/> that can be used to further configure the MVC services.</returns>
2735
public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)
2836
{
2937
if (services == null)
3038
{
3139
throw new ArgumentNullException(nameof(services));
3240
}
3341

34-
return AddMvcCore(services, setupAction: null);
42+
ConfigureDefaultServices(services);
43+
AddMvcCoreServices(services);
44+
45+
return new MvcCoreBuilder(services);
3546
}
3647

48+
/// <summary>
49+
/// Adds essential MVC services to the specified <see cref="IServiceCollection" />.
50+
/// </summary>
51+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
52+
/// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
53+
/// <returns>An <see cref="IMvcCoreBuilder"/> that can be used to further configure the MVC services.</returns>
3754
public static IMvcCoreBuilder AddMvcCore(
3855
this IServiceCollection services,
3956
Action<MvcOptions> setupAction)
@@ -43,16 +60,15 @@ public static IMvcCoreBuilder AddMvcCore(
4360
throw new ArgumentNullException(nameof(services));
4461
}
4562

46-
ConfigureDefaultServices(services);
47-
48-
AddMvcCoreServices(services);
49-
50-
if (setupAction != null)
63+
if (setupAction == null)
5164
{
52-
services.Configure(setupAction);
65+
throw new ArgumentNullException(nameof(setupAction));
5366
}
5467

55-
return new MvcCoreBuilder(services);
68+
var builder = services.AddMvcCore();
69+
services.Configure(setupAction);
70+
71+
return builder;
5672
}
5773

5874
// To enable unit testing

src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66

77
namespace Microsoft.AspNetCore.Mvc.Internal
88
{
9+
/// <summary>
10+
/// Allows fine grained configuration of MVC services.
11+
/// </summary>
912
public class MvcBuilder : IMvcBuilder
1013
{
14+
/// <summary>
15+
/// Initializes a new <see cref="MvcBuilder"/> instance.
16+
/// </summary>
17+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
1118
public MvcBuilder(IServiceCollection services)
1219
{
1320
if (services == null)
@@ -18,6 +25,7 @@ public MvcBuilder(IServiceCollection services)
1825
Services = services;
1926
}
2027

28+
/// <inheritdoc />
2129
public IServiceCollection Services { get; }
2230
}
2331
}

src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcCoreBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66

77
namespace Microsoft.AspNetCore.Mvc.Internal
88
{
9+
/// <summary>
10+
/// Allows fine grained configuration of essential MVC services.
11+
/// </summary>
912
public class MvcCoreBuilder : IMvcCoreBuilder
1013
{
14+
/// <summary>
15+
/// Initializes a new instance of <see cref="MvcCoreBuilder"/>.
16+
/// </summary>
17+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
1118
public MvcCoreBuilder(IServiceCollection services)
1219
{
1320
if (services == null)
@@ -18,6 +25,7 @@ public MvcCoreBuilder(IServiceCollection services)
1825
Services = services;
1926
}
2027

28+
/// <inheritdoc />
2129
public IServiceCollection Services { get; }
2230
}
2331
}

src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,14 @@ public static class MvcServiceCollectionExtensions
1616
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
1717
/// </summary>
1818
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
19-
/// <returns>A reference to this instance after the operation has completed.</returns>
19+
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
2020
public static IMvcBuilder AddMvc(this IServiceCollection services)
2121
{
2222
if (services == null)
2323
{
2424
throw new ArgumentNullException(nameof(services));
2525
}
2626

27-
return AddMvc(services, setupAction: null);
28-
}
29-
30-
/// <summary>
31-
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
32-
/// </summary>
33-
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
34-
/// <param name="setupAction">An action delegate to configure the provided <see cref="MvcOptions"/>.</param>
35-
/// <returns>A reference to this instance after the operation has completed.</returns>
36-
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
37-
{
38-
if (services == null)
39-
{
40-
throw new ArgumentNullException(nameof(services));
41-
}
42-
4327
var builder = services.AddMvcCore();
4428

4529
builder.AddApiExplorer();
@@ -60,11 +44,31 @@ public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOpt
6044

6145
builder.AddCors();
6246

63-
if (setupAction != null)
47+
return new MvcBuilder(builder.Services);
48+
}
49+
50+
/// <summary>
51+
/// Adds MVC services to the specified <see cref="IServiceCollection" />.
52+
/// </summary>
53+
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
54+
/// <param name="setupAction">An <see cref="Action{MvcOptions}"/> to configure the provided <see cref="MvcOptions"/>.</param>
55+
/// <returns>An <see cref="IMvcBuilder"/> that can be used to further configure the MVC services.</returns>
56+
public static IMvcBuilder AddMvc(this IServiceCollection services, Action<MvcOptions> setupAction)
57+
{
58+
if (services == null)
59+
{
60+
throw new ArgumentNullException(nameof(services));
61+
}
62+
63+
if (setupAction == null)
6464
{
65-
builder.Services.Configure(setupAction);
65+
throw new ArgumentNullException(nameof(setupAction));
6666
}
67-
return new MvcBuilder(services);
67+
68+
var builder = services.AddMvc();
69+
builder.Services.Configure(setupAction);
70+
71+
return builder;
6872
}
6973
}
7074
}

0 commit comments

Comments
 (0)