Skip to content

Commit d1141d5

Browse files
authored
Merge pull request #1881 from riganti/bug/route-groups
Refactoring & cleanup in routes & route groups + bug fixes
2 parents f698113 + bc705e7 commit d1141d5

24 files changed

+244
-303
lines changed

src/Framework/Framework/Compilation/DotHtmlFileInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public sealed class DotHtmlFileInfo
1414
public ImmutableArray<CompilationDiagnosticViewModel> Warnings { get; internal set; } = ImmutableArray<CompilationDiagnosticViewModel>.Empty;
1515

1616
/// <summary>Gets or sets the virtual path to the view.</summary>
17-
public string VirtualPath { get; }
17+
public string? VirtualPath { get; }
1818

1919
public string? TagName { get; }
2020
public string? Namespace { get; }
@@ -25,7 +25,7 @@ public sealed class DotHtmlFileInfo
2525
public ImmutableArray<string>? DefaultValues { get; }
2626
public bool? HasParameters { get; }
2727

28-
public DotHtmlFileInfo(string virtualPath, string? tagName = null, string? nameSpace = null, string? assembly = null, string? tagPrefix = null, string? url = null, string? routeName = null, ImmutableArray<string>? defaultValues = null, bool? hasParameters = null)
28+
public DotHtmlFileInfo(string? virtualPath, string? tagName = null, string? nameSpace = null, string? assembly = null, string? tagPrefix = null, string? url = null, string? routeName = null, ImmutableArray<string>? defaultValues = null, bool? hasParameters = null)
2929
{
3030
VirtualPath = virtualPath;
3131
Status = IsDothtmlFile(virtualPath) ? CompilationState.None : CompilationState.NonCompilable;
@@ -40,7 +40,7 @@ public DotHtmlFileInfo(string virtualPath, string? tagName = null, string? nameS
4040
HasParameters = hasParameters;
4141
}
4242

43-
private static bool IsDothtmlFile(string virtualPath)
43+
private static bool IsDothtmlFile(string? virtualPath)
4444
{
4545
return !string.IsNullOrWhiteSpace(virtualPath) &&
4646
(

src/Framework/Framework/Compilation/DotvvmViewCompilationService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public async Task<bool> CompileAll(bool buildInParallel = true, bool forceRecomp
122122
var compilationTaskFactory = (DotHtmlFileInfo t) => () => {
123123
BuildView(t, forceRecompile, out var masterPage);
124124
if (masterPage != null && masterPage.Status == CompilationState.None)
125-
discoveredMasterPages.TryAdd(masterPage.VirtualPath, masterPage);
125+
discoveredMasterPages.TryAdd(masterPage.VirtualPath!, masterPage);
126126
};
127127

128128
var compileTasks = filesToCompile.Select(compilationTaskFactory).ToArray();
@@ -184,9 +184,9 @@ public bool BuildView(DotHtmlFileInfo file, bool forceRecompile, out DotHtmlFile
184184
try
185185
{
186186
if (forceRecompile)
187-
controlBuilderFactory.InvalidateCache(file.VirtualPath);
187+
controlBuilderFactory.InvalidateCache(file.VirtualPath!);
188188

189-
var pageBuilder = controlBuilderFactory.GetControlBuilder(file.VirtualPath);
189+
var pageBuilder = controlBuilderFactory.GetControlBuilder(file.VirtualPath!);
190190

191191
using var scopedServices = dotvvmConfiguration.ServiceProvider.CreateScope(); // dependencies that are configured as scoped cannot be resolved from root service provider
192192
scopedServices.ServiceProvider.GetRequiredService<DotvvmRequestContextStorage>().Context = new ViewCompilationFakeRequestContext(scopedServices.ServiceProvider);

src/Framework/Framework/Compilation/Static/StaticViewCompiler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using DotVVM.Framework.Configuration;
1111
using DotVVM.Framework.Hosting;
1212
using DotVVM.Framework.Security;
13+
using DotVVM.Framework.Utils;
1314
using Microsoft.CodeAnalysis;
1415
using Microsoft.Extensions.DependencyInjection;
1516
using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -33,7 +34,7 @@ public static ImmutableArray<DotvvmCompilationDiagnostic> CompileAll(
3334
diagnostics.AddRange(CompileNoThrow(configuration, markupControl!));
3435
}
3536

36-
var views = configuration.RouteTable.Select(r => r.VirtualPath).ToImmutableArray();
37+
var views = configuration.RouteTable.Select(r => r.VirtualPath).WhereNotNull().ToImmutableArray();
3738
foreach(var view in views)
3839
{
3940
diagnostics.AddRange(CompileNoThrow(configuration, view));

src/Framework/Framework/Configuration/FreezableDictionary.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static void Freeze<K, V>([AllowNull] ref IDictionary<K, V> dict)
2323
}
2424
}
2525
}
26-
sealed class FreezableDictionary<K, V> : IDictionary<K, V>, IReadOnlyCollection<KeyValuePair<K, V>>
26+
sealed class FreezableDictionary<K, V> : IDictionary<K, V>, IReadOnlyCollection<KeyValuePair<K, V>>, IReadOnlyDictionary<K, V>
2727
where K : notnull
2828
{
2929
private readonly Dictionary<K, V> dict;
@@ -95,12 +95,18 @@ public V this[K index]
9595
set { ThrowIfFrozen(); dict[index] = value; }
9696
}
9797

98+
public Dictionary<K, V> CreateCopy() => new(dict, dict.Comparer);
99+
98100
public int Count => dict.Count;
99101

100102
public bool IsReadOnly => isFrozen;
101103

102104
public ICollection<K> Keys => ((IDictionary<K, V>)dict).Keys.ToArray();
103105

104106
public ICollection<V> Values => ((IDictionary<K, V>)dict).Values.ToArray();
107+
108+
IEnumerable<K> IReadOnlyDictionary<K, V>.Keys => Keys;
109+
110+
IEnumerable<V> IReadOnlyDictionary<K, V>.Values => Values;
105111
}
106112
}

src/Framework/Framework/Controls/RouteLinkHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private static void EnsureValidBindingType(IBinding binding)
194194

195195
private static Dictionary<string, object?> ComposeNewRouteParameters(RouteLink control, IDotvvmRequestContext context, RouteBase route)
196196
{
197-
var parameters = new Dictionary<string, object?>(route.DefaultValues, StringComparer.OrdinalIgnoreCase);
197+
var parameters = route.CloneDefaultValues();
198198
foreach (var param in context.Parameters!)
199199
{
200200
parameters[param.Key] = param.Value;

src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using DotVVM.Framework.Configuration;
6+
using DotVVM.Framework.Utils;
67

78
namespace DotVVM.Framework.Hosting
89
{
@@ -39,7 +40,8 @@ public AggregateMarkupFileLoader()
3940
/// </summary>
4041
public string GetMarkupFileVirtualPath(IDotvvmRequestContext context)
4142
{
42-
return context.Route!.VirtualPath;
43+
return context.Route!.VirtualPath
44+
?? throw new Exception($"The route {context.Route.RouteName} must have a non-null virtual path.");
4345
}
4446
}
4547
}

src/Framework/Framework/Hosting/DefaultMarkupFileLoader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class DefaultMarkupFileLoader : IMarkupFileLoader
3838
/// </summary>
3939
public string GetMarkupFileVirtualPath(IDotvvmRequestContext context)
4040
{
41-
return context.Route!.VirtualPath;
41+
return context.Route!.VirtualPath
42+
?? throw new Exception($"The route {context.Route.RouteName} must have a non-null virtual path.");
4243
}
4344
}
4445
}

src/Framework/Framework/Hosting/EmbeddedMarkupFileLoader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public class EmbeddedMarkupFileLoader : IMarkupFileLoader
6363
/// </summary>
6464
public string GetMarkupFileVirtualPath(IDotvvmRequestContext context)
6565
{
66-
return context.Route!.VirtualPath;
66+
return context.Route!.VirtualPath
67+
?? throw new Exception($"The route {context.Route.RouteName} must have a non-null virtual path.");
6768
}
6869
}
6970
}

src/Framework/Framework/ResourceManagement/LocalResourceUrlManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public LocalResourceUrlManager(DotvvmConfiguration configuration, IResourceHashS
2828
this.resourceRoute = new DotvvmRoute(
2929
url: $"{HostingConstants.ResourceRouteName}/{{{HashParameterName}}}/{{{NameParameterName}:regex(.*)}}",
3030
virtualPath: "",
31+
name: $"_dotvvm_{nameof(LocalResourceUrlManager)}",
3132
defaultValues: null,
3233
presenterFactory: _ => throw new NotSupportedException(),
3334
configuration: configuration);

src/Framework/Framework/Routing/DefaultRouteStrategy.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ protected virtual RouteBase BuildRoute(RouteStrategyMarkupFileInfo file)
107107
var defaultParameters = GetRouteDefaultParameters(file);
108108
var presenterFactory = GetRoutePresenterFactory(file);
109109

110-
return new DotvvmRoute(url, file.AppRelativePath, defaultParameters, presenterFactory, configuration)
111-
{
112-
RouteName = routeName
113-
};
110+
return new DotvvmRoute(url, file.AppRelativePath, routeName, defaultParameters, presenterFactory, configuration);
114111
}
115112

116113
protected virtual string GetRouteName(RouteStrategyMarkupFileInfo file)
@@ -162,7 +159,7 @@ private static IEnumerable<string> GetRoutesForFile(string fileName)
162159
protected override IEnumerable<RouteBase> BuildRoutes(RouteStrategyMarkupFileInfo file)
163160
{
164161
return getRouteList(file.AppRelativePath)
165-
.Select(url => new DotvvmRoute(url, file.AppRelativePath, GetRouteDefaultParameters(file), GetRoutePresenterFactory(file), this.configuration) { RouteName = url });
162+
.Select(url => new DotvvmRoute(url, file.AppRelativePath, url, GetRouteDefaultParameters(file), GetRoutePresenterFactory(file), this.configuration));
166163
}
167164
}
168165
}

0 commit comments

Comments
 (0)