From bda90d64979c966a2e211b2633ec6d01be76c147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 10:39:53 +0300 Subject: [PATCH] Implemented most basic setting manager to get a setting's value. --- .../Volo.Abp.Settings.Domain.csproj | 1 - .../Abp/Settings/AbpSettingsDomainModule.cs | 2 -- .../Volo/Abp/Settings/Setting.cs | 9 ++---- .../Volo/Abp/Settings/SettingStore.cs | 2 +- .../CurrentTenantSettingContributor.cs | 21 ------------- .../Abp/Settings/DefaultSettingContributor.cs | 9 +++--- .../Volo/Abp/Settings/ISettingContributor.cs | 2 +- .../Volo/Abp/Settings/SettingManager.cs | 31 +++++++++++++++---- .../Volo/Abp/Settings/SettingOptions.cs | 17 ++++++++++ .../Settings/AbpIdentityTestDataBuilder.cs | 1 - .../Volo/Abp/Settings/SettingManager_Tests.cs | 9 ++++-- 11 files changed, 57 insertions(+), 47 deletions(-) delete mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs diff --git a/src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj b/src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj index 2339d7a04c..425916670d 100644 --- a/src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj +++ b/src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj @@ -15,7 +15,6 @@ - diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs index 2c1d5266ac..a6edc3f87a 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs @@ -1,12 +1,10 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; -using Volo.Abp.MultiTenancy; namespace Volo.Abp.Settings { [DependsOn(typeof(AbpSettingsModule))] [DependsOn(typeof(AbpDddModule))] - [DependsOn(typeof(AbpMultiTenancyAbstractionsModule))] public class AbpSettingsDomainModule : AbpModule { public override void ConfigureServices(IServiceCollection services) diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs index 5db42e473c..c2a8318fc6 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs @@ -1,14 +1,11 @@ using System; using JetBrains.Annotations; using Volo.Abp.Domain.Entities; -using Volo.Abp.MultiTenancy; namespace Volo.Abp.Settings { - public class Setting : Entity, IMultiTenant + public class Setting : Entity { - public virtual Guid? TenantId { get; protected set; } - [NotNull] public virtual string Name { get; protected set; } @@ -31,8 +28,7 @@ namespace Volo.Abp.Settings [NotNull] string name, [NotNull] string value, [CanBeNull] string entityType = null, - [CanBeNull] string entityId = null, - Guid? tenantId = null) + [CanBeNull] string entityId = null) { Check.NotNull(name, nameof(name)); Check.NotNull(value, nameof(value)); @@ -42,7 +38,6 @@ namespace Volo.Abp.Settings Value = value; EntityType = entityType; EntityId = entityId; - TenantId = tenantId; } } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs index 43cf73acce..5131a71116 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs @@ -25,7 +25,7 @@ namespace Volo.Abp.Settings var setting = await _settingRepository.FindAsync(name, entityType, entityId); if (setting == null) { - setting = new Setting(GuidGenerator.Create(), name, value, entityType, entityId, CurrentTenant.Id); + setting = new Setting(GuidGenerator.Create(), name, value, entityType, entityId); await _settingRepository.InsertAsync(setting); } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs deleted file mode 100644 index efe95a552b..0000000000 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; - -namespace Volo.Abp.Settings -{ - public class CurrentTenantSettingContributor : ISettingContributor - { - private readonly ISettingStore _settingStore; - - public CurrentTenantSettingContributor(ISettingStore settingStore) - { - _settingStore = settingStore; - } - - public async Task GetOrNull(string name) - { - //Optimization: Get all Tenant-42 settings and cache it! - var tenantId = 42; //Get from current tenant - return await _settingStore.GetOrNullAsync(name, "Tenant", tenantId.ToString()); - } - } -} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs index ce141b7ebd..4fc8ea3d3f 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs @@ -1,8 +1,9 @@ using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.Settings { - public class DefaultSettingContributor : ISettingContributor + public class DefaultSettingContributor : ISettingContributor, ISingletonDependency { private readonly ISettingStore _settingStore; @@ -10,10 +11,10 @@ namespace Volo.Abp.Settings { _settingStore = settingStore; } - - public async Task GetOrNull(string name) + + public async Task GetOrNull(string name, string entityType, string entityId, bool fallback = true) { - //Optimization: Get all settings and cache it! + //TODO: Optimization: Get all settings and cache it! return await _settingStore.GetOrNullAsync(name, null, null); } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs index 27fea72ff8..6bddcf4794 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs @@ -4,6 +4,6 @@ namespace Volo.Abp.Settings { public interface ISettingContributor { - Task GetOrNull(string name); + Task GetOrNull(string name, string entityType, string entityId, bool fallback = true); } } \ 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 2f1d1ffcaf..8fc98c8245 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -1,11 +1,32 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Settings { - public class SettingManager : ISettingManager, ITransientDependency + public class SettingManager : ISettingManager, ISingletonDependency { + protected Lazy> Contributors { get; } + + protected SettingOptions Options { get; } + + public SettingManager(IOptions options, IServiceProvider serviceProvider) + { + Options = options.Value; + + Contributors = new Lazy>( + () => Options + .Contributors + .Select(c => serviceProvider.GetRequiredService(c) as ISettingContributor) + .ToList(), + true + ); + } + public Task GetOrNullAsync(string name) { return GetOrNullAsync(name, null, null); @@ -13,11 +34,9 @@ namespace Volo.Abp.Settings public Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) { - var contributors = new List(); - - foreach (var contributor in contributors) + foreach (var contributor in Contributors.Value) { - var value = contributor.GetOrNull(name); + var value = contributor.GetOrNull(name, entityType, entityId, fallback); if (value != null) { return value; diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs new file mode 100644 index 0000000000..595b76aa64 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs @@ -0,0 +1,17 @@ +using Volo.Abp.Collections; + +namespace Volo.Abp.Settings +{ + public class SettingOptions + { + public ITypeList Contributors { get; } + + public SettingOptions() + { + Contributors = new TypeList + { + typeof(DefaultSettingContributor) + }; + } + } +} 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 9201cfa777..707eb4c7d0 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs @@ -17,7 +17,6 @@ namespace Volo.Abp.Settings public void Build() { _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting1", "42")); - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "55")); } } } \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs index 0c34713b69..75f76a6180 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs @@ -1,4 +1,6 @@ -using Xunit; +using System.Threading.Tasks; +using Shouldly; +using Xunit; namespace Volo.Abp.Settings { @@ -12,9 +14,10 @@ namespace Volo.Abp.Settings } [Fact] - public void Test1() + public async Task Test1() { - + var value = await _settingManager.GetOrNullAsync("MySetting1"); + value.ShouldBe("42"); } } }