Skip to content

Commit 146778f

Browse files
authored
doc(IIpLocatorProvider): update use locator documentation (#6401)
* refactor: 重构代码提高可读性 * doc: 更新定位器文档
1 parent 2f59a8d commit 146778f

File tree

4 files changed

+25
-45
lines changed

4 files changed

+25
-45
lines changed

src/BootstrapBlazor.Server/Components/Samples/Locators.razor

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private IIpLocatorFactory? IpLocatorFactory { get; set; }
1717
</Tips>
1818
<Pre>Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)</Pre>
1919
<p><b>@Localizer["LocatorsNormalExtendDescription"]</b></p>
20+
2021
<p><b>@Localizer["LocatorsNormalExtend1"]</b></p>
2122
<Pre>private class CustomerLocatorProvider : DefaultIpLocatorProvider
2223
{
@@ -25,35 +26,20 @@ private IIpLocatorFactory? IpLocatorFactory { get; set; }
2526
throw new NotImplementedException();
2627
}
2728
}</Pre>
29+
2830
<p><b>@Localizer["LocatorsNormalExtend2"]</b></p>
2931
<Pre>services.AddSingleton&lt;IIpLocatorProvider, CustomerLocatorProvider&gt;();</Pre>
3032
<p>@((MarkupString)Localizer["LocatorsNormalCustomerLocator"].Value)</p>
33+
34+
<p><b>@Localizer["LocatorsNormalExtend3"]</b></p>
35+
<Pre>var provider = IpLocatorFactory.Create(ProviderName);
36+
Location = await provider.Locate(Ip);</Pre>
37+
3138
<p>@Localizer["LocatorsNormalIpTitle"]</p>
3239
<p><code>112.224.74.239</code> @Localizer["LocatorsNormalTips3"]</p>
3340
<p><code>183.160.236.53</code> @Localizer["LocatorsNormalTips4"]</p>
3441

3542
<DemoBlock Title="@Localizer["LocatorsNormalTitle"]" Introduction="@Localizer["LocatorsNormalIntro"]" Name="Normal">
36-
<section ignore>
37-
@((MarkupString)Localizer["LocatorsProviderDesc"].Value)
38-
<Tips>
39-
<p>@((MarkupString)Localizer["LocatorsProviderOptions"].Value)</p>
40-
</Tips>
41-
<Pre>{
42-
"BootstrapBlazorOptions": {
43-
"WebClientOptions": {
44-
"EnableIpLocator": true
45-
}
46-
}
47-
}</Pre>
48-
<Pre>services.AddBootstrapBlazor(op =>
49-
{
50-
op.WebClientOptions.EnableIpLocator = true;
51-
});</Pre>
52-
<Pre>services.Configure&lt;BootstrapBlazorOptions&gt;(op =>
53-
{
54-
op.WebClientOptions.EnableIpLocator = true;
55-
});</Pre>
56-
</section>
5743
<div class="row g-3 form-inline">
5844
<div class="col-12 col-sm-6">
5945
<Select Items="_providers" Value="@ProviderName" OnValueChanged="OnProviderNameChanged" ShowLabel="true" DisplayText="LocatorProvider">

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,7 @@
40954095
"LocatorsNormalExtendDescription": "Extend the custom geo-location query interface",
40964096
"LocatorsNormalExtend1": "1. Implement a custom locator",
40974097
"LocatorsNormalExtend2": "2. Configure a custom locator",
4098+
"LocatorsNormalExtend3": "3. Use locator",
40984099
"LocatorsNormalCustomerLocator": "Add <code>CustomerLocatorProvider</code> to the service container using the <code>AddSingleton</code> method",
40994100
"LocatorsNormalIpTitle": "IP test data",
41004101
"LocatorsNormalTips3": "Shandong, China Unicom",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,7 @@
40954095
"LocatorsNormalExtendDescription": "扩展自定义地理位置查询接口",
40964096
"LocatorsNormalExtend1": "1. 实现自定义定位器",
40974097
"LocatorsNormalExtend2": "2. 配置自定义定位器",
4098+
"LocatorsNormalExtend3": "3. 通过定位器定位",
40984099
"LocatorsNormalCustomerLocator": "通过 <code>AddSingleton</code> 方法将自定义定位器 <code>CustomerLocatorProvider</code> 添加到服务容器中",
40994100
"LocatorsNormalIpTitle": "IP 测试数据",
41004101
"LocatorsNormalTips3": "山东省 中国联通",

src/BootstrapBlazor/Services/IPLocator/DefaultIpLocatorFactory.cs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,31 @@ namespace BootstrapBlazor.Components;
1010
/// <summary>
1111
/// IIPLocatorFactory 接口实现类
1212
/// </summary>
13-
class DefaultIpLocatorFactory : IIpLocatorFactory
13+
class DefaultIpLocatorFactory(IServiceProvider provider, IOptionsMonitor<BootstrapBlazorOptions> options) : IIpLocatorFactory
1414
{
15-
private readonly Dictionary<string, IIpLocatorProvider> _providers = [];
16-
17-
private readonly IServiceProvider _serviceProvider;
18-
19-
private readonly IOptionsMonitor<BootstrapBlazorOptions> _options;
20-
21-
public DefaultIpLocatorFactory(IServiceProvider provider, IOptionsMonitor<BootstrapBlazorOptions> options)
22-
{
23-
_serviceProvider = provider;
24-
_options = options;
25-
26-
foreach (var p in provider.GetServices<IIpLocatorProvider>())
27-
{
28-
if (p.Key != null)
29-
{
30-
_providers[p.Key] = p;
31-
}
32-
}
33-
}
15+
private Dictionary<string, IIpLocatorProvider>? _providers = null;
3416

3517
/// <summary>
3618
/// 创建 <see cref="IIpLocatorProvider"/> 实例方法
3719
/// </summary>
3820
/// <param name="key"></param>
3921
public IIpLocatorProvider Create(string? key = null)
4022
{
41-
var providerKey = key;
42-
if (string.IsNullOrEmpty(key))
23+
_providers ??= GenerateProviders();
24+
var providerKey = key ?? options.CurrentValue.IpLocatorOptions.ProviderName;
25+
return string.IsNullOrEmpty(providerKey) ? _providers.Values.Last() : _providers[providerKey];
26+
}
27+
28+
private Dictionary<string, IIpLocatorProvider> GenerateProviders()
29+
{
30+
var providers = new Dictionary<string, IIpLocatorProvider>();
31+
foreach (var p in provider.GetServices<IIpLocatorProvider>())
4332
{
44-
providerKey = _options.CurrentValue.IpLocatorOptions.ProviderName;
33+
if (p.Key != null)
34+
{
35+
providers[p.Key] = p;
36+
}
4537
}
46-
return string.IsNullOrEmpty(providerKey) ? _providers.Values.Last() : _providers[providerKey];
38+
return providers;
4739
}
4840
}

0 commit comments

Comments
 (0)