From 09ce9c3041ba05a42a925f34548c0f6c0487c2e8 Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Thu, 2 Aug 2018 16:33:07 -0700 Subject: [PATCH] Api clean up --- .../EndsWithStringMatchProcessor.cs | 29 ++++++++++-- .../RoutingServiceCollectionExtensions.cs | 1 - .../INameMetadata.cs | 10 ---- .../Matching/MatchProcessorBase.cs | 42 ----------------- .../NameBasedEndpointFinder.cs | 46 ------------------- .../DefaultMatchProcessorFactoryTest.cs | 36 +++++++++++++-- .../RouteValueBasedEndpointFinderTest.cs | 34 +------------- 7 files changed, 57 insertions(+), 141 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Routing/INameMetadata.cs delete mode 100644 src/Microsoft.AspNetCore.Routing/Matching/MatchProcessorBase.cs delete mode 100644 src/Microsoft.AspNetCore.Routing/NameBasedEndpointFinder.cs diff --git a/samples/RoutingSample.Web/EndsWithStringMatchProcessor.cs b/samples/RoutingSample.Web/EndsWithStringMatchProcessor.cs index ea50812015..f0201f79d9 100644 --- a/samples/RoutingSample.Web/EndsWithStringMatchProcessor.cs +++ b/samples/RoutingSample.Web/EndsWithStringMatchProcessor.cs @@ -3,12 +3,14 @@ using System; using System.Globalization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Matching; using Microsoft.Extensions.Logging; namespace RoutingSample.Web { - internal class EndsWithStringMatchProcessor : MatchProcessorBase + internal class EndsWithStringMatchProcessor : MatchProcessor { private readonly ILogger _logger; @@ -17,15 +19,34 @@ namespace RoutingSample.Web _logger = logger; } - public override bool Process(object value) + public string ParameterName { get; private set; } + + public string ConstraintArgument { get; private set; } + + public override void Initialize(string parameterName, string constraintArgument) { - if (value == null) + ParameterName = parameterName; + ConstraintArgument = constraintArgument; + } + + public override bool ProcessInbound(HttpContext httpContext, RouteValueDictionary values) + { + return Process(values); + } + + public override bool ProcessOutbound(HttpContext httpContext, RouteValueDictionary values) + { + return Process(values); + } + + private bool Process(RouteValueDictionary values) + { + if (!values.TryGetValue(ParameterName, out var value) || value == null) { return false; } var valueString = Convert.ToString(value, CultureInfo.InvariantCulture); - var endsWith = valueString.EndsWith(ConstraintArgument, StringComparison.OrdinalIgnoreCase); if (!endsWith) diff --git a/src/Microsoft.AspNetCore.Routing/DependencyInjection/RoutingServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.Routing/DependencyInjection/RoutingServiceCollectionExtensions.cs index 42433f64c0..19f3d0e77a 100644 --- a/src/Microsoft.AspNetCore.Routing/DependencyInjection/RoutingServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.Routing/DependencyInjection/RoutingServiceCollectionExtensions.cs @@ -67,7 +67,6 @@ namespace Microsoft.Extensions.DependencyInjection services.TryAddTransient(); // Link generation related services - services.TryAddSingleton, NameBasedEndpointFinder>(); services.TryAddSingleton, RouteValuesBasedEndpointFinder>(); services.TryAddSingleton(); diff --git a/src/Microsoft.AspNetCore.Routing/INameMetadata.cs b/src/Microsoft.AspNetCore.Routing/INameMetadata.cs deleted file mode 100644 index c38ae7f0e5..0000000000 --- a/src/Microsoft.AspNetCore.Routing/INameMetadata.cs +++ /dev/null @@ -1,10 +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. - -namespace Microsoft.AspNetCore.Routing -{ - public interface INameMetadata - { - string Name { get; } - } -} diff --git a/src/Microsoft.AspNetCore.Routing/Matching/MatchProcessorBase.cs b/src/Microsoft.AspNetCore.Routing/Matching/MatchProcessorBase.cs deleted file mode 100644 index 9ac2196671..0000000000 --- a/src/Microsoft.AspNetCore.Routing/Matching/MatchProcessorBase.cs +++ /dev/null @@ -1,42 +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 Microsoft.AspNetCore.Http; - -namespace Microsoft.AspNetCore.Routing.Matching -{ - public abstract class MatchProcessorBase : MatchProcessor - { - public string ParameterName { get; private set; } - - public string ConstraintArgument { get; private set; } - - public override void Initialize(string parameterName, string constraintArgument) - { - ParameterName = parameterName; - ConstraintArgument = constraintArgument; - } - - public abstract bool Process(object value); - - public override bool ProcessInbound(HttpContext httpContext, RouteValueDictionary values) - { - return Process(values); - } - - public override bool ProcessOutbound(HttpContext httpContext, RouteValueDictionary values) - { - return Process(values); - } - - private bool Process(RouteValueDictionary values) - { - if (!values.TryGetValue(ParameterName, out var value) || value == null) - { - return false; - } - - return Process(value); - } - } -} diff --git a/src/Microsoft.AspNetCore.Routing/NameBasedEndpointFinder.cs b/src/Microsoft.AspNetCore.Routing/NameBasedEndpointFinder.cs deleted file mode 100644 index b10c2b7934..0000000000 --- a/src/Microsoft.AspNetCore.Routing/NameBasedEndpointFinder.cs +++ /dev/null @@ -1,46 +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 Microsoft.AspNetCore.Routing.Matching; -using Microsoft.Extensions.Logging; - -namespace Microsoft.AspNetCore.Routing -{ - internal class NameBasedEndpointFinder : IEndpointFinder - { - private readonly CompositeEndpointDataSource _endpointDatasource; - private readonly ILogger _logger; - - public NameBasedEndpointFinder( - CompositeEndpointDataSource endpointDataSource, - ILogger logger) - { - _endpointDatasource = endpointDataSource; - _logger = logger; - } - - public IEnumerable FindEndpoints(string endpointName) - { - if (string.IsNullOrEmpty(endpointName)) - { - return Array.Empty(); - } - - var endpoints = _endpointDatasource.Endpoints.OfType(); - - foreach (var endpoint in endpoints) - { - var nameMetadata = endpoint.Metadata.GetMetadata(); - if (nameMetadata != null && - string.Equals(endpointName, nameMetadata.Name, StringComparison.OrdinalIgnoreCase)) - { - return new[] { endpoint }; - } - } - return Array.Empty(); - } - } -} diff --git a/test/Microsoft.AspNetCore.Routing.Tests/Matching/DefaultMatchProcessorFactoryTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/Matching/DefaultMatchProcessorFactoryTest.cs index 1be24950b1..acfd78d29f 100644 --- a/test/Microsoft.AspNetCore.Routing.Tests/Matching/DefaultMatchProcessorFactoryTest.cs +++ b/test/Microsoft.AspNetCore.Routing.Tests/Matching/DefaultMatchProcessorFactoryTest.cs @@ -61,8 +61,8 @@ namespace Microsoft.AspNetCore.Routing.Matching var factory = GetMatchProcessorFactory(); var parameter = RoutePatternFactory.ParameterPart( - "id", - @default: null, + "id", + @default: null, parameterKind: RoutePatternParameterKind.Standard, constraints: new[] { RoutePatternFactory.Constraint("int"), }); @@ -282,12 +282,38 @@ namespace Microsoft.AspNetCore.Routing.Matching } } - private class EndsWithStringMatchProcessor : MatchProcessorBase + private class EndsWithStringMatchProcessor : MatchProcessor { - public override bool Process(object value) + public string ParameterName { get; private set; } + + public string ConstraintArgument { get; private set; } + + public override void Initialize(string parameterName, string constraintArgument) { + ParameterName = parameterName; + ConstraintArgument = constraintArgument; + } + + public override bool ProcessInbound(HttpContext httpContext, RouteValueDictionary values) + { + return Process(values); + } + + public override bool ProcessOutbound(HttpContext httpContext, RouteValueDictionary values) + { + return Process(values); + } + + private bool Process(RouteValueDictionary values) + { + if (!values.TryGetValue(ParameterName, out var value) || value == null) + { + return false; + } + var valueString = Convert.ToString(value, CultureInfo.InvariantCulture); - return valueString.EndsWith(ConstraintArgument); + var endsWith = valueString.EndsWith(ConstraintArgument, StringComparison.OrdinalIgnoreCase); + return endsWith; } } } diff --git a/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs index af2cd3faf9..25ce04bf82 100644 --- a/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs +++ b/test/Microsoft.AspNetCore.Routing.Tests/RouteValueBasedEndpointFinderTest.cs @@ -1,6 +1,7 @@ // 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.Threading.Tasks; @@ -79,29 +80,6 @@ namespace Microsoft.AspNetCore.Routing Assert.Same(endpoint3, Assert.IsType(namedMatches[1].Match.Entry.Data)); } - [Fact] - public void GetOutboundMatches_DoesNotGetNamedMatchesFor_EndpointsHaving_INameMetadata() - { - // Arrange - var endpoint1 = CreateEndpoint("/a"); - var endpoint2 = CreateEndpoint("/a", routeName: "named"); - var endpoint3 = CreateEndpoint( - "/b", - metadataCollection: new EndpointMetadataCollection(new[] { new NameMetadata("named") })); - - // Act - var finder = CreateEndpointFinder(endpoint1, endpoint2); - - // Assert - Assert.NotNull(finder.AllMatches); - Assert.Equal(2, finder.AllMatches.Count()); - Assert.NotNull(finder.NamedMatches); - Assert.True(finder.NamedMatches.TryGetValue("named", out var namedMatches)); - var namedMatch = Assert.Single(namedMatches); - var actual = Assert.IsType(namedMatch.Match.Entry.Data); - Assert.Same(endpoint2, actual); - } - [Fact] public void EndpointDataSource_ChangeCallback_Refreshes_OutboundMatches() { @@ -279,16 +257,6 @@ namespace Microsoft.AspNetCore.Routing null); } - private class NameMetadata : INameMetadata - { - public NameMetadata(string name) - { - Name = name; - } - - public string Name { get; } - } - private class CustomRouteValuesBasedEndpointFinder : RouteValuesBasedEndpointFinder { public CustomRouteValuesBasedEndpointFinder(