Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

允许从配置里面重新获取配置管理应用 #71

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@
<RepositoryType>git</RepositoryType>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<Copyright>Copyright (c) dotnet-campus 2020-$([System.DateTime]::Now.ToString(`yyyy`))</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
<!-- 嵌入源代码到符号文件,方便调试 -->
<EmbedAllSources>true</EmbedAllSources>
<!-- 输出符号文件 -->
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<!-- 嵌入 README 文件 -->
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath="\" Visible="false"/>
</ItemGroup>
</Project>
20 changes: 20 additions & 0 deletions src/dotnetCampus.Configurations/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using dotnetCampus.Configurations.Core;
Expand Down Expand Up @@ -271,5 +272,24 @@ protected void ClearValues()
/// 获取用于管理应用程序字符串配置项的管理器。
/// </summary>
internal IConfigurationRepo? Repo { get; set; }

internal IAppConfigurator? AppConfigurator { get; set; }

/// <summary>
/// 尝试获取 <see cref="IAppConfigurator"/> 实例。只有配置框架内部创建的配置,才能获取到 <see cref="IAppConfigurator"/> 实例。
/// </summary>
/// <param name="appConfigurator"></param>
/// <returns></returns>
public bool TryGetAppConfigurator
(
#if NETCOREAPP3_0_OR_GREATER
[NotNullWhen(true)]
#endif
out IAppConfigurator? appConfigurator
)
{
appConfigurator = AppConfigurator;
return appConfigurator != null;
}
}
}
28 changes: 8 additions & 20 deletions src/dotnetCampus.Configurations/Core/ConcurrentAppConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace dotnetCampus.Configurations.Core
Expand All @@ -17,12 +18,10 @@ internal ConcurrentAppConfigurator(IConfigurationRepo repo)
_repo = repo ?? throw new ArgumentNullException(nameof(repo));
}

private readonly object _locker = new object();

private readonly IConfigurationRepo _repo;

private readonly Dictionary<Type, Configuration> _configurationDictionary
= new Dictionary<Type, Configuration>();
private readonly ConcurrentDictionary<Type, Configuration> _configurationDictionary
= new ConcurrentDictionary<Type, Configuration>();

/// <summary>
/// 获取 <typeparamref name="TConfiguration"/> 类型的配置项组。
Expand All @@ -31,25 +30,14 @@ private readonly Dictionary<Type, Configuration> _configurationDictionary
/// <returns>配置项组。</returns>
public TConfiguration Of<TConfiguration>() where TConfiguration : Configuration, new()
{
if (_configurationDictionary.TryGetValue(typeof(TConfiguration), out var configuration))
{
return (TConfiguration) configuration;
}

lock (_locker)
return (TConfiguration) _configurationDictionary.GetOrAdd(typeof(TConfiguration), _ =>
{
if (_configurationDictionary.TryGetValue(typeof(TConfiguration), out var lockedConfiguration))
{
return (TConfiguration) lockedConfiguration;
}

configuration = new TConfiguration
return new TConfiguration
{
Repo = _repo
Repo = _repo,
AppConfigurator = this,
};
_configurationDictionary[typeof(TConfiguration)] = configuration;
return (TConfiguration)configuration;
}
});
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<GenerateDocumentationFile>false</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading