From 959b2b841039ec100292c615a118d7b3ef56cb6a Mon Sep 17 00:00:00 2001 From: isc30 Date: Sun, 3 May 2020 17:57:16 +0200 Subject: [PATCH 1/4] Inline XML Documentation --- .../BlazorLazyLoading.Components.csproj | 4 +++ .../BlazorLazyLoading.Server.csproj | 1 + .../StartupExtensions.cs | 35 ++++++++++++++++--- .../BlazorLazyLoading.Wasm.csproj | 1 + .../StartupExtensions.cs | 15 ++++++-- .../AssemblyLoader.Server.csproj | 1 + .../AssemblyLoader.Wasm.csproj | 1 + .../Services/AppDomainAssemblyLoadContext.cs | 2 +- .../AppDomainAssemblyLoadContextFactory.cs | 2 +- src/AssemblyLoader/AssemblyLoader.csproj | 1 + .../AssemblyByNameAndVersionComparer.cs | 1 - src/AssemblyLoader/Helpers/HashCode.cs | 19 ---------- src/AssemblyLoader/Models/AssemblyData.cs | 7 ++++ .../Models/AssemblyLoaderContext.cs | 13 +++++++ src/LazyComponents/LazyComponents.csproj | 1 + .../ManifestGenerator.csproj | 1 + src/ManifestReader/ManifestReader.csproj | 1 + 17 files changed, 77 insertions(+), 29 deletions(-) delete mode 100644 src/AssemblyLoader/Helpers/HashCode.cs diff --git a/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj b/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj index bbfa08e..c6b0927 100644 --- a/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj +++ b/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj @@ -3,6 +3,10 @@ netstandard2.1 3.0 + 8.0 + enable + true + true diff --git a/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj b/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj index 17cd670..7d801ca 100644 --- a/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj +++ b/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj @@ -6,6 +6,7 @@ 8.0 enable true + true diff --git a/nuget/BlazorLazyLoading.Server/StartupExtensions.cs b/nuget/BlazorLazyLoading.Server/StartupExtensions.cs index 8eda079..9c0518d 100644 --- a/nuget/BlazorLazyLoading.Server/StartupExtensions.cs +++ b/nuget/BlazorLazyLoading.Server/StartupExtensions.cs @@ -11,13 +11,27 @@ namespace BlazorLazyLoading.Server { + /// + /// Server startup extensions for BlazorLazyLoading + /// public static class BLLServerStartupExtensions { + /// + /// Registers BlazorLazyLoading services + /// public static IServiceCollection AddLazyLoading( this IServiceCollection services, LazyLoadingOptions options) { - services.AddScoped(); + if (options.UseAssemblyIsolation) + { + services.AddScoped(); + } + else + { + services.AddSingleton(); + } + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -38,6 +52,9 @@ public static IServiceCollection AddLazyLoading( return services; } + /// + /// Configures the host to use BlazorLazyLoading + /// public static void UseLazyLoading( this IApplicationBuilder app) { @@ -53,13 +70,23 @@ public static void UseLazyLoading( } } + /// + /// BlazorLazyLoading options + /// public sealed class LazyLoadingOptions { /// - /// Specifies a list of Module Names (hints) to: - /// - Download DLLs from them - /// - Use their manifest to locate lazy resources + ///
Specifies a list of Module Names (hints) to:
+ ///
- Download DLLs from them
+ ///
- Use their manifest to locate lazy resources
///
public IEnumerable ModuleHints { get; set; } = Array.Empty(); + + /// + ///
Configures assembly isolation level. Do NOT set this to 'false' unless you want to share 'static' fields between users.
+ ///
Keeping this enabled ensures that the server can be scaled horizontally.
+ ///
default: true
+ ///
+ public bool UseAssemblyIsolation { get; set; } = true; } } diff --git a/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj b/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj index a433129..8c5b83a 100644 --- a/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj +++ b/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj @@ -5,6 +5,7 @@ 8.0 enable true + true diff --git a/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs b/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs index d5bd9ee..9f25aa3 100644 --- a/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs +++ b/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs @@ -7,8 +7,14 @@ namespace BlazorLazyLoading.Wasm { + /// + /// WebAssembly startup extensions for BlazorLazyLoading + /// public static class BLLWasmStartupExtensions { + /// + /// Registers BlazorLazyLoading services + /// public static IServiceCollection AddLazyLoading( this IServiceCollection services, LazyLoadingOptions options) @@ -29,12 +35,15 @@ public static IServiceCollection AddLazyLoading( } } + /// + /// BlazorLazyLoading options + /// public sealed class LazyLoadingOptions { /// - /// Specifies a list of Module Names (hints) to: - /// - Download DLLs from them - /// - Use their manifest to locate lazy resources + ///
Specifies a list of Module Names (hints) to:
+ ///
- Download DLLs from them
+ ///
- Use their manifest to locate lazy resources
///
public IEnumerable ModuleHints { get; set; } = Array.Empty(); } diff --git a/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj b/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj index 509fe08..79a84ab 100644 --- a/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj +++ b/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj @@ -6,6 +6,7 @@ 8.0 enable true + true diff --git a/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj b/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj index 8f6be75..c440980 100644 --- a/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj +++ b/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj @@ -5,6 +5,7 @@ 8.0 enable true + true diff --git a/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContext.cs b/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContext.cs index e8f6fc5..bfd6ec7 100644 --- a/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContext.cs +++ b/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContext.cs @@ -9,7 +9,7 @@ namespace BlazorLazyLoading.Wasm.Services { - public sealed class AppDomainAssemblyLoadContext : IAssemblyLoadContext + internal sealed class AppDomainAssemblyLoadContext : IAssemblyLoadContext { private readonly object _domainLock = new object(); private readonly AppDomain _baseDomain; diff --git a/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs b/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs index af9f60c..105fc3c 100644 --- a/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs +++ b/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs @@ -2,7 +2,7 @@ namespace BlazorLazyLoading.Wasm.Services { - public sealed class AppDomainAssemblyLoadContextFactory : IAssemblyLoadContextFactory + internal sealed class AppDomainAssemblyLoadContextFactory : IAssemblyLoadContextFactory { public IAssemblyLoadContext Create(string name) { diff --git a/src/AssemblyLoader/AssemblyLoader.csproj b/src/AssemblyLoader/AssemblyLoader.csproj index 3ca51d4..58730ae 100644 --- a/src/AssemblyLoader/AssemblyLoader.csproj +++ b/src/AssemblyLoader/AssemblyLoader.csproj @@ -5,6 +5,7 @@ 8.0 enable true + true diff --git a/src/AssemblyLoader/Comparers/AssemblyByNameAndVersionComparer.cs b/src/AssemblyLoader/Comparers/AssemblyByNameAndVersionComparer.cs index 9566e5e..023013a 100644 --- a/src/AssemblyLoader/Comparers/AssemblyByNameAndVersionComparer.cs +++ b/src/AssemblyLoader/Comparers/AssemblyByNameAndVersionComparer.cs @@ -1,6 +1,5 @@ using System; using System.Reflection; -using BlazorLazyLoading.Helpers; namespace BlazorLazyLoading.Comparers { diff --git a/src/AssemblyLoader/Helpers/HashCode.cs b/src/AssemblyLoader/Helpers/HashCode.cs deleted file mode 100644 index 8a6fdb0..0000000 --- a/src/AssemblyLoader/Helpers/HashCode.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections.Generic; - -namespace BlazorLazyLoading.Helpers -{ - //public static class HashCode - //{ - // public static int Combine(params object[] instances) - // { - // int hash = 17; - - // foreach (var i in instances) - // { - // hash = unchecked((hash * 31) + (i?.GetHashCode() ?? 0)); - // } - - // return hash; - // } - //} -} diff --git a/src/AssemblyLoader/Models/AssemblyData.cs b/src/AssemblyLoader/Models/AssemblyData.cs index 57620d9..5223bc9 100644 --- a/src/AssemblyLoader/Models/AssemblyData.cs +++ b/src/AssemblyLoader/Models/AssemblyData.cs @@ -1,10 +1,17 @@ namespace BlazorLazyLoading.Models { + /// + /// Contains the data required to load an assembly + /// public sealed class AssemblyData { + /// Bytes from the DLL public readonly byte[] DllBytes; + + /// Bytes from the PDB public readonly byte[]? PdbBytes; + /// Constructs AssemblyData public AssemblyData( byte[] dllBytes, byte[]? pdbBytes) diff --git a/src/AssemblyLoader/Models/AssemblyLoaderContext.cs b/src/AssemblyLoader/Models/AssemblyLoaderContext.cs index fe11308..287ee59 100644 --- a/src/AssemblyLoader/Models/AssemblyLoaderContext.cs +++ b/src/AssemblyLoader/Models/AssemblyLoaderContext.cs @@ -3,13 +3,23 @@ namespace BlazorLazyLoading.Models { + /// + /// Describes the tree of loaded assembly dependencies + /// public sealed class AssemblyLoaderContext { + /// + /// Assembly Name + /// public readonly AssemblyName AssemblyName; + /// Parent Node public readonly AssemblyLoaderContext? Parent = null; + + /// Children Nodes public readonly List Children = new List(); + /// Constructs the AssemblyLoaderContext public AssemblyLoaderContext(AssemblyName name) { AssemblyName = name; @@ -21,6 +31,9 @@ private AssemblyLoaderContext(AssemblyName name, AssemblyLoaderContext parent) AssemblyName = name; } + /// + /// Creates a new scope for the current AssemblyLoaderContext + /// public AssemblyLoaderContext NewScope(AssemblyName name) { var scope = new AssemblyLoaderContext(name, this); diff --git a/src/LazyComponents/LazyComponents.csproj b/src/LazyComponents/LazyComponents.csproj index d6abda6..8e8bfb5 100644 --- a/src/LazyComponents/LazyComponents.csproj +++ b/src/LazyComponents/LazyComponents.csproj @@ -5,6 +5,7 @@ 8.0 enable true + true diff --git a/src/ManifestGenerator/ManifestGenerator.csproj b/src/ManifestGenerator/ManifestGenerator.csproj index 17bfa71..27eb15f 100644 --- a/src/ManifestGenerator/ManifestGenerator.csproj +++ b/src/ManifestGenerator/ManifestGenerator.csproj @@ -5,6 +5,7 @@ 8.0 enable true + true diff --git a/src/ManifestReader/ManifestReader.csproj b/src/ManifestReader/ManifestReader.csproj index 413c914..64ba18b 100644 --- a/src/ManifestReader/ManifestReader.csproj +++ b/src/ManifestReader/ManifestReader.csproj @@ -5,6 +5,7 @@ 8.0 enable true + true From 68110983e01d498e274ce12b32e39c9dfb0351f4 Mon Sep 17 00:00:00 2001 From: isc30 Date: Mon, 4 May 2020 07:09:48 +0200 Subject: [PATCH 2/4] Only error for nullable related warnings --- .../BlazorLazyLoading.Components.csproj | 2 +- .../BlazorLazyLoading.Server.csproj | 2 +- .../StartupExtensions.cs | 7 +++++- .../BlazorLazyLoading.Wasm.csproj | 2 +- .../StartupExtensions.cs | 7 +++++- .../AssemblyLoader.Server.csproj | 2 +- .../AssemblyLoader.Wasm.csproj | 2 +- .../AppDomainAssemblyLoadContextFactory.cs | 2 +- .../Abstractions/IAssemblyDataLocator.cs | 5 +++- src/AssemblyLoader/AssemblyLoader.csproj | 2 +- .../Extensions/EnumerableExtensions.cs | 24 +++++++++++++++++++ .../Services/AssemblyDataLocator.cs | 9 +++++-- src/LazyComponents/LazyComponent/Lazy.cs | 20 +--------------- src/LazyComponents/LazyComponents.csproj | 2 +- src/LazyComponents/LazyRoute/LazyRouter.cs | 1 + .../ManifestGenerator.csproj | 2 +- src/ManifestReader/ManifestReader.csproj | 2 +- 17 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 src/AssemblyLoader/Extensions/EnumerableExtensions.cs diff --git a/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj b/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj index 0a911ed..ff65dbf 100644 --- a/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj +++ b/nuget/BlazorLazyLoading.Components/BlazorLazyLoading.Components.csproj @@ -5,7 +5,7 @@ 3.0 8.0 enable - true + nullable true diff --git a/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj b/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj index b47e064..bb76df5 100644 --- a/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj +++ b/nuget/BlazorLazyLoading.Server/BlazorLazyLoading.Server.csproj @@ -5,7 +5,7 @@ Library 8.0 enable - true + nullable true diff --git a/nuget/BlazorLazyLoading.Server/StartupExtensions.cs b/nuget/BlazorLazyLoading.Server/StartupExtensions.cs index 9c0518d..fc6fa17 100644 --- a/nuget/BlazorLazyLoading.Server/StartupExtensions.cs +++ b/nuget/BlazorLazyLoading.Server/StartupExtensions.cs @@ -33,8 +33,8 @@ public static IServiceCollection AddLazyLoading( } services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(typeof(IAssemblyDataLocator), options.AssemblyDataLocator ?? typeof(AssemblyDataLocator)); services.AddSingleton( p => @@ -88,5 +88,10 @@ public sealed class LazyLoadingOptions ///
default: true
/// public bool UseAssemblyIsolation { get; set; } = true; + + /// + /// Configures a custom AssemblyDataLocator. The type must implement IAssemblyDataLocator. + /// + public Type? AssemblyDataLocator { get; set; } = null; } } diff --git a/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj b/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj index 903a450..22aecb4 100644 --- a/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj +++ b/nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj @@ -4,7 +4,7 @@ netstandard2.1 8.0 enable - true + nullable true diff --git a/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs b/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs index 9f25aa3..1725fca 100644 --- a/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs +++ b/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs @@ -21,7 +21,7 @@ public static IServiceCollection AddLazyLoading( { services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(typeof(IAssemblyDataLocator), options.AssemblyDataLocator ?? typeof(AssemblyDataLocator)); services.AddSingleton(); services.AddSingleton(); @@ -46,5 +46,10 @@ public sealed class LazyLoadingOptions ///
- Use their manifest to locate lazy resources
/// public IEnumerable ModuleHints { get; set; } = Array.Empty(); + + /// + /// Configures a custom AssemblyDataLocator. The type must implement IAssemblyDataLocator. + /// + public Type? AssemblyDataLocator { get; set; } = null; } } diff --git a/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj b/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj index 4d57da6..a889bae 100644 --- a/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj +++ b/src/AssemblyLoader.Server/AssemblyLoader.Server.csproj @@ -5,7 +5,7 @@ Library 8.0 enable - true + nullable true diff --git a/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj b/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj index 960903f..01771f8 100644 --- a/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj +++ b/src/AssemblyLoader.Wasm/AssemblyLoader.Wasm.csproj @@ -4,7 +4,7 @@ netstandard2.1 8.0 enable - true + nullable true diff --git a/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs b/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs index 105fc3c..af9f60c 100644 --- a/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs +++ b/src/AssemblyLoader.Wasm/Services/AppDomainAssemblyLoadContextFactory.cs @@ -2,7 +2,7 @@ namespace BlazorLazyLoading.Wasm.Services { - internal sealed class AppDomainAssemblyLoadContextFactory : IAssemblyLoadContextFactory + public sealed class AppDomainAssemblyLoadContextFactory : IAssemblyLoadContextFactory { public IAssemblyLoadContext Create(string name) { diff --git a/src/AssemblyLoader/Abstractions/IAssemblyDataLocator.cs b/src/AssemblyLoader/Abstractions/IAssemblyDataLocator.cs index 87c5154..8440d4d 100644 --- a/src/AssemblyLoader/Abstractions/IAssemblyDataLocator.cs +++ b/src/AssemblyLoader/Abstractions/IAssemblyDataLocator.cs @@ -4,10 +4,13 @@ namespace BlazorLazyLoading.Abstractions { + /// + /// Locates assembly DLLs based on context and hints + /// public interface IAssemblyDataLocator { /// - /// Returns a list of possible paths where the assembly data is + /// Returns a list of possible paths to look for the assembly data (dll) /// public IEnumerable GetFindPaths(AssemblyName assemblyName, AssemblyLoaderContext context); } diff --git a/src/AssemblyLoader/AssemblyLoader.csproj b/src/AssemblyLoader/AssemblyLoader.csproj index a9952c0..d03d89d 100644 --- a/src/AssemblyLoader/AssemblyLoader.csproj +++ b/src/AssemblyLoader/AssemblyLoader.csproj @@ -4,7 +4,7 @@ netstandard2.1 8.0 enable - true + nullable true diff --git a/src/AssemblyLoader/Extensions/EnumerableExtensions.cs b/src/AssemblyLoader/Extensions/EnumerableExtensions.cs new file mode 100644 index 0000000..6b838db --- /dev/null +++ b/src/AssemblyLoader/Extensions/EnumerableExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace BlazorLazyLoading.Extensions +{ + public static class EnumerableExtensions + { + public static IEnumerable NotNull(this IEnumerable source) + where T : class + { + return source.Where(i => i != null).Cast(); + } + + public static IEnumerable DistinctBy( + this IEnumerable source, + Func selector) + { + return source + .GroupBy(m => selector(m)) + .Select(g => g.First()); + } + } +} diff --git a/src/AssemblyLoader/Services/AssemblyDataLocator.cs b/src/AssemblyLoader/Services/AssemblyDataLocator.cs index 41812e6..86ade6a 100644 --- a/src/AssemblyLoader/Services/AssemblyDataLocator.cs +++ b/src/AssemblyLoader/Services/AssemblyDataLocator.cs @@ -5,17 +5,22 @@ namespace BlazorLazyLoading.Services { - public sealed class AssemblyDataLocator : IAssemblyDataLocator + /// + /// Locates assembly DLLs based on context and hints + /// + public class AssemblyDataLocator : IAssemblyDataLocator { private readonly ILazyModuleHintsProvider _lazyModuleNamesProvider; + /// public AssemblyDataLocator( ILazyModuleHintsProvider lazyModuleProvider) { _lazyModuleNamesProvider = lazyModuleProvider; } - public IEnumerable GetFindPaths( + /// + public virtual IEnumerable GetFindPaths( AssemblyName assemblyName, AssemblyLoaderContext context) { diff --git a/src/LazyComponents/LazyComponent/Lazy.cs b/src/LazyComponents/LazyComponent/Lazy.cs index 42a2185..7763607 100644 --- a/src/LazyComponents/LazyComponent/Lazy.cs +++ b/src/LazyComponents/LazyComponent/Lazy.cs @@ -1,10 +1,10 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Threading.Tasks; using BlazorLazyLoading.Abstractions; +using BlazorLazyLoading.Extensions; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; using Newtonsoft.Json.Linq; @@ -137,22 +137,4 @@ private void BuildFallbackComponent(RenderTreeBuilder builder) builder.CloseElement(); } } - - public static class X - { - public static IEnumerable NotNull(this IEnumerable source) - where T : class - { - return source.Where(i => i != null).Cast(); - } - - public static IEnumerable DistinctBy( - this IEnumerable source, - Func selector) - { - return source - .GroupBy(m => selector(m)) - .Select(g => g.First()); - } - } } diff --git a/src/LazyComponents/LazyComponents.csproj b/src/LazyComponents/LazyComponents.csproj index 6cad318..641bc3e 100644 --- a/src/LazyComponents/LazyComponents.csproj +++ b/src/LazyComponents/LazyComponents.csproj @@ -4,7 +4,7 @@ netstandard2.1 8.0 enable - true + nullable true diff --git a/src/LazyComponents/LazyRoute/LazyRouter.cs b/src/LazyComponents/LazyRoute/LazyRouter.cs index c0b9261..abf5a65 100644 --- a/src/LazyComponents/LazyRoute/LazyRouter.cs +++ b/src/LazyComponents/LazyRoute/LazyRouter.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Threading.Tasks; using BlazorLazyLoading.Abstractions; +using BlazorLazyLoading.Extensions; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Routing; using Microsoft.Extensions.Logging; diff --git a/src/ManifestGenerator/ManifestGenerator.csproj b/src/ManifestGenerator/ManifestGenerator.csproj index 27eb15f..d2ee05c 100644 --- a/src/ManifestGenerator/ManifestGenerator.csproj +++ b/src/ManifestGenerator/ManifestGenerator.csproj @@ -4,7 +4,7 @@ netstandard2.0 8.0 enable - true + nullable true diff --git a/src/ManifestReader/ManifestReader.csproj b/src/ManifestReader/ManifestReader.csproj index 78db544..275aead 100644 --- a/src/ManifestReader/ManifestReader.csproj +++ b/src/ManifestReader/ManifestReader.csproj @@ -4,7 +4,7 @@ netstandard2.1 8.0 enable - true + nullable true From bcc62efc0229a688fb7836aa57bde04ca15658a6 Mon Sep 17 00:00:00 2001 From: isc30 Date: Mon, 4 May 2020 08:09:36 +0200 Subject: [PATCH 3/4] - fix small bug of incorrect cache of manifest paths - small improvements to lazy component (loading, error and required) --- demo/WasmHost/Pages/Counter.razor | 13 +++- .../StartupExtensions.cs | 4 +- .../StartupExtensions.cs | 4 +- .../Extensions/EnumerableExtensions.cs | 3 +- .../Services/AssemblyDataLocator.cs | 4 +- src/LazyComponents/LazyComponent/Lazy.cs | 61 ++++++++++++++----- .../Abstractions/IManifestLocator.cs | 3 + .../Services/ManifestLocator.cs | 5 ++ .../Services/ManifestRepository.cs | 12 +++- 9 files changed, 80 insertions(+), 29 deletions(-) diff --git a/demo/WasmHost/Pages/Counter.razor b/demo/WasmHost/Pages/Counter.razor index 7ad43fd..d5f2336 100644 --- a/demo/WasmHost/Pages/Counter.razor +++ b/demo/WasmHost/Pages/Counter.razor @@ -9,11 +9,18 @@ - + + + + +

Component loading errored...

+
- - + + + + @code { diff --git a/nuget/BlazorLazyLoading.Server/StartupExtensions.cs b/nuget/BlazorLazyLoading.Server/StartupExtensions.cs index fc6fa17..6e42716 100644 --- a/nuget/BlazorLazyLoading.Server/StartupExtensions.cs +++ b/nuget/BlazorLazyLoading.Server/StartupExtensions.cs @@ -76,9 +76,7 @@ public static void UseLazyLoading( public sealed class LazyLoadingOptions { /// - ///
Specifies a list of Module Names (hints) to:
- ///
- Download DLLs from them
- ///
- Use their manifest to locate lazy resources
+ /// Specifies a list of Module Names (hints) to download DLLs from them and use their manifest to locate lazy resources ///
public IEnumerable ModuleHints { get; set; } = Array.Empty(); diff --git a/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs b/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs index 1725fca..d88b75d 100644 --- a/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs +++ b/nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs @@ -41,9 +41,7 @@ public static IServiceCollection AddLazyLoading( public sealed class LazyLoadingOptions { /// - ///
Specifies a list of Module Names (hints) to:
- ///
- Download DLLs from them
- ///
- Use their manifest to locate lazy resources
+ /// Specifies a list of Module Names (hints) to download DLLs from them and use their manifest to locate lazy resources ///
public IEnumerable ModuleHints { get; set; } = Array.Empty(); diff --git a/src/AssemblyLoader/Extensions/EnumerableExtensions.cs b/src/AssemblyLoader/Extensions/EnumerableExtensions.cs index 6b838db..f8db812 100644 --- a/src/AssemblyLoader/Extensions/EnumerableExtensions.cs +++ b/src/AssemblyLoader/Extensions/EnumerableExtensions.cs @@ -6,7 +6,8 @@ namespace BlazorLazyLoading.Extensions { public static class EnumerableExtensions { - public static IEnumerable NotNull(this IEnumerable source) + public static IEnumerable NotNull( + this IEnumerable source) where T : class { return source.Where(i => i != null).Cast(); diff --git a/src/AssemblyLoader/Services/AssemblyDataLocator.cs b/src/AssemblyLoader/Services/AssemblyDataLocator.cs index 86ade6a..a3a721f 100644 --- a/src/AssemblyLoader/Services/AssemblyDataLocator.cs +++ b/src/AssemblyLoader/Services/AssemblyDataLocator.cs @@ -8,7 +8,7 @@ namespace BlazorLazyLoading.Services /// /// Locates assembly DLLs based on context and hints /// - public class AssemblyDataLocator : IAssemblyDataLocator + public sealed class AssemblyDataLocator : IAssemblyDataLocator { private readonly ILazyModuleHintsProvider _lazyModuleNamesProvider; @@ -20,7 +20,7 @@ public AssemblyDataLocator( } /// - public virtual IEnumerable GetFindPaths( + public IEnumerable GetFindPaths( AssemblyName assemblyName, AssemblyLoaderContext context) { diff --git a/src/LazyComponents/LazyComponent/Lazy.cs b/src/LazyComponents/LazyComponent/Lazy.cs index 7763607..d7c07e1 100644 --- a/src/LazyComponents/LazyComponent/Lazy.cs +++ b/src/LazyComponents/LazyComponent/Lazy.cs @@ -17,7 +17,13 @@ public class Lazy : ComponentBase public string Name { get; set; } = null!; [Parameter] - public RenderFragment? ChildContent { get; set; } = null; + public bool Required { get; set; } = false; + + [Parameter] + public RenderFragment? Loading { get; set; } = null; + + [Parameter] + public RenderFragment? Error { get; set; } = null; [Parameter] public Func? OnBeforeLoadAsync { get; set; } = null; @@ -35,8 +41,21 @@ public class Lazy : ComponentBase [Inject] private IManifestRepository _manifestRepository { get; set; } = null!; + private RenderFragment? _currentFallbackBuilder = null; + + protected override void OnInitialized() + { + _currentFallbackBuilder = Loading; + base.OnInitialized(); // trigger initial render + } + protected override async Task OnInitializedAsync() { + if (OnBeforeLoadAsync != null) + { + await OnBeforeLoadAsync(this); + } + await base.OnInitializedAsync().ConfigureAwait(false); var allManifests = await _manifestRepository.GetAllAsync().ConfigureAwait(false); @@ -71,6 +90,7 @@ protected override async Task OnInitializedAsync() })); var bestMatches = manifests + .Where(i => i.Score > 0) .GroupBy(i => i.Score) .OrderByDescending(i => i.Key) .FirstOrDefault() @@ -78,13 +98,16 @@ protected override async Task OnInitializedAsync() if (bestMatches == null || !bestMatches.Any()) { - Debug.WriteLine($"Unable to find lazy component '{Name}'"); + DisplayErrorView(); + ThrowIfRequired($"Unable to find lazy component '{Name}'. Required: {(Required ? "true" : "false")}"); return; } if (bestMatches.Count > 1) { - throw new NotSupportedException($"Multiple matches for Component with name '{Name}': '{string.Join(";", bestMatches.Select(m => m.Match.TypeFullName))}'"); + DisplayErrorView(); + ThrowIfRequired($"Multiple matches for Component with name '{Name}': '{string.Join(";", bestMatches.Select(m => m.Match.TypeFullName))}'"); + return; } var bestMatch = bestMatches.First(); @@ -97,12 +120,13 @@ protected override async Task OnInitializedAsync() }) .ConfigureAwait(false); - if (OnBeforeLoadAsync != null) + Type = componentAssembly?.GetType(bestMatch.Match.TypeFullName); + + if (Type == null) { - await OnBeforeLoadAsync(this); + DisplayErrorView(false); } - Type = componentAssembly?.GetType(bestMatch.Match.TypeFullName); StateHasChanged(); } @@ -125,16 +149,25 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) private void BuildFallbackComponent(RenderTreeBuilder builder) { - if (ChildContent != null) + _currentFallbackBuilder?.Invoke(builder); + } + + private void DisplayErrorView(bool render = true) + { + _currentFallbackBuilder = Error; + if (render) StateHasChanged(); + } + + private void ThrowIfRequired(string errorMessage) + { + if (Required) { - ChildContent.Invoke(builder); - return; + throw new NotSupportedException(errorMessage); + } + else + { + Debug.WriteLine(errorMessage); } - - builder.OpenElement(0, "div"); - builder.AddAttribute(1, "class", "bll-loading"); - builder.AddContent(2, "Loading..."); - builder.CloseElement(); } } } diff --git a/src/ManifestReader/Abstractions/IManifestLocator.cs b/src/ManifestReader/Abstractions/IManifestLocator.cs index 5953775..b021736 100644 --- a/src/ManifestReader/Abstractions/IManifestLocator.cs +++ b/src/ManifestReader/Abstractions/IManifestLocator.cs @@ -2,6 +2,9 @@ namespace BlazorLazyLoading.Abstractions { + /// + /// Locates _lazy.json manifests + /// public interface IManifestLocator { /// diff --git a/src/ManifestReader/Services/ManifestLocator.cs b/src/ManifestReader/Services/ManifestLocator.cs index 9787ef9..22f0274 100644 --- a/src/ManifestReader/Services/ManifestLocator.cs +++ b/src/ManifestReader/Services/ManifestLocator.cs @@ -4,16 +4,21 @@ namespace BlazorLazyLoading.Services { + /// + /// Locates _lazy.json manifests based on hints + /// public sealed class ManifestLocator : IManifestLocator { private readonly ILazyModuleHintsProvider _moduleHintsProvider; + /// public ManifestLocator( ILazyModuleHintsProvider moduleHintsProvider) { _moduleHintsProvider = moduleHintsProvider; } + /// public IEnumerable GetManifestPaths() { return _moduleHintsProvider.ModuleNameHints diff --git a/src/ManifestReader/Services/ManifestRepository.cs b/src/ManifestReader/Services/ManifestRepository.cs index afa5275..00043a6 100644 --- a/src/ManifestReader/Services/ManifestRepository.cs +++ b/src/ManifestReader/Services/ManifestRepository.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using BlazorLazyLoading.Abstractions; +using BlazorLazyLoading.Extensions; using BlazorLazyLoading.Models; using Newtonsoft.Json.Linq; @@ -70,7 +71,7 @@ private async Task FetchAllManifests() private async Task ReadAllManifests() { var manifestDataTasks = new Dictionary>(); - var manifestPaths = _manifestLocator.GetManifestPaths(); + var manifestPaths = _manifestLocator.GetManifestPaths().Except(_loadedPaths.Keys); foreach (var path in manifestPaths) { @@ -79,11 +80,16 @@ private async Task ReadAllManifests() await Task.WhenAll(manifestDataTasks.Values.ToArray()).ConfigureAwait(false); - var manifestData = manifestDataTasks + var pathData = manifestDataTasks .Select(i => new { Path = i.Key, Data = i.Value.Result }) .ToList(); - var manifestModels = manifestData + foreach (var path in pathData) + { + _loadedPaths.TryAdd(path.Path, path.Data); + } + + var manifestModels = pathData .Where(i => i.Data != null) .ToDictionary(i => i.Path, i => JObject.Parse(Encoding.UTF8.GetString(i.Data!))); From a1d25a28d4c5814f7d9e946d87ab041181f5d45c Mon Sep 17 00:00:00 2001 From: isc30 Date: Mon, 4 May 2020 08:10:37 +0200 Subject: [PATCH 4/4] fix OnBeforeLoadAsync --- src/LazyComponents/LazyComponent/Lazy.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LazyComponents/LazyComponent/Lazy.cs b/src/LazyComponents/LazyComponent/Lazy.cs index d7c07e1..f757813 100644 --- a/src/LazyComponents/LazyComponent/Lazy.cs +++ b/src/LazyComponents/LazyComponent/Lazy.cs @@ -51,13 +51,13 @@ protected override void OnInitialized() protected override async Task OnInitializedAsync() { + await base.OnInitializedAsync().ConfigureAwait(false); + if (OnBeforeLoadAsync != null) { await OnBeforeLoadAsync(this); } - await base.OnInitializedAsync().ConfigureAwait(false); - var allManifests = await _manifestRepository.GetAllAsync().ConfigureAwait(false); var manifests = allManifests