Merge pull request #6988 from NicolasDorier/ui-extension-registry

Refactor: Add UIExtensionsRegistry
This commit is contained in:
Nicolas Dorier
2025-11-12 12:07:11 +09:00
committed by GitHub
3 changed files with 21 additions and 12 deletions

View File

@@ -2,27 +2,18 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using BTCPayServer.Abstractions.Contracts; using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Components.UIExtensionPoint namespace BTCPayServer.Components.UIExtensionPoint
{ {
public class UiExtensionPoint : ViewComponent public class UiExtensionPoint(UIExtensionsRegistry uiExtensions) : ViewComponent
{ {
private readonly IEnumerable<IUIExtension> _uiExtensions;
public UiExtensionPoint(IEnumerable<IUIExtension> uiExtensions)
{
_uiExtensions = uiExtensions;
}
public IViewComponentResult Invoke(string location, object model) public IViewComponentResult Invoke(string location, object model)
{ {
return View(new UiExtensionPointViewModel() return View(new UiExtensionPointViewModel()
{ {
Partials = _uiExtensions Partials = uiExtensions.ExtensionsByLocation[location].Select(c => c.Partial).ToArray(),
.Where(extension =>
extension.Location.Equals(location, StringComparison.InvariantCultureIgnoreCase))
.Select(extension => extension.Partial).ToArray(),
Model = model Model = model
}); });
} }

View File

@@ -94,6 +94,7 @@ namespace BTCPayServer.Hosting
services.TryAddSingleton<ViewLocalizer>(); services.TryAddSingleton<ViewLocalizer>();
services.TryAddSingleton<IStringLocalizer>(o => o.GetRequiredService<IStringLocalizerFactory>().Create("","")); services.TryAddSingleton<IStringLocalizer>(o => o.GetRequiredService<IStringLocalizerFactory>().Create("",""));
services.TryAddSingleton<DelayedTaskScheduler>(); services.TryAddSingleton<DelayedTaskScheduler>();
services.TryAddSingleton<UIExtensionsRegistry>();
services.AddSingleton<MvcNewtonsoftJsonOptions>(o => o.GetRequiredService<IOptions<MvcNewtonsoftJsonOptions>>().Value); services.AddSingleton<MvcNewtonsoftJsonOptions>(o => o.GetRequiredService<IOptions<MvcNewtonsoftJsonOptions>>().Value);
services.AddSingleton<JsonSerializerSettings>(o => o.GetRequiredService<IOptions<MvcNewtonsoftJsonOptions>>().Value.SerializerSettings); services.AddSingleton<JsonSerializerSettings>(o => o.GetRequiredService<IOptions<MvcNewtonsoftJsonOptions>>().Value.SerializerSettings);

View File

@@ -0,0 +1,17 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Services;
namespace BTCPayServer.Services;
public class UIExtensionsRegistry
{
public UIExtensionsRegistry(IEnumerable<IUIExtension> uiExtensions)
{
ExtensionsByLocation = uiExtensions.OfType<UIExtension>().ToLookup(o => o.Location);
}
public ILookup<string, UIExtension> ExtensionsByLocation { get; }
}