diff --git a/Directory.Build.props b/Directory.Build.props index c541d02..2c7c23c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,5 +12,16 @@ git latest enable + Copyright (c) dotnet-campus 2020-$([System.DateTime]::Now.ToString(`yyyy`)) + README.md + + true + + true + snupkg + + + + \ No newline at end of file diff --git a/src/dotnetCampus.Configurations/Configuration.cs b/src/dotnetCampus.Configurations/Configuration.cs index b15fbdc..55b1f18 100644 --- a/src/dotnetCampus.Configurations/Configuration.cs +++ b/src/dotnetCampus.Configurations/Configuration.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using dotnetCampus.Configurations.Core; @@ -271,5 +272,24 @@ protected void ClearValues() /// 获取用于管理应用程序字符串配置项的管理器。 /// internal IConfigurationRepo? Repo { get; set; } + + internal IAppConfigurator? AppConfigurator { get; set; } + + /// + /// 尝试获取 实例。只有配置框架内部创建的配置,才能获取到 实例。 + /// + /// + /// + public bool TryGetAppConfigurator + ( +#if NETCOREAPP3_0_OR_GREATER + [NotNullWhen(true)] +#endif + out IAppConfigurator? appConfigurator + ) + { + appConfigurator = AppConfigurator; + return appConfigurator != null; + } } } diff --git a/src/dotnetCampus.Configurations/Core/ConcurrentAppConfigurator.cs b/src/dotnetCampus.Configurations/Core/ConcurrentAppConfigurator.cs index 600db94..38d0df6 100644 --- a/src/dotnetCampus.Configurations/Core/ConcurrentAppConfigurator.cs +++ b/src/dotnetCampus.Configurations/Core/ConcurrentAppConfigurator.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; namespace dotnetCampus.Configurations.Core @@ -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 _configurationDictionary - = new Dictionary(); + private readonly ConcurrentDictionary _configurationDictionary + = new ConcurrentDictionary(); /// /// 获取 类型的配置项组。 @@ -31,25 +30,14 @@ private readonly Dictionary _configurationDictionary /// 配置项组。 public TConfiguration Of() 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; - } + }); } /// diff --git a/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj index f065bd2..da63d21 100644 --- a/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj +++ b/tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests/dotnetCampus.Configurations.MicrosoftExtensionsConfiguration.Tests.csproj @@ -5,6 +5,7 @@ false false + true diff --git a/tests/dotnetCampus.Configurations.Tests/dotnetCampus.Configurations.Tests.csproj b/tests/dotnetCampus.Configurations.Tests/dotnetCampus.Configurations.Tests.csproj index 99e64d0..59042d4 100644 --- a/tests/dotnetCampus.Configurations.Tests/dotnetCampus.Configurations.Tests.csproj +++ b/tests/dotnetCampus.Configurations.Tests/dotnetCampus.Configurations.Tests.csproj @@ -5,6 +5,7 @@ latest enable false + true