From f9618dcf14612917199a34f734993c9f5c168974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 8 Feb 2018 14:02:28 +0300 Subject: [PATCH] Refactored setting system. --- .../DefaultStoreSettingValueProvider.cs | 29 ----------------- .../GlobalSettingManagerExtensions.cs | 24 ++++++++++++++ .../Settings/GlobalSettingValueProvider.cs | 31 +++++++++++++++++++ .../Volo/Abp/Settings/ISettingManager.cs | 2 -- .../Volo/Abp/Settings/SettingManager.cs | 8 ----- .../Volo/Abp/Settings/SettingOptions.cs | 2 +- .../Settings/AbpIdentityTestDataBuilder.cs | 4 +-- .../Settings/SettingManager_Basic_Tests.cs | 6 ++-- 8 files changed, 61 insertions(+), 45 deletions(-) delete mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs deleted file mode 100644 index 86166f1eb7..0000000000 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Threading.Tasks; - -namespace Volo.Abp.Settings -{ - public class DefaultStoreSettingValueProvider : SettingValueProvider - { - public override string EntityType => null; - - public DefaultStoreSettingValueProvider(ISettingStore settingStore) - : base(settingStore) - { - } - - public override Task GetOrNullAsync(SettingDefinition setting, string entityId) - { - return SettingStore.GetOrNullAsync(setting.Name, null, null); - } - - public override Task SetAsync(SettingDefinition setting, string value, string entityId) - { - return SettingStore.SetAsync(setting.Name, value, null, null); - } - - public override Task ClearAsync(SettingDefinition setting, string entityId) - { - return SettingStore.DeleteAsync(setting.Name, null, null); - } - } -} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs new file mode 100644 index 0000000000..ce22151a38 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace Volo.Abp.Settings +{ + public static class GlobalSettingManagerExtensions + { + public static Task GetOrNullGlobalAsync(this ISettingManager settingManager, [NotNull] string name, bool fallback = true) + { + return settingManager.GetOrNullAsync(name, GlobalSettingValueProvider.DefaultEntityType, null, fallback); + } + + public static Task> GetAllGlobalAsync(this ISettingManager settingManager, bool fallback = true) + { + return settingManager.GetAllAsync(GlobalSettingValueProvider.DefaultEntityType, null, fallback); + } + + public static Task SetGlobalAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value) + { + return settingManager.SetAsync(name, value, GlobalSettingValueProvider.DefaultEntityType, null); + } + } +} diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs new file mode 100644 index 0000000000..c5c628cffd --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs @@ -0,0 +1,31 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Settings +{ + public class GlobalSettingValueProvider : SettingValueProvider + { + public const string DefaultEntityType = "Global"; + + public override string EntityType => DefaultEntityType; + + public GlobalSettingValueProvider(ISettingStore settingStore) + : base(settingStore) + { + } + + public override Task GetOrNullAsync(SettingDefinition setting, string entityId) + { + return SettingStore.GetOrNullAsync(setting.Name, EntityType, null); + } + + public override Task SetAsync(SettingDefinition setting, string value, string entityId) + { + return SettingStore.SetAsync(setting.Name, value, EntityType, null); + } + + public override Task ClearAsync(SettingDefinition setting, string entityId) + { + return SettingStore.DeleteAsync(setting.Name, EntityType, null); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs index 4c3e64a3b3..4d60e5a61c 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -14,8 +14,6 @@ namespace Volo.Abp.Settings Task> GetAllAsync([NotNull] string entityType, [CanBeNull] string entityId, bool fallback = true); - Task SetAsync([NotNull] string name, [CanBeNull] string value, bool forceToSet = false); - Task SetAsync([NotNull] string name, [CanBeNull] string value, [NotNull] string entityType, [CanBeNull] string entityId, bool forceToSet = false); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index 4795c086cb..b90666bfe3 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Collections.Immutable; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; @@ -132,13 +131,6 @@ namespace Volo.Abp.Settings return settingValues.Values.ToList(); } - public virtual Task SetAsync(string name, string value, bool forceToSet = false) - { - Check.NotNull(name, nameof(name)); - - return SetInternalAsync(name, value, null, null, forceToSet); - } - public virtual Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) { Check.NotNull(name, nameof(name)); diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs index cfc98e58e6..66b4fa6c05 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs @@ -14,7 +14,7 @@ namespace Volo.Abp.Settings ValueProviders = new TypeList { typeof(DefaultValueSettingValueProvider), - typeof(DefaultStoreSettingValueProvider) + typeof(GlobalSettingValueProvider) }; } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs index 2cfc147126..06b54bbbd3 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs @@ -21,9 +21,9 @@ namespace Volo.Abp.Settings public void Build() { - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting1", "42")); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting1", "42", GlobalSettingValueProvider.DefaultEntityType)); - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "default-store-value")); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "default-store-value", GlobalSettingValueProvider.DefaultEntityType)); _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user1-store-value", UserSettingValueProvider.DefaultEntityType, User1Id.ToString())); _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user2-store-value", UserSettingValueProvider.DefaultEntityType, User2Id.ToString())); } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Basic_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Basic_Tests.cs index 184fd3bb80..7ac6205346 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Basic_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Basic_Tests.cs @@ -44,12 +44,12 @@ namespace Volo.Abp.Settings } [Fact] - public async Task Should_Set_Value() + public async Task Should_Set_Global_Value() { - await _settingManager.SetAsync("MySetting1", "43"); + await _settingManager.SetGlobalAsync("MySetting1", "43"); + (await _settingManager.GetOrNullGlobalAsync("MySetting1")).ShouldBe("43"); (await _settingManager.GetOrNullAsync("MySetting1")).ShouldBe("43"); - //TODO: Also check database! } } }