From ac410b76d9e1a3a454bf21b79491ee6d9d0e15b6 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 3 Aug 2018 16:30:57 +1200 Subject: [PATCH] Change MvcEndpointInfo to internal (#8210) --- .../MvcApplicationBuilderExtensions.cs | 32 -- .../Builder/MvcEndpointInfo.cs | 2 +- .../Builder/MvcEndpointInfoBuilder.cs | 19 -- .../MvcEndpointInfoBuilderExtensions.cs | 229 -------------- .../Internal/MvcEndpointDataSource.cs | 1 - .../MvcApplicationBuilderExtensionsTest.cs | 20 -- .../MvcEndpointInfoBuilderExtensionsTest.cs | 282 ------------------ 7 files changed, 1 insertion(+), 584 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilder.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilderExtensions.cs delete mode 100644 test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcEndpointInfoBuilderExtensionsTest.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs index ab4b465492..08baf62c2e 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs @@ -171,38 +171,6 @@ namespace Microsoft.AspNetCore.Builder } } - public static IApplicationBuilder UseMvcWithEndpoint( - this IApplicationBuilder app, - Action configureRoutes) - { - if (app == null) - { - throw new ArgumentNullException(nameof(app)); - } - - if (configureRoutes == null) - { - throw new ArgumentNullException(nameof(configureRoutes)); - } - - VerifyMvcIsRegistered(app); - - var mvcEndpointDataSource = app.ApplicationServices - .GetRequiredService>() - .OfType() - .First(); - - var constraintResolver = app.ApplicationServices.GetRequiredService(); - - MvcEndpointInfoBuilder routeBuilder = new MvcEndpointInfoBuilder(constraintResolver); - - configureRoutes(routeBuilder); - - mvcEndpointDataSource.ConventionalEndpointInfos.AddRange(routeBuilder.EndpointInfos); - - return app.UseEndpoint(); - } - private static void VerifyMvcIsRegistered(IApplicationBuilder app) { // Verify if AddMvc was done before calling UseMvc diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfo.cs b/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfo.cs index ba59c34b44..e0943b82c0 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfo.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfo.cs @@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Routing.Template; namespace Microsoft.AspNetCore.Builder { - public class MvcEndpointInfo + internal class MvcEndpointInfo { public MvcEndpointInfo( string name, diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilder.cs b/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilder.cs deleted file mode 100644 index 99000fdd08..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilder.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using Microsoft.AspNetCore.Routing; - -namespace Microsoft.AspNetCore.Builder -{ - public class MvcEndpointInfoBuilder - { - public MvcEndpointInfoBuilder(IInlineConstraintResolver constraintResolver) - { - ConstraintResolver = constraintResolver; - } - - public List EndpointInfos { get; } = new List(); - public IInlineConstraintResolver ConstraintResolver { get; } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilderExtensions.cs deleted file mode 100644 index b52ef3692b..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Core/Builder/MvcEndpointInfoBuilderExtensions.cs +++ /dev/null @@ -1,229 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc.Core; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Routing.Constraints; - -namespace Microsoft.AspNetCore.Builder -{ - /// - /// Provides extension methods for to add endpoints. - /// - public static class MvcEndpointInfoBuilderExtensions - { - #region MapEndpoint - /// - /// Adds a endpoint to the with the specified name and template. - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The URL pattern of the endpoint. - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template) - { - endpointBuilder.MapEndpoint(name, template, null); - return endpointBuilder; - } - - /// - /// Adds a endpoint to the with the specified name, template, and default values. - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The URL pattern of the endpoint. - /// - /// An object that contains default values for endpoint parameters. The object's properties represent the names - /// and values of the default values. - /// - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template, object defaults) - { - return endpointBuilder.MapEndpoint(name, template, defaults, null); - } - - /// - /// Adds a endpoint to the with the specified name, template, default values, and - /// constraints. - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The URL pattern of the endpoint. - /// - /// An object that contains default values for endpoint parameters. The object's properties represent the names - /// and values of the default values. - /// - /// - /// An object that contains constraints for the endpoint. The object's properties represent the names and values - /// of the constraints. - /// - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template, object defaults, object constraints) - { - return endpointBuilder.MapEndpoint(name, template, defaults, constraints, null); - } - - /// - /// Adds a endpoint to the with the specified name, template, default values, and - /// data tokens. - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The URL pattern of the endpoint. - /// - /// An object that contains default values for endpoint parameters. The object's properties represent the names - /// and values of the default values. - /// - /// - /// An object that contains constraints for the endpoint. The object's properties represent the names and values - /// of the constraints. - /// - /// - /// An object that contains data tokens for the endpoint. The object's properties represent the names and values - /// of the data tokens. - /// - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapEndpoint(this MvcEndpointInfoBuilder endpointBuilder, string name, string template, object defaults, object constraints, object dataTokens) - { - endpointBuilder.EndpointInfos.Add(new MvcEndpointInfo( - name, - template, - new RouteValueDictionary(defaults), - new RouteValueDictionary(constraints), - new RouteValueDictionary(dataTokens), - endpointBuilder.ConstraintResolver)); - - return endpointBuilder; - } - #endregion - - #region MapAreaEndpoint - /// - /// Adds a endpoint to the with the given MVC area with the specified - /// , and . - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The MVC area name. - /// The URL pattern of the endpoint. - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapAreaEndpoint( - this MvcEndpointInfoBuilder endpointBuilder, - string name, - string areaName, - string template) - { - MapAreaEndpoint(endpointBuilder, name, areaName, template, defaults: null, constraints: null, dataTokens: null); - return endpointBuilder; - } - - /// - /// Adds a endpoint to the with the given MVC area with the specified - /// , , , and - /// . - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The MVC area name. - /// The URL pattern of the endpoint. - /// - /// An object that contains default values for endpoint parameters. The object's properties represent the - /// names and values of the default values. - /// - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapAreaEndpoint( - this MvcEndpointInfoBuilder endpointBuilder, - string name, - string areaName, - string template, - object defaults) - { - MapAreaEndpoint(endpointBuilder, name, areaName, template, defaults, constraints: null, dataTokens: null); - return endpointBuilder; - } - - /// - /// Adds a endpoint to the with the given MVC area with the specified - /// , , , - /// , and . - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The MVC area name. - /// The URL pattern of the endpoint. - /// - /// An object that contains default values for endpoint parameters. The object's properties represent the - /// names and values of the default values. - /// - /// - /// An object that contains constraints for the endpoint. The object's properties represent the names and - /// values of the constraints. - /// - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapAreaEndpoint( - this MvcEndpointInfoBuilder endpointBuilder, - string name, - string areaName, - string template, - object defaults, - object constraints) - { - MapAreaEndpoint(endpointBuilder, name, areaName, template, defaults, constraints, dataTokens: null); - return endpointBuilder; - } - - /// - /// Adds a endpoint to the with the given MVC area with the specified - /// , , , - /// , , and . - /// - /// The to add the endpoint to. - /// The name of the endpoint. - /// The MVC area name. - /// The URL pattern of the endpoint. - /// - /// An object that contains default values for endpoint parameters. The object's properties represent the - /// names and values of the default values. - /// - /// - /// An object that contains constraints for the endpoint. The object's properties represent the names and - /// values of the constraints. - /// - /// - /// An object that contains data tokens for the endpoint. The object's properties represent the names and - /// values of the data tokens. - /// - /// A reference to this instance after the operation has completed. - public static MvcEndpointInfoBuilder MapAreaEndpoint( - this MvcEndpointInfoBuilder endpointBuilder, - string name, - string areaName, - string template, - object defaults, - object constraints, - object dataTokens) - { - if (endpointBuilder == null) - { - throw new ArgumentNullException(nameof(endpointBuilder)); - } - - if (string.IsNullOrEmpty(areaName)) - { - throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(areaName)); - } - - var defaultsDictionary = new RouteValueDictionary(defaults); - defaultsDictionary["area"] = defaultsDictionary["area"] ?? areaName; - - var constraintsDictionary = new RouteValueDictionary(constraints); - constraintsDictionary["area"] = constraintsDictionary["area"] ?? new StringRouteConstraint(areaName); - - endpointBuilder.MapEndpoint(name, template, defaultsDictionary, constraintsDictionary, dataTokens); - return endpointBuilder; - } - #endregion - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcEndpointDataSource.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcEndpointDataSource.cs index 180b34011f..3770856e64 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcEndpointDataSource.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/MvcEndpointDataSource.cs @@ -442,7 +442,6 @@ namespace Microsoft.AspNetCore.Mvc.Internal } } - // REVIEW: Infos added after endpoints are initialized will not be used public List ConventionalEndpointInfos { get; } private class RouteNameMetadata : IRouteNameMetadata diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs index 39fd3847db..d731a697f2 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcApplicationBuilderExtensionsTest.cs @@ -37,26 +37,6 @@ namespace Microsoft.AspNetCore.Mvc.Core.Builder exception.Message); } - [Fact] - public void UseMvcWithEndpoint_ThrowsInvalidOperationException_IfMvcMarkerServiceIsNotRegistered() - { - // Arrange - var applicationBuilderMock = new Mock(); - applicationBuilderMock - .Setup(s => s.ApplicationServices) - .Returns(Mock.Of()); - - // Act & Assert - var exception = Assert.Throws( - () => applicationBuilderMock.Object.UseMvcWithEndpoint(rb => { })); - - Assert.Equal( - "Unable to find the required services. Please add all the required services by calling " + - "'IServiceCollection.AddMvc' inside the call to 'ConfigureServices(...)' " + - "in the application startup code.", - exception.Message); - } - [Fact] public void UseMvc_EndpointRoutingDisabled_NoEndpointInfos() { diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcEndpointInfoBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcEndpointInfoBuilderExtensionsTest.cs deleted file mode 100644 index 4a1431793b..0000000000 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Builder/MvcEndpointInfoBuilderExtensionsTest.cs +++ /dev/null @@ -1,282 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.Routing.Constraints; -using Moq; -using Xunit; - -namespace Microsoft.AspNetCore.Mvc.Core.Test.Builder -{ - public class MvcEndpointInfoBuilderExtensionsTest - { - #region MapAreaEndpoint - [Fact] - public void MapAreaEndpoint_Simple() - { - // Arrange - var builder = CreateEndpointBuilder(); - - // Act - builder.MapAreaEndpoint(name: null, areaName: "admin", template: "site/Admin/"); - - // Assert - var endpointInfo = Assert.Single(builder.EndpointInfos); - - Assert.Null(endpointInfo.Name); - Assert.Equal("site/Admin/", endpointInfo.Template); - Assert.Collection( - endpointInfo.Constraints.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.IsType(kvp.Value); - }); - Assert.Empty(endpointInfo.DataTokens); - Assert.Collection( - endpointInfo.Defaults.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.Equal("admin", kvp.Value); - }); - } - - [Fact] - public void MapAreaEndpoint_Defaults() - { - // Arrange - var builder = CreateEndpointBuilder(); - - // Act - builder.MapAreaEndpoint( - name: "admin_area", - areaName: "admin", - template: "site/Admin/", - defaults: new { action = "Home" }); - - // Assert - var endpointInfo = Assert.Single(builder.EndpointInfos); - - Assert.Equal("admin_area", endpointInfo.Name); - Assert.Equal("site/Admin/", endpointInfo.Template); - Assert.Collection( - endpointInfo.Constraints.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.IsType(kvp.Value); - }); - Assert.Empty(endpointInfo.DataTokens); - Assert.Collection( - endpointInfo.Defaults.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("action", kvp.Key); - Assert.Equal("Home", kvp.Value); - }, - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.Equal("admin", kvp.Value); - }); - } - - [Fact] - public void MapAreaEndpoint_DefaultsAndConstraints() - { - // Arrange - var builder = CreateEndpointBuilder(); - - // Act - builder.MapAreaEndpoint( - name: "admin_area", - areaName: "admin", - template: "site/Admin/", - defaults: new { action = "Home" }, - constraints: new { id = new IntRouteConstraint() }); - - // Assert - var endpointInfo = Assert.Single(builder.EndpointInfos); - - Assert.Equal("admin_area", endpointInfo.Name); - Assert.Equal("site/Admin/", endpointInfo.Template); - Assert.Collection( - endpointInfo.Constraints.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.IsType(kvp.Value); - }, - kvp => - { - Assert.Equal("id", kvp.Key); - Assert.IsType(kvp.Value); - }); - Assert.Empty(endpointInfo.DataTokens); - Assert.Collection( - endpointInfo.Defaults.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("action", kvp.Key); - Assert.Equal("Home", kvp.Value); - }, - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.Equal("admin", kvp.Value); - }); - } - - [Fact] - public void MapAreaEndpoint_DefaultsConstraintsAndDataTokens() - { - // Arrange - var builder = CreateEndpointBuilder(); - - // Act - builder.MapAreaEndpoint( - name: "admin_area", - areaName: "admin", - template: "site/Admin/", - defaults: new { action = "Home" }, - constraints: new { id = new IntRouteConstraint() }, - dataTokens: new { some_token = "hello" }); - - // Assert - var endpointInfo = Assert.Single(builder.EndpointInfos); - - Assert.Equal("admin_area", endpointInfo.Name); - Assert.Equal("site/Admin/", endpointInfo.Template); - Assert.Collection( - endpointInfo.Constraints.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.IsType(kvp.Value); - }, - kvp => - { - Assert.Equal("id", kvp.Key); - Assert.IsType(kvp.Value); - }); - Assert.Collection( - endpointInfo.DataTokens.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("some_token", kvp.Key); - Assert.Equal("hello", kvp.Value); - }); - Assert.Collection( - endpointInfo.Defaults.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("action", kvp.Key); - Assert.Equal("Home", kvp.Value); - }, - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.Equal("admin", kvp.Value); - }); - } - - [Fact] - public void MapAreaEndpoint_DoesNotReplaceValuesForAreaIfAlreadyPresentInConstraintsOrDefaults() - { - // Arrange - var builder = CreateEndpointBuilder(); - - // Act - builder.MapAreaEndpoint( - name: "admin_area", - areaName: "admin", - template: "site/Admin/", - defaults: new { area = "Home" }, - constraints: new { area = new IntRouteConstraint() }, - dataTokens: new { some_token = "hello" }); - - // Assert - var endpointInfo = Assert.Single(builder.EndpointInfos); - - Assert.Equal("admin_area", endpointInfo.Name); - Assert.Equal("site/Admin/", endpointInfo.Template); - Assert.Collection( - endpointInfo.Constraints.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.IsType(kvp.Value); - }); - Assert.Collection( - endpointInfo.DataTokens.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("some_token", kvp.Key); - Assert.Equal("hello", kvp.Value); - }); - Assert.Collection( - endpointInfo.Defaults.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.Equal("Home", kvp.Value); - }); - } - - [Fact] - public void MapAreaEndpoint_UsesPassedInAreaNameAsIs() - { - // Arrange - var builder = CreateEndpointBuilder(); - var areaName = "user.admin"; - - // Act - builder.MapAreaEndpoint(name: null, areaName: areaName, template: "site/Admin/"); - - // Assert - var endpointInfo = Assert.Single(builder.EndpointInfos); - - Assert.Null(endpointInfo.Name); - Assert.Equal("site/Admin/", endpointInfo.Template); - Assert.Collection( - endpointInfo.Constraints.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.IsType(kvp.Value); - - var values = new RouteValueDictionary(new { area = areaName }); - var match = kvp.Value.Match( - new DefaultHttpContext(), - route: new Mock().Object, - routeKey: kvp.Key, - values: values, - routeDirection: RouteDirection.UrlGeneration); - - Assert.True(match); - }); - Assert.Empty(endpointInfo.DataTokens); - Assert.Collection( - endpointInfo.Defaults.OrderBy(kvp => kvp.Key), - kvp => - { - Assert.Equal("area", kvp.Key); - Assert.Equal(kvp.Value, areaName); - }); - } - #endregion - - private MvcEndpointInfoBuilder CreateEndpointBuilder() - { - var builder = new MvcEndpointInfoBuilder(Mock.Of()); - return builder; - } - } -} \ No newline at end of file