diff --git a/samples/StarWars/DictionaryResourcesProvider.cs b/samples/StarWars/DictionaryResourcesProvider.cs index 4c935b5..b29f0b0 100644 --- a/samples/StarWars/DictionaryResourcesProvider.cs +++ b/samples/StarWars/DictionaryResourcesProvider.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Threading; using System.Threading.Tasks; @@ -68,30 +67,33 @@ public DictionaryResourcesProvider() }; } - private IReadOnlyDictionary> _masterDictionary; + private readonly IReadOnlyDictionary> + _masterDictionary; - public Task TryGetResourceAsync( + public Task TryGetResourceAsync( string key, CultureInfo culture, - [NotNullWhen(returnValue: true)] out Resource? res, CancellationToken cancellationToken) { - res = null; + return Task.FromResult(TryGetResource(key, culture)); + } + + private Resource? TryGetResource(string key, CultureInfo culture) + { Language language = ToLanguage(culture); if (!_masterDictionary.ContainsKey(language)) { - return Task.FromResult(false); + return null; } if (!_masterDictionary[language].ContainsKey(key)) { - return Task.FromResult(false); + return null; } string label = _masterDictionary[language][key]; - res = new Resource(key, label); - return Task.FromResult(true); + return new Resource(key, label); } private static Language ToLanguage(CultureInfo culture) @@ -109,4 +111,4 @@ private static Language ToLanguage(CultureInfo culture) } } } -} +} \ No newline at end of file diff --git a/src/HotChocolate.Extensions.Translation.Tests/Resources/ResourcesProviderAdapterTests.cs b/src/HotChocolate.Extensions.Translation.Tests/Resources/ResourcesProviderAdapterTests.cs index 051d7b8..92ffc27 100644 --- a/src/HotChocolate.Extensions.Translation.Tests/Resources/ResourcesProviderAdapterTests.cs +++ b/src/HotChocolate.Extensions.Translation.Tests/Resources/ResourcesProviderAdapterTests.cs @@ -23,8 +23,8 @@ public async Task TryGetTranslationAsString_WhenResourceFound_ShouldReturnResour //Arrange dependencies var provider = new Mock(MockBehavior.Strict); provider - .Setup(p => p.TryGetResourceAsync(key, culture, out resource, It.IsAny())) - .ReturnsAsync(true); + .Setup(p => p.TryGetResourceAsync(key, culture, It.IsAny())) + .ReturnsAsync(resource); var adapter = new ResourcesProviderAdapter(provider.Object, observers: default!); @@ -47,8 +47,9 @@ public async Task TryGetTranslationAsString_WhenResourceNotFound_ShouldReturnFal //Arrange dependencies var provider = new Mock(MockBehavior.Strict); provider - .Setup(p => p.TryGetResourceAsync(key, culture, out resource, It.IsAny())) - .ReturnsAsync(false); + .Setup(p => p.TryGetResourceAsync(key, culture, It.IsAny())) + .ReturnsAsync(resource); + var observer = new DummyObserver(); var adapter = new ResourcesProviderAdapter(provider.Object, observers: new[] { observer }); diff --git a/src/HotChocolate.Extensions.Translation/Resources/IResourcesProvider.cs b/src/HotChocolate.Extensions.Translation/Resources/IResourcesProvider.cs index 3e33956..c0e91db 100644 --- a/src/HotChocolate.Extensions.Translation/Resources/IResourcesProvider.cs +++ b/src/HotChocolate.Extensions.Translation/Resources/IResourcesProvider.cs @@ -1,4 +1,3 @@ -using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Threading; using System.Threading.Tasks; @@ -7,10 +6,9 @@ namespace HotChocolate.Extensions.Translation.Resources { public interface IResourcesProvider { - Task TryGetResourceAsync( + Task TryGetResourceAsync( string key, CultureInfo culture, - [NotNullWhen(returnValue:true)] out Resource? res, CancellationToken cancellationToken); } } diff --git a/src/HotChocolate.Extensions.Translation/Resources/ResourcesProviderAdapter.cs b/src/HotChocolate.Extensions.Translation/Resources/ResourcesProviderAdapter.cs index 5a45314..c668264 100644 --- a/src/HotChocolate.Extensions.Translation/Resources/ResourcesProviderAdapter.cs +++ b/src/HotChocolate.Extensions.Translation/Resources/ResourcesProviderAdapter.cs @@ -24,9 +24,11 @@ public async Task TryGetTranslationAsStringAsync( string fallbackValue, CancellationToken cancellationToken) { - if (await _resourcesProvider - .TryGetResourceAsync(key, culture, out Resource? res, cancellationToken) - .ConfigureAwait(false)) + var res = await _resourcesProvider + .TryGetResourceAsync(key, culture, cancellationToken) + .ConfigureAwait(false); + + if (res is { }) { return res.Value; } @@ -39,4 +41,4 @@ public async Task TryGetTranslationAsStringAsync( return fallbackValue; } } -} +} \ No newline at end of file