refactor: apply default vs transform to xproj

refactor(spa-services): clean code

refactor(node-services): clean code, extract classes nto separate files

refactor(angular-services): prime cache cleanup
This commit is contained in:
Andrei Tserakhau
2016-05-23 11:28:42 +03:00
parent 2c35945562
commit 95cba7f5dd
32 changed files with 621 additions and 448 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
@@ -7,26 +6,27 @@ namespace Microsoft.AspNetCore.SpaServices
{
internal class SpaRouteConstraint : IRouteConstraint
{
private readonly string clientRouteTokenName;
private readonly string _clientRouteTokenName;
public SpaRouteConstraint(string clientRouteTokenName) {
if (string.IsNullOrEmpty(clientRouteTokenName)) {
throw new ArgumentException("Value cannot be null or empty", "clientRouteTokenName");
public SpaRouteConstraint(string clientRouteTokenName)
{
if (string.IsNullOrEmpty(clientRouteTokenName))
{
throw new ArgumentException("Value cannot be null or empty", nameof(clientRouteTokenName));
}
this.clientRouteTokenName = clientRouteTokenName;
_clientRouteTokenName = clientRouteTokenName;
}
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
var clientRouteValue = (values[this.clientRouteTokenName] as string) ?? string.Empty;
return !HasDotInLastSegment(clientRouteValue);
}
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
RouteValueDictionary values,
RouteDirection routeDirection)
=> !HasDotInLastSegment(values[_clientRouteTokenName] as string ?? string.Empty);
private bool HasDotInLastSegment(string uri)
{
var lastSegmentStartPos = uri.LastIndexOf('/');
return uri.IndexOf('.', lastSegmentStartPos + 1) >= 0;
}
=> uri.IndexOf('.', uri.LastIndexOf('/') + 1) >= 0;
}
}
}

View File

@@ -4,18 +4,34 @@ using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SpaServices;
// Putting in this namespace so it's always available whenever MapRoute is
namespace Microsoft.AspNetCore.Builder
{
public static class SpaRouteExtensions
{
private const string ClientRouteTokenName = "clientRoute";
public static void MapSpaFallbackRoute(this IRouteBuilder routeBuilder, string name, object defaults, object constraints = null, object dataTokens = null)
{
MapSpaFallbackRoute(routeBuilder, name, /* templatePrefix */ (string)null, defaults, constraints, dataTokens);
}
public static void MapSpaFallbackRoute(
this IRouteBuilder routeBuilder,
string name,
object defaults,
object constraints = null,
object dataTokens = null)
=> MapSpaFallbackRoute(
routeBuilder,
name,
/* templatePrefix */ null,
defaults,
constraints,
dataTokens);
public static void MapSpaFallbackRoute(this IRouteBuilder routeBuilder, string name, string templatePrefix, object defaults, object constraints = null, object dataTokens = null)
public static void MapSpaFallbackRoute(
this IRouteBuilder routeBuilder,
string name,
string templatePrefix,
object defaults,
object constraints = null,
object dataTokens = null)
{
var template = CreateRouteTemplate(templatePrefix);
@@ -29,25 +45,27 @@ namespace Microsoft.AspNetCore.Builder
{
templatePrefix = templatePrefix ?? string.Empty;
if (templatePrefix.Contains("?")) {
if (templatePrefix.Contains("?"))
{
// TODO: Consider supporting this. The {*clientRoute} part should be added immediately before the '?'
throw new ArgumentException("SPA fallback route templates don't support querystrings");
}
if (templatePrefix.Contains("#")) {
throw new ArgumentException("SPA fallback route templates should not include # characters. The hash part of a URI does not get sent to the server.");
if (templatePrefix.Contains("#"))
{
throw new ArgumentException(
"SPA fallback route templates should not include # characters. The hash part of a URI does not get sent to the server.");
}
if (templatePrefix != string.Empty && !templatePrefix.EndsWith("/")) {
if (templatePrefix != string.Empty && !templatePrefix.EndsWith("/"))
{
templatePrefix += "/";
}
return templatePrefix + $"{{*{ ClientRouteTokenName }}}";
return templatePrefix + $"{{*{ClientRouteTokenName}}}";
}
private static IDictionary<string, object> ObjectToDictionary(object value)
{
return value as IDictionary<string, object> ?? new RouteValueDictionary(value);
}
=> value as IDictionary<string, object> ?? new RouteValueDictionary(value);
}
}
}