From f8ff00868f49de56a1d639f10607c02a296a3f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 6 Feb 2018 15:29:39 +0300 Subject: [PATCH 01/21] Added initial code for setting system --- Volo.Abp.sln | 23 ++++++++- .../EntityFrameworkCore/AbpDeskDbContext.cs | 2 +- src/Volo.Abp.Ddd/Volo/Abp/AbpServiceBase.cs | 3 ++ ...IdentityServerEntityFrameworkCoreModule.cs | 7 +++ .../Volo/Abp/MultiTenancy/CurrentTenant.cs | 2 +- ...tityConsts.cs => AbpMultiTenancyConsts.cs} | 0 .../Volo.Abp.Settings.Domain.csproj | 22 +++++++++ .../Abp/Settings/AbpSettingsDomainModule.cs | 17 +++++++ .../Volo/Abp/Settings/ISettingRepository.cs | 14 ++++++ .../Volo/Abp/Settings/Setting.cs | 48 ++++++++++++++++++ .../Volo/Abp/Settings/SettingStore.cs | 42 ++++++++++++++++ ...lo.Abp.Settings.EntityFrameworkCore.csproj | 21 ++++++++ .../AbpSettingsDbContext.cs | 16 ++++++ .../AbpSettingsEntityFrameworkCoreModule.cs | 22 +++++++++ .../EfCoreSettingRepository.cs | 28 +++++++++++ .../IAbpSettingsDbContext.cs | 10 ++++ .../Volo.Abp.Settings.csproj | 20 ++++++++ .../Volo/Abp/Settings/AbpSettingsModule.cs | 13 +++++ .../CurrentTenantSettingContributor.cs | 21 ++++++++ .../Abp/Settings/DefaultSettingContributor.cs | 20 ++++++++ .../Volo/Abp/Settings/ISettingContributor.cs | 9 ++++ .../Volo/Abp/Settings/ISettingManager.cs | 20 ++++++++ .../Volo/Abp/Settings/ISettingStore.cs | 15 ++++++ .../Volo/Abp/Settings/SettingManager.cs | 49 +++++++++++++++++++ .../Volo/Abp/Settings/SettingValue.cs | 22 +++++++++ 25 files changed, 463 insertions(+), 3 deletions(-) rename src/Volo.Abp.MultiTenancy.Domain/Volo/Abp/MultiTenancy/{AbpIdentityConsts.cs => AbpMultiTenancyConsts.cs} (100%) create mode 100644 src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj create mode 100644 src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs create mode 100644 src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs create mode 100644 src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs create mode 100644 src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs create mode 100644 src/Volo.Abp.Settings.EntityFrameworkCore/Volo.Abp.Settings.EntityFrameworkCore.csproj create mode 100644 src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs create mode 100644 src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs create mode 100644 src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs create mode 100644 src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/IAbpSettingsDbContext.cs create mode 100644 src/Volo.Abp.Settings/Volo.Abp.Settings.csproj create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingsModule.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValue.cs diff --git a/Volo.Abp.sln b/Volo.Abp.sln index 259de7b7cf..f94fa81e50 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -244,7 +244,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.MultiTenancy.Appli EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.MultiTenancy.Web", "src\Volo.Abp.MultiTenancy.Web\Volo.Abp.MultiTenancy.Web.csproj", "{0808036D-065B-4525-BF30-CBCEA062F8E9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.MultiTenancy.HttpApi", "src\Volo.Abp.MultiTenancy.HttpApi\Volo.Abp.MultiTenancy.HttpApi.csproj", "{06423FAC-67E2-4B7C-B29F-39578F460C4F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.MultiTenancy.HttpApi", "src\Volo.Abp.MultiTenancy.HttpApi\Volo.Abp.MultiTenancy.HttpApi.csproj", "{06423FAC-67E2-4B7C-B29F-39578F460C4F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Settings", "src\Volo.Abp.Settings\Volo.Abp.Settings.csproj", "{CB6FD800-B6C5-4C2A-8920-B8A29C74AEF6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Settings.EntityFrameworkCore", "src\Volo.Abp.Settings.EntityFrameworkCore\Volo.Abp.Settings.EntityFrameworkCore.csproj", "{1DCD9600-215A-4A94-AEFD-577568D62633}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Settings.Domain", "src\Volo.Abp.Settings.Domain\Volo.Abp.Settings.Domain.csproj", "{837BC537-3AEC-4F6D-9D64-9704F64D0E1E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -640,6 +646,18 @@ Global {06423FAC-67E2-4B7C-B29F-39578F460C4F}.Debug|Any CPU.Build.0 = Debug|Any CPU {06423FAC-67E2-4B7C-B29F-39578F460C4F}.Release|Any CPU.ActiveCfg = Release|Any CPU {06423FAC-67E2-4B7C-B29F-39578F460C4F}.Release|Any CPU.Build.0 = Release|Any CPU + {CB6FD800-B6C5-4C2A-8920-B8A29C74AEF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB6FD800-B6C5-4C2A-8920-B8A29C74AEF6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB6FD800-B6C5-4C2A-8920-B8A29C74AEF6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB6FD800-B6C5-4C2A-8920-B8A29C74AEF6}.Release|Any CPU.Build.0 = Release|Any CPU + {1DCD9600-215A-4A94-AEFD-577568D62633}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DCD9600-215A-4A94-AEFD-577568D62633}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DCD9600-215A-4A94-AEFD-577568D62633}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DCD9600-215A-4A94-AEFD-577568D62633}.Release|Any CPU.Build.0 = Release|Any CPU + {837BC537-3AEC-4F6D-9D64-9704F64D0E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {837BC537-3AEC-4F6D-9D64-9704F64D0E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {837BC537-3AEC-4F6D-9D64-9704F64D0E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {837BC537-3AEC-4F6D-9D64-9704F64D0E1E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -759,6 +777,9 @@ Global {B65973F1-9EE0-4914-B6F2-D88F753E1D21} = {3CE22CAC-4B27-4EEC-A35E-C01219ED6E99} {0808036D-065B-4525-BF30-CBCEA062F8E9} = {CA154803-3589-47B3-B7CB-B18F94FE1EB6} {06423FAC-67E2-4B7C-B29F-39578F460C4F} = {CA154803-3589-47B3-B7CB-B18F94FE1EB6} + {CB6FD800-B6C5-4C2A-8920-B8A29C74AEF6} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} + {1DCD9600-215A-4A94-AEFD-577568D62633} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} + {837BC537-3AEC-4F6D-9D64-9704F64D0E1E} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5} diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs index e53b60966f..8a583e93f2 100644 --- a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs +++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskDbContext.cs @@ -26,7 +26,7 @@ namespace AbpDesk.EntityFrameworkCore { base.OnModelCreating(modelBuilder); - modelBuilder.ConfigureMultiTenancy(MultiTenancyDbContext.TablePrefix, MultiTenancyDbContext.Schema); + modelBuilder.ConfigureMultiTenancy(); //Use different classes to map each entity type, as a better practice? modelBuilder.Entity(b => diff --git a/src/Volo.Abp.Ddd/Volo/Abp/AbpServiceBase.cs b/src/Volo.Abp.Ddd/Volo/Abp/AbpServiceBase.cs index 7911c7a366..01b7aecaf1 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/AbpServiceBase.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/AbpServiceBase.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Guids; +using Volo.Abp.MultiTenancy; using Volo.Abp.ObjectMapping; using Volo.Abp.Uow; @@ -17,6 +18,8 @@ namespace Volo.Abp public ILoggerFactory LoggerFactory { get; set; } + public ICurrentTenant CurrentTenant { get; set; } + protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; protected ILogger Logger => _lazyLogger.Value; diff --git a/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs b/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs index e7c787a206..9594451956 100644 --- a/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs +++ b/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerEntityFrameworkCoreModule.cs @@ -1,6 +1,9 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.IdentityServer.ApiResources; using Volo.Abp.IdentityServer.Clients; +using Volo.Abp.IdentityServer.Grants; +using Volo.Abp.IdentityServer.IdentityResources; using Volo.Abp.Modularity; namespace Volo.Abp.IdentityServer.EntityFrameworkCore @@ -14,7 +17,11 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore services.AddAbpDbContext(options => { options.AddDefaultRepositories(); + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); }); services.AddAssemblyOf(); diff --git a/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/CurrentTenant.cs b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/CurrentTenant.cs index 1cee215eea..743355d415 100644 --- a/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/CurrentTenant.cs +++ b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/CurrentTenant.cs @@ -21,7 +21,7 @@ namespace Volo.Abp.MultiTenancy return SetCurrent(id); } - public IDisposable Clear() + public IDisposable Clear() //TODO: Remove { return Change(null); } diff --git a/src/Volo.Abp.MultiTenancy.Domain/Volo/Abp/MultiTenancy/AbpIdentityConsts.cs b/src/Volo.Abp.MultiTenancy.Domain/Volo/Abp/MultiTenancy/AbpMultiTenancyConsts.cs similarity index 100% rename from src/Volo.Abp.MultiTenancy.Domain/Volo/Abp/MultiTenancy/AbpIdentityConsts.cs rename to src/Volo.Abp.MultiTenancy.Domain/Volo/Abp/MultiTenancy/AbpMultiTenancyConsts.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 new file mode 100644 index 0000000000..2339d7a04c --- /dev/null +++ b/src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj @@ -0,0 +1,22 @@ + + + + + + netstandard2.0 + Volo.Abp.Settings.Domain + Volo.Abp.Settings.Domain + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + + diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs new file mode 100644 index 0000000000..2c1d5266ac --- /dev/null +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs @@ -0,0 +1,17 @@ +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) + { + services.AddAssemblyOf(); + } + } +} diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs new file mode 100644 index 0000000000..7c7164204a --- /dev/null +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; + +namespace Volo.Abp.Settings +{ + public interface ISettingRepository : IBasicRepository + { + Task FindAsync(string name, string entityType, string entityId); + + Task> GetListAsync(string entityType, string entityId); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs new file mode 100644 index 0000000000..5db42e473c --- /dev/null +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs @@ -0,0 +1,48 @@ +using System; +using JetBrains.Annotations; +using Volo.Abp.Domain.Entities; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.Settings +{ + public class Setting : Entity, IMultiTenant + { + public virtual Guid? TenantId { get; protected set; } + + [NotNull] + public virtual string Name { get; protected set; } + + [NotNull] + public virtual string Value { get; internal set; } + + [CanBeNull] + public virtual string EntityType { get; protected set; } + + [CanBeNull] + public virtual string EntityId { get; protected set; } + + protected Setting() + { + + } + + public Setting( + Guid id, + [NotNull] string name, + [NotNull] string value, + [CanBeNull] string entityType = null, + [CanBeNull] string entityId = null, + Guid? tenantId = null) + { + Check.NotNull(name, nameof(name)); + Check.NotNull(value, nameof(value)); + + Id = id; + Name = name; + 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 new file mode 100644 index 0000000000..43cf73acce --- /dev/null +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Settings +{ + public class SettingStore : AbpServiceBase, ISettingStore, ITransientDependency + { + private readonly ISettingRepository _settingRepository; + + public SettingStore(ISettingRepository settingRepository) + { + _settingRepository = settingRepository; + } + + public async Task GetOrNullAsync(string name, string entityType, string entityId) + { + var setting = await _settingRepository.FindAsync(name, entityType, entityId); + return setting?.Value; + } + + public async Task SetAsync(string name, string value, string entityType, string entityId) + { + var setting = await _settingRepository.FindAsync(name, entityType, entityId); + if (setting == null) + { + setting = new Setting(GuidGenerator.Create(), name, value, entityType, entityId, CurrentTenant.Id); + await _settingRepository.InsertAsync(setting); + } + + setting.Value = value; + await _settingRepository.UpdateAsync(setting); + } + + public async Task> GetListAsync(string entityType, string entityId) + { + var setting = await _settingRepository.GetListAsync(entityType, entityId); + return setting.Select(s => new SettingValue(s.Name, s.Value)).ToList(); + } + } +} diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo.Abp.Settings.EntityFrameworkCore.csproj b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo.Abp.Settings.EntityFrameworkCore.csproj new file mode 100644 index 0000000000..8ca5ce8156 --- /dev/null +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo.Abp.Settings.EntityFrameworkCore.csproj @@ -0,0 +1,21 @@ + + + + + + netstandard2.0 + Volo.Abp.Settings.EntityFrameworkCore + Volo.Abp.Settings.EntityFrameworkCore + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + + diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs new file mode 100644 index 0000000000..19150e366b --- /dev/null +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.Settings.EntityFrameworkCore +{ + public class AbpSettingsDbContext : AbpDbContext, IAbpSettingsDbContext + { + public DbSet Settings { get; set; } + + public AbpSettingsDbContext(DbContextOptions options) + : base(options) + { + + } + } +} diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs new file mode 100644 index 0000000000..8b906f1579 --- /dev/null +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; + +namespace Volo.Abp.Settings.EntityFrameworkCore +{ + [DependsOn(typeof(AbpSettingsDomainModule))] + [DependsOn(typeof(AbpSettingsEntityFrameworkCoreModule))] + public class AbpSettingsEntityFrameworkCoreModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAbpDbContext(options => + { + options.AddDefaultRepositories(); + + options.AddRepository(); + }); + + services.AddAssemblyOf(); + } + } +} diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs new file mode 100644 index 0000000000..2073f36269 --- /dev/null +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.Settings.EntityFrameworkCore +{ + public class EfCoreSettingRepository : EfCoreRepository, ISettingRepository + { + public EfCoreSettingRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + } + + public async Task FindAsync(string name, string entityType, string entityId) + { + return await DbSet.FirstOrDefaultAsync(s => s.Name == name && s.EntityType == entityType && s.EntityId == entityId); + } + + public async Task> GetListAsync(string entityType, string entityId) + { + return await DbSet.Where(s => s.EntityType == entityType && s.EntityId == entityId).ToListAsync(); + } + } +} diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/IAbpSettingsDbContext.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/IAbpSettingsDbContext.cs new file mode 100644 index 0000000000..1ef5e93867 --- /dev/null +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/IAbpSettingsDbContext.cs @@ -0,0 +1,10 @@ +using Microsoft.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; + +namespace Volo.Abp.Settings.EntityFrameworkCore +{ + public interface IAbpSettingsDbContext : IEfCoreDbContext + { + DbSet Settings { get; set; } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo.Abp.Settings.csproj b/src/Volo.Abp.Settings/Volo.Abp.Settings.csproj new file mode 100644 index 0000000000..a26679d049 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo.Abp.Settings.csproj @@ -0,0 +1,20 @@ + + + + + + netstandard2.0 + Volo.Abp.Settings + Volo.Abp.Settings + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingsModule.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingsModule.cs new file mode 100644 index 0000000000..0018d495a4 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/AbpSettingsModule.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; + +namespace Volo.Abp.Settings +{ + public class AbpSettingsModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs new file mode 100644 index 0000000000..efe95a552b --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/CurrentTenantSettingContributor.cs @@ -0,0 +1,21 @@ +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 new file mode 100644 index 0000000000..ce141b7ebd --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Settings +{ + public class DefaultSettingContributor : ISettingContributor + { + private readonly ISettingStore _settingStore; + + public DefaultSettingContributor(ISettingStore settingStore) + { + _settingStore = settingStore; + } + + public async Task GetOrNull(string name) + { + //Optimization: Get all settings and cache it! + return await _settingStore.GetOrNullAsync(name, null, null); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs new file mode 100644 index 0000000000..27fea72ff8 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Settings +{ + public interface ISettingContributor + { + Task GetOrNull(string name); + } +} \ 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 new file mode 100644 index 0000000000..b768648e9d --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Volo.Abp.Settings +{ + public interface ISettingManager + { + Task GetOrNullAsync(string name); + + Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true); + + Task> GetAllAsync(); + + Task> GetAllAsync(string entityType, string entityId, bool fallback = true); + + Task SetAsync(string name, string value); + + Task SetAsync(string name, string value, string entityType, string entityId); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs new file mode 100644 index 0000000000..1c4924b1e3 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace Volo.Abp.Settings +{ + public interface ISettingStore + { + Task GetOrNullAsync([NotNull] string name, [CanBeNull] string entityType, [CanBeNull] string entityId); + + Task SetAsync([NotNull] string name, [NotNull] string value, [CanBeNull] string entityType, [CanBeNull] string entityId); + + Task> GetListAsync([CanBeNull] string entityType, [CanBeNull] string entityId); + } +} diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs new file mode 100644 index 0000000000..da9a9c7b83 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Volo.Abp.Settings +{ + public class SettingManager : ISettingManager + { + public Task GetOrNullAsync(string name) + { + return GetOrNullAsync(name, null, null); + } + + public Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) + { + var contributors = new List(); + + foreach (var contributor in contributors) + { + var value = contributor.GetOrNull(name); + if (value != null) + { + return value; + } + } + + return null; + } + + public Task> GetAllAsync() + { + throw new System.NotImplementedException(); + } + + public Task> GetAllAsync(string entityType, string entityId, bool fallback = true) + { + throw new System.NotImplementedException(); + } + + public Task SetAsync(string name, string value) + { + throw new System.NotImplementedException(); + } + + public Task SetAsync(string name, string value, string entityType, string entityId) + { + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValue.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValue.cs new file mode 100644 index 0000000000..99b2de33f1 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValue.cs @@ -0,0 +1,22 @@ +namespace Volo.Abp.Settings +{ + public class SettingValue : NameValue + { + /// + /// Creates a new . + /// + public SettingValue() + { + + } + + /// + /// Creates a new . + /// + public SettingValue(string name, string value) + { + Name = name; + Value = value; + } + } +} \ No newline at end of file From 284ee092a5acf920a2d3b2536def7a69509d61ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 6 Feb 2018 17:42:04 +0300 Subject: [PATCH 02/21] Added unit test project for settings. --- Volo.Abp.sln | 7 +++ .../AbpSettingsEntityFrameworkCoreModule.cs | 3 +- .../Volo/Abp/Settings/SettingManager.cs | 3 +- .../Volo.Abp.MultiTenancy.Tests.csproj | 4 -- .../Volo.Abp.Settings.Tests.csproj | 25 +++++++++ .../AbpIdentityApplicationTestBase.cs | 12 +++++ .../AbpIdentityApplicationTestModule.cs | 52 +++++++++++++++++++ .../Settings/AbpIdentityTestDataBuilder.cs | 23 ++++++++ .../Volo/Abp/Settings/SettingManager_Tests.cs | 20 +++++++ 9 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 test/Volo.Abp.Settings.Tests/Volo.Abp.Settings.Tests.csproj create mode 100644 test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs create mode 100644 test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs create mode 100644 test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs create mode 100644 test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs diff --git a/Volo.Abp.sln b/Volo.Abp.sln index f94fa81e50..2aebb01576 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -252,6 +252,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Settings.EntityFra EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Settings.Domain", "src\Volo.Abp.Settings.Domain\Volo.Abp.Settings.Domain.csproj", "{837BC537-3AEC-4F6D-9D64-9704F64D0E1E}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Settings.Tests", "test\Volo.Abp.Settings.Tests\Volo.Abp.Settings.Tests.csproj", "{61422EC8-8FFB-48EA-B4B5-527E29077930}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -658,6 +660,10 @@ Global {837BC537-3AEC-4F6D-9D64-9704F64D0E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU {837BC537-3AEC-4F6D-9D64-9704F64D0E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU {837BC537-3AEC-4F6D-9D64-9704F64D0E1E}.Release|Any CPU.Build.0 = Release|Any CPU + {61422EC8-8FFB-48EA-B4B5-527E29077930}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {61422EC8-8FFB-48EA-B4B5-527E29077930}.Debug|Any CPU.Build.0 = Debug|Any CPU + {61422EC8-8FFB-48EA-B4B5-527E29077930}.Release|Any CPU.ActiveCfg = Release|Any CPU + {61422EC8-8FFB-48EA-B4B5-527E29077930}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -780,6 +786,7 @@ Global {CB6FD800-B6C5-4C2A-8920-B8A29C74AEF6} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {1DCD9600-215A-4A94-AEFD-577568D62633} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {837BC537-3AEC-4F6D-9D64-9704F64D0E1E} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} + {61422EC8-8FFB-48EA-B4B5-527E29077930} = {37087D1B-3693-4E96-983D-A69F210BDE53} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5} diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs index 8b906f1579..013c32d16f 100644 --- a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsEntityFrameworkCoreModule.cs @@ -1,10 +1,11 @@ using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.EntityFrameworkCore; using Volo.Abp.Modularity; namespace Volo.Abp.Settings.EntityFrameworkCore { [DependsOn(typeof(AbpSettingsDomainModule))] - [DependsOn(typeof(AbpSettingsEntityFrameworkCoreModule))] + [DependsOn(typeof(AbpEntityFrameworkCoreModule))] public class AbpSettingsEntityFrameworkCoreModule : AbpModule { public override void ConfigureServices(IServiceCollection services) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index da9a9c7b83..2f1d1ffcaf 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -1,9 +1,10 @@ using System.Collections.Generic; using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.Settings { - public class SettingManager : ISettingManager + public class SettingManager : ISettingManager, ITransientDependency { public Task GetOrNullAsync(string name) { diff --git a/test/Volo.Abp.MultiTenancy.Tests/Volo.Abp.MultiTenancy.Tests.csproj b/test/Volo.Abp.MultiTenancy.Tests/Volo.Abp.MultiTenancy.Tests.csproj index a76a44b67d..8967428388 100644 --- a/test/Volo.Abp.MultiTenancy.Tests/Volo.Abp.MultiTenancy.Tests.csproj +++ b/test/Volo.Abp.MultiTenancy.Tests/Volo.Abp.MultiTenancy.Tests.csproj @@ -19,9 +19,5 @@ - - - - diff --git a/test/Volo.Abp.Settings.Tests/Volo.Abp.Settings.Tests.csproj b/test/Volo.Abp.Settings.Tests/Volo.Abp.Settings.Tests.csproj new file mode 100644 index 0000000000..df86c0a460 --- /dev/null +++ b/test/Volo.Abp.Settings.Tests/Volo.Abp.Settings.Tests.csproj @@ -0,0 +1,25 @@ + + + + netcoreapp2.0 + Volo.Abp.MultiTenancy.Tests + Volo.Abp.MultiTenancy.Tests + true + false + false + false + + + + + + + + + + + + + + + diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs new file mode 100644 index 0000000000..4f2f07fcfb --- /dev/null +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs @@ -0,0 +1,12 @@ +using Volo.Abp.TestBase; + +namespace Volo.Abp.Settings +{ + public class AbpSettingsTestBase : AbpIntegratedTest + { + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) + { + options.UseAutofac(); + } + } +} diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs new file mode 100644 index 0000000000..7015bda9cc --- /dev/null +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs @@ -0,0 +1,52 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Modularity; +using Volo.Abp.Settings.EntityFrameworkCore; +using Volo.Abp.Uow; + +namespace Volo.Abp.Settings +{ + [DependsOn(typeof(AbpSettingsEntityFrameworkCoreModule), typeof(AbpAutofacModule))] + public class AbpSettingsTestModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + + services.AddEntityFrameworkInMemoryDatabase(); + + var databaseName = Guid.NewGuid().ToString(); + + services.Configure(options => + { + options.Configure(context => + { + context.DbContextOptions.UseInMemoryDatabase(databaseName); + }); + }); + + services.Configure(options => + { + options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; //EF in-memory database does not support transactions + }); + } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + SeedTestData(context); + } + + private static void SeedTestData(ApplicationInitializationContext context) + { + using (var scope = context.ServiceProvider.CreateScope()) + { + scope.ServiceProvider + .GetRequiredService() + .Build(); + } + } + } +} diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs new file mode 100644 index 0000000000..9201cfa777 --- /dev/null +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs @@ -0,0 +1,23 @@ +using Volo.Abp.DependencyInjection; +using Volo.Abp.Guids; + +namespace Volo.Abp.Settings +{ + public class AbpIdentityTestDataBuilder : ITransientDependency + { + private readonly ISettingRepository _settingRepository; + private readonly IGuidGenerator _guidGenerator; + + public AbpIdentityTestDataBuilder(ISettingRepository settingRepository, IGuidGenerator guidGenerator) + { + _settingRepository = settingRepository; + _guidGenerator = guidGenerator; + } + + 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 new file mode 100644 index 0000000000..0c34713b69 --- /dev/null +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs @@ -0,0 +1,20 @@ +using Xunit; + +namespace Volo.Abp.Settings +{ + public class SettingManager_Tests : AbpSettingsTestBase + { + private readonly ISettingManager _settingManager; + + public SettingManager_Tests() + { + _settingManager = GetRequiredService(); + } + + [Fact] + public void Test1() + { + + } + } +} 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 03/21] 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"); } } } From 68b88ff6a899cc26c7c38957a4140911e7ba2cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 11:34:48 +0300 Subject: [PATCH 04/21] Added initial setting definition manager. --- .../Abp/Settings/DefaultSettingContributor.cs | 2 +- .../Volo/Abp/Settings/ISettingContributor.cs | 2 +- .../Abp/Settings/ISettingDefinitionContext.cs | 9 +++ .../Abp/Settings/ISettingDefinitionManager.cs | 7 ++ .../Volo/Abp/Settings/ISettingProvider.cs | 7 ++ .../Volo/Abp/Settings/SettingDefinition.cs | 16 +++++ .../Abp/Settings/SettingDefinitionContext.cs | 32 +++++++++ .../Abp/Settings/SettingDefinitionManager.cs | 68 +++++++++++++++++++ .../Volo/Abp/Settings/SettingManager.cs | 16 +++-- .../Volo/Abp/Settings/SettingOptions.cs | 4 ++ .../Volo/Abp/Settings/SettingProvider.cs | 9 +++ .../AbpIdentityApplicationTestModule.cs | 5 ++ .../Volo/Abp/Settings/SettingManager_Tests.cs | 17 ++++- .../Volo/Abp/Settings/TestSettingProvider.cs | 13 ++++ 14 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionContext.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingProvider.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingProvider.cs create mode 100644 test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs index 4fc8ea3d3f..66c1d50f5f 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs @@ -12,7 +12,7 @@ namespace Volo.Abp.Settings _settingStore = settingStore; } - public async Task GetOrNull(string name, string entityType, string entityId, bool fallback = true) + public async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) { //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 6bddcf4794..1fdaa206f3 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, string entityType, string entityId, bool fallback = true); + Task GetOrNullAsync(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/ISettingDefinitionContext.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionContext.cs new file mode 100644 index 0000000000..220e40fe59 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionContext.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.Settings +{ + public interface ISettingDefinitionContext + { + SettingDefinition GetOrNull(string name); + + void Add(params SettingDefinition[] definitions); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs new file mode 100644 index 0000000000..28ec16e185 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.Settings +{ + public interface ISettingDefinitionManager + { + SettingDefinition Get(string name); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingProvider.cs new file mode 100644 index 0000000000..d3334b3b2a --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingProvider.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.Settings +{ + public interface ISettingProvider + { + void Define(ISettingDefinitionContext context); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs new file mode 100644 index 0000000000..f025276b5e --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs @@ -0,0 +1,16 @@ +namespace Volo.Abp.Settings +{ + public class SettingDefinition + { + public string Name { get; } + + public string DefaultValue { get; set; } + + public SettingDefinition(string name, string defaultValue = null) + { + Name = name; + + DefaultValue = defaultValue; + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs new file mode 100644 index 0000000000..9ab4369b57 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; + +namespace Volo.Abp.Settings +{ + public class SettingDefinitionContext : ISettingDefinitionContext + { + protected Dictionary Settings { get; } + + public SettingDefinitionContext(Dictionary settings) + { + Settings = settings; + } + + public virtual SettingDefinition GetOrNull(string name) + { + return Settings.GetOrDefault(name); + } + + public virtual void Add(params SettingDefinition[] definitions) + { + if (definitions.IsNullOrEmpty()) + { + return; + } + + foreach (var definition in definitions) + { + Settings[definition.Name] = definition; + } + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs new file mode 100644 index 0000000000..14bb230b91 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Settings +{ + public class SettingDefinitionManager : ISettingDefinitionManager, ISingletonDependency + { + protected Lazy> Providers { get; } + + protected Lazy> SettingDefinitions { get; } + + protected SettingOptions Options { get; } + + private readonly IServiceProvider _serviceProvider; + + public SettingDefinitionManager( + IOptions options, + IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + Options = options.Value; + + Providers = new Lazy>(CreateSettingProviders, true); + SettingDefinitions = new Lazy>(CreateSettingDefinitions, true); + } + + public virtual SettingDefinition Get(string name) + { + var settingDefinition = GetOrNull(name); + + if (settingDefinition == null) + { + throw new AbpException("Undefined setting: " + name); + } + + return settingDefinition; + } + + public virtual SettingDefinition GetOrNull(string name) + { + return SettingDefinitions.Value.GetOrDefault(name); + } + + private List CreateSettingProviders() + { + return Options + .Providers + .Select(p => _serviceProvider.GetRequiredService(p) as ISettingProvider) + .ToList(); + } + + private IDictionary CreateSettingDefinitions() + { + var settings = new Dictionary(); + + foreach (var provider in Providers.Value) + { + provider.Define(new SettingDefinitionContext(settings)); + } + + return settings; + } + } +} \ 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 8fc98c8245..ca042a7108 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -10,12 +10,18 @@ namespace Volo.Abp.Settings { public class SettingManager : ISettingManager, ISingletonDependency { + protected ISettingDefinitionManager SettingDefinitionManager { get; } + protected Lazy> Contributors { get; } protected SettingOptions Options { get; } - public SettingManager(IOptions options, IServiceProvider serviceProvider) + public SettingManager( + IOptions options, + IServiceProvider serviceProvider, + ISettingDefinitionManager settingDefinitionManager) { + SettingDefinitionManager = settingDefinitionManager; Options = options.Value; Contributors = new Lazy>( @@ -32,18 +38,20 @@ namespace Volo.Abp.Settings return GetOrNullAsync(name, null, null); } - public Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) + public async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) { + var settingDefinition = SettingDefinitionManager.Get(name); + foreach (var contributor in Contributors.Value) { - var value = contributor.GetOrNull(name, entityType, entityId, fallback); + var value = await contributor.GetOrNullAsync(name, entityType, entityId, fallback); if (value != null) { return value; } } - return null; + return settingDefinition.DefaultValue; } public Task> GetAllAsync() diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs index 595b76aa64..3be5cc2e8d 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs @@ -6,12 +6,16 @@ namespace Volo.Abp.Settings { public ITypeList Contributors { get; } + public ITypeList Providers { get; set; } + public SettingOptions() { Contributors = new TypeList { typeof(DefaultSettingContributor) }; + + Providers = new TypeList(); } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingProvider.cs new file mode 100644 index 0000000000..bc5a436682 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingProvider.cs @@ -0,0 +1,9 @@ +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Settings +{ + public abstract class SettingProvider : ISettingProvider, ISingletonDependency + { + public abstract void Define(ISettingDefinitionContext context); + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs index 7015bda9cc..71c49212cf 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs @@ -32,6 +32,11 @@ namespace Volo.Abp.Settings { options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled; //EF in-memory database does not support transactions }); + + services.Configure(options => + { + options.Providers.Add(); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) 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 75f76a6180..383a2f801d 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 @@ -14,7 +14,22 @@ namespace Volo.Abp.Settings } [Fact] - public async Task Test1() + public async Task Should_Throw_Exception_When_Try_To_Get_An_Undefined_Setting() + { + await Assert.ThrowsAsync( + async () => await _settingManager.GetOrNullAsync("UndefinedSetting") + ); + } + + [Fact] + public async Task Should_Get_Default_Value_If_Not_Set_In_Store() + { + var value = await _settingManager.GetOrNullAsync("SettingNotSetInStore"); + value.ShouldBe("default-value"); + } + + [Fact] + public async Task Should_Get_From_Store_Without_Entity_Props() { var value = await _settingManager.GetOrNullAsync("MySetting1"); value.ShouldBe("42"); diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs new file mode 100644 index 0000000000..ea06231514 --- /dev/null +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs @@ -0,0 +1,13 @@ +using System; + +namespace Volo.Abp.Settings +{ + public class TestSettingProvider : SettingProvider + { + public override void Define(ISettingDefinitionContext context) + { + context.Add(new SettingDefinition("MySetting1")); + context.Add(new SettingDefinition("SettingNotSetInStore", defaultValue: "default-value")); + } + } +} \ No newline at end of file From 99d6556a16b0009e57a75e7da453f671ccd574ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 14:01:04 +0300 Subject: [PATCH 05/21] Revised setting system. --- src/Volo.Abp.Session/Volo.Abp.Session.csproj | 1 + .../Volo/Abp/Session/AbpSessionModule.cs | 7 +++ .../Abp/Session/UserSettingContributor.cs | 37 ++++++++++++ .../Settings/UserSettingManagerExtensions.cs | 26 ++++++++ .../Abp/Settings/DefaultSettingContributor.cs | 21 ------- .../Volo/Abp/Settings/ISettingContributor.cs | 6 +- .../Abp/Settings/ISettingDefinitionManager.cs | 7 ++- .../Volo/Abp/Settings/ISettingManager.cs | 2 +- .../Volo/Abp/Settings/SettingContributor.cs | 21 +++++++ .../Abp/Settings/SettingDefinitionManager.cs | 2 + .../Volo/Abp/Settings/SettingManager.cs | 60 ++++++++++++++++--- .../Volo/Abp/Settings/SettingOptions.cs | 6 +- 12 files changed, 158 insertions(+), 38 deletions(-) create mode 100644 src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs create mode 100644 src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs delete mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs diff --git a/src/Volo.Abp.Session/Volo.Abp.Session.csproj b/src/Volo.Abp.Session/Volo.Abp.Session.csproj index dde6a11a58..6aef78ea80 100644 --- a/src/Volo.Abp.Session/Volo.Abp.Session.csproj +++ b/src/Volo.Abp.Session/Volo.Abp.Session.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs b/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs index b547f74663..2f9a3e7cf4 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs @@ -1,14 +1,21 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; using Volo.Abp.Security; +using Volo.Abp.Settings; namespace Volo.Abp.Session { [DependsOn(typeof(AbpSecurityModule))] + [DependsOn(typeof(AbpSettingsModule))] public class AbpSessionModule : AbpModule { public override void ConfigureServices(IServiceCollection services) { + services.Configure(options => + { + options.Contributors.Add(); + }); + services.AddAssemblyOf(); } } diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs new file mode 100644 index 0000000000..009b59bd81 --- /dev/null +++ b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs @@ -0,0 +1,37 @@ +using System.Threading.Tasks; +using Volo.Abp.Settings; + +namespace Volo.Abp.Session +{ + //TODO: Optimization: Get all settings and cache it! + + public class UserSettingContributor : SettingContributor + { + public const string DefaultEntityType = "User"; + + public override string EntityType => DefaultEntityType; + + protected ICurrentUser CurrentUser { get; } + + public UserSettingContributor(ISettingStore settingStore, ICurrentUser currentUser) + : base(settingStore) + { + CurrentUser = currentUser; + } + + public override async Task GetOrNullAsync(string name, bool fallback) + { + if (CurrentUser.Id == null) + { + return null; + } + + return await SettingStore.GetOrNullAsync(name, EntityType, CurrentUser.Id.Value.ToString()); + } + + public override async Task GetOrNullAsync(string name, string entityId, bool fallback = true) + { + return await SettingStore.GetOrNullAsync(name, EntityType, entityId); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs new file mode 100644 index 0000000000..62eafb94c3 --- /dev/null +++ b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Volo.Abp.Session; + +namespace Volo.Abp.Settings +{ + public static class UserSettingManagerExtensions + { + public static Task GetOrNullForUserAsync(this ISettingManager settingManager, [NotNull] string name, Guid userId, bool fallback = true) + { + return settingManager.GetOrNullAsync(name, UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); + } + + public static Task> GetAllForUserAsync(this ISettingManager settingManager, Guid userId, bool fallback = true) + { + return settingManager.GetAllAsync(UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); + } + + public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value) + { + return settingManager.SetAsync(name, value, UserSettingContributor.DefaultEntityType, userId.ToString()); + } + } +} diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs deleted file mode 100644 index 66c1d50f5f..0000000000 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultSettingContributor.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Settings -{ - public class DefaultSettingContributor : ISettingContributor, ISingletonDependency - { - private readonly ISettingStore _settingStore; - - public DefaultSettingContributor(ISettingStore settingStore) - { - _settingStore = settingStore; - } - - public async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) - { - //TODO: Optimization: Get all settings and cache it! - return await _settingStore.GetOrNullAsync(name, null, null); - } - } -} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs index 1fdaa206f3..0b48f06839 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs @@ -4,6 +4,10 @@ namespace Volo.Abp.Settings { public interface ISettingContributor { - Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true); + string EntityType { get; } + + Task GetOrNullAsync(string name, bool fallback); + + Task GetOrNullAsync(string name, string entityId, bool fallback = true); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs index 28ec16e185..e8795e283b 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs @@ -1,7 +1,10 @@ -namespace Volo.Abp.Settings +using JetBrains.Annotations; + +namespace Volo.Abp.Settings { public interface ISettingDefinitionManager { - SettingDefinition Get(string name); + [NotNull] + SettingDefinition Get([NotNull] string name); } } \ 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 b768648e9d..d51da5b1f2 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -5,7 +5,7 @@ namespace Volo.Abp.Settings { public interface ISettingManager { - Task GetOrNullAsync(string name); + Task GetOrNullAsync(string name, bool fallback = true); Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true); diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs new file mode 100644 index 0000000000..f3c7a226d5 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs @@ -0,0 +1,21 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Settings +{ + public abstract class SettingContributor : ISettingContributor, ISingletonDependency + { + public abstract string EntityType { get; } + + protected ISettingStore SettingStore { get; } + + protected SettingContributor(ISettingStore settingStore) + { + SettingStore = settingStore; + } + + public abstract Task GetOrNullAsync(string name, bool fallback); + + public abstract Task GetOrNullAsync(string name, string entityId, bool fallback = true); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs index 14bb230b91..a80ee47ab2 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs @@ -30,6 +30,8 @@ namespace Volo.Abp.Settings public virtual SettingDefinition Get(string name) { + Check.NotNull(name, nameof(name)); + var settingDefinition = GetOrNull(name); if (settingDefinition == null) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index ca042a7108..b059bfe18a 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -16,11 +16,15 @@ namespace Volo.Abp.Settings protected SettingOptions Options { get; } + protected ISettingStore SettingStore { get; } + public SettingManager( - IOptions options, - IServiceProvider serviceProvider, - ISettingDefinitionManager settingDefinitionManager) + IOptions options, + IServiceProvider serviceProvider, + ISettingDefinitionManager settingDefinitionManager, + ISettingStore settingStore) { + SettingStore = settingStore; SettingDefinitionManager = settingDefinitionManager; Options = options.Value; @@ -33,27 +37,38 @@ namespace Volo.Abp.Settings ); } - public Task GetOrNullAsync(string name) + public Task GetOrNullAsync(string name, bool fallback = true) { - return GetOrNullAsync(name, null, null); + return GetOrNullAsync(name, null, null, fallback); } public async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) { var settingDefinition = SettingDefinitionManager.Get(name); - foreach (var contributor in Contributors.Value) + foreach (var contributor in GetContributors(entityType, fallback)) { - var value = await contributor.GetOrNullAsync(name, entityType, entityId, fallback); + var value = await GetContributorValue(contributor, name, entityId, fallback); if (value != null) { return value; } } + var defaultStoreValue = await SettingStore.GetOrNullAsync(name, null, null); + if (defaultStoreValue != null) + { + return defaultStoreValue; + } + + if (!fallback) + { + return null; + } + return settingDefinition.DefaultValue; } - + public Task> GetAllAsync() { throw new System.NotImplementedException(); @@ -73,5 +88,34 @@ namespace Volo.Abp.Settings { throw new System.NotImplementedException(); } + + private static async Task GetContributorValue(ISettingContributor contributor, string name, string entityId, bool fallback) + { + if (entityId != null) + { + return await contributor.GetOrNullAsync(name, entityId, fallback); + } + else + { + return await contributor.GetOrNullAsync(name, fallback); + } + } + + private IEnumerable GetContributors(string entityType, bool fallback) + { + var contributors = Enumerable.Reverse(Contributors.Value); + + if (entityType != null) + { + contributors = contributors.SkipWhile(c => c.EntityType != entityType); + } + + if (!fallback) + { + contributors = contributors.TakeWhile(c => c.EntityType == entityType); + } + + return contributors; + } } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs index 3be5cc2e8d..eb4b65c44a 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs @@ -10,11 +10,7 @@ namespace Volo.Abp.Settings public SettingOptions() { - Contributors = new TypeList - { - typeof(DefaultSettingContributor) - }; - + Contributors = new TypeList(); Providers = new TypeList(); } } From 1d212e090b91653248793509ac1064501bdd037e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 15:25:40 +0300 Subject: [PATCH 06/21] Refactored setting system. --- .../Abp/Session/UserSettingContributor.cs | 21 +++--- .../Volo/Abp/Settings/ISettingContributor.cs | 5 +- .../Volo/Abp/Settings/ISettingManager.cs | 11 +-- .../Volo/Abp/Settings/SettingContributor.cs | 4 +- .../Volo/Abp/Settings/SettingManager.cs | 71 ++++++++++--------- .../AbpIdentityApplicationTestModule.cs | 6 +- .../Settings/AbpIdentityTestDataBuilder.cs | 11 ++- .../Volo/Abp/Settings/SettingManager_Tests.cs | 5 +- .../SettingManager_User_Based_Tests.cs | 57 +++++++++++++++ .../Volo/Abp/Settings/TestSettingProvider.cs | 5 +- 10 files changed, 135 insertions(+), 61 deletions(-) create mode 100644 test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs index 009b59bd81..931e08ef7f 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Volo.Abp.Settings; namespace Volo.Abp.Session @@ -19,19 +20,19 @@ namespace Volo.Abp.Session CurrentUser = currentUser; } - public override async Task GetOrNullAsync(string name, bool fallback) + public override async Task GetOrNullAsync(SettingDefinition setting, string entityId) { - if (CurrentUser.Id == null) + if (entityId == null) { - return null; - } + if (CurrentUser.Id == null) + { + return null; + } - return await SettingStore.GetOrNullAsync(name, EntityType, CurrentUser.Id.Value.ToString()); - } + entityId = CurrentUser.Id.ToString(); + } - public override async Task GetOrNullAsync(string name, string entityId, bool fallback = true) - { - return await SettingStore.GetOrNullAsync(name, EntityType, entityId); + return await SettingStore.GetOrNullAsync(setting.Name, EntityType, entityId); } } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs index 0b48f06839..9a90e42f8d 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using JetBrains.Annotations; namespace Volo.Abp.Settings { @@ -6,8 +7,6 @@ namespace Volo.Abp.Settings { string EntityType { get; } - Task GetOrNullAsync(string name, bool fallback); - - Task GetOrNullAsync(string name, string entityId, bool fallback = true); + Task GetOrNullAsync(SettingDefinition setting, [CanBeNull] string entityId); } } \ 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 d51da5b1f2..96f2f5648e 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -1,20 +1,21 @@ using System.Collections.Generic; using System.Threading.Tasks; +using JetBrains.Annotations; namespace Volo.Abp.Settings { public interface ISettingManager { - Task GetOrNullAsync(string name, bool fallback = true); + Task GetOrNullAsync([NotNull]string name); - Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true); + Task GetOrNullAsync([NotNull]string name, [NotNull] string entityType, string entityId, bool fallback = true); Task> GetAllAsync(); - Task> GetAllAsync(string entityType, string entityId, bool fallback = true); + Task> GetAllAsync([NotNull] string entityType, string entityId, bool fallback = true); - Task SetAsync(string name, string value); + Task SetAsync([NotNull] string name, string value, bool forceToSet = false); - Task SetAsync(string name, string value, string entityType, string entityId); + Task SetAsync([NotNull] string name, string value, [NotNull] string entityType, string entityId, bool forceToSet = false); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs index f3c7a226d5..ba8129e972 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs @@ -14,8 +14,6 @@ namespace Volo.Abp.Settings SettingStore = settingStore; } - public abstract Task GetOrNullAsync(string name, bool fallback); - - public abstract Task GetOrNullAsync(string name, string entityId, bool fallback = true); + public abstract Task GetOrNullAsync(SettingDefinition setting, string entityId); } } \ 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 b059bfe18a..18ebba6cc0 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -37,18 +37,15 @@ namespace Volo.Abp.Settings ); } - public Task GetOrNullAsync(string name, bool fallback = true) + public async Task GetOrNullAsync(string name) { - return GetOrNullAsync(name, null, null, fallback); - } + Check.NotNull(name, nameof(name)); - public async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) - { - var settingDefinition = SettingDefinitionManager.Get(name); + var setting = SettingDefinitionManager.Get(name); - foreach (var contributor in GetContributors(entityType, fallback)) + foreach (var contributor in Enumerable.Reverse(Contributors.Value)) { - var value = await GetContributorValue(contributor, name, entityId, fallback); + var value = await contributor.GetOrNullAsync(setting, null); if (value != null) { return value; @@ -61,17 +58,42 @@ namespace Volo.Abp.Settings return defaultStoreValue; } + return setting.DefaultValue; + } + + public virtual async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) + { + Check.NotNull(name, nameof(name)); + Check.NotNull(entityType, nameof(entityType)); + + var setting = SettingDefinitionManager.Get(name); + + foreach (var contributor in GetFilteredContributors(entityType, fallback)) + { + var value = await contributor.GetOrNullAsync(setting, entityId); + if (value != null) + { + return value; + } + } + if (!fallback) { return null; } - return settingDefinition.DefaultValue; + var defaultStoreValue = await SettingStore.GetOrNullAsync(name, null, null); + if (defaultStoreValue != null) + { + return defaultStoreValue; + } + + return setting.DefaultValue; } - + public Task> GetAllAsync() { - throw new System.NotImplementedException(); + return GetAllAsync(null, null); } public Task> GetAllAsync(string entityType, string entityId, bool fallback = true) @@ -79,36 +101,21 @@ namespace Volo.Abp.Settings throw new System.NotImplementedException(); } - public Task SetAsync(string name, string value) + public Task SetAsync(string name, string value, bool forceToSet = false) { throw new System.NotImplementedException(); } - public Task SetAsync(string name, string value, string entityType, string entityId) + public Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) { throw new System.NotImplementedException(); } - private static async Task GetContributorValue(ISettingContributor contributor, string name, string entityId, bool fallback) - { - if (entityId != null) - { - return await contributor.GetOrNullAsync(name, entityId, fallback); - } - else - { - return await contributor.GetOrNullAsync(name, fallback); - } - } - - private IEnumerable GetContributors(string entityType, bool fallback) + private IEnumerable GetFilteredContributors(string entityType, bool fallback) { - var contributors = Enumerable.Reverse(Contributors.Value); - - if (entityType != null) - { - contributors = contributors.SkipWhile(c => c.EntityType != entityType); - } + var contributors = Enumerable + .Reverse(Contributors.Value) + .SkipWhile(c => c.EntityType != entityType); if (!fallback) { diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs index 71c49212cf..871aeab2f5 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs @@ -4,12 +4,16 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.Modularity; +using Volo.Abp.Session; using Volo.Abp.Settings.EntityFrameworkCore; using Volo.Abp.Uow; namespace Volo.Abp.Settings { - [DependsOn(typeof(AbpSettingsEntityFrameworkCoreModule), typeof(AbpAutofacModule))] + [DependsOn( + typeof(AbpSettingsEntityFrameworkCoreModule), + typeof(AbpSessionModule), + typeof(AbpAutofacModule))] public class AbpSettingsTestModule : AbpModule { public override void ConfigureServices(IServiceCollection services) 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 707eb4c7d0..a1e5850c23 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs @@ -1,10 +1,15 @@ -using Volo.Abp.DependencyInjection; +using System; +using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; +using Volo.Abp.Session; namespace Volo.Abp.Settings { public class AbpIdentityTestDataBuilder : ITransientDependency { + public static Guid User1Id = Guid.NewGuid(); + public static Guid User2Id = Guid.NewGuid(); + private readonly ISettingRepository _settingRepository; private readonly IGuidGenerator _guidGenerator; @@ -17,6 +22,10 @@ namespace Volo.Abp.Settings public void Build() { _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting1", "42")); + + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "default-store-value")); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user1-store-value", UserSettingContributor.DefaultEntityType, User1Id.ToString())); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user2-store-value", UserSettingContributor.DefaultEntityType, User2Id.ToString())); } } } \ 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 383a2f801d..1196910994 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 @@ -29,10 +29,9 @@ namespace Volo.Abp.Settings } [Fact] - public async Task Should_Get_From_Store_Without_Entity_Props() + public async Task Should_Get_Base_Store_Value() { - var value = await _settingManager.GetOrNullAsync("MySetting1"); - value.ShouldBe("42"); + (await _settingManager.GetOrNullAsync("MySetting1")).ShouldBe("42"); } } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs new file mode 100644 index 0000000000..dd421b3243 --- /dev/null +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs @@ -0,0 +1,57 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; +using Volo.Abp.Session; +using Xunit; + +namespace Volo.Abp.Settings +{ + public class SettingManager_User_Based_Tests: AbpSettingsTestBase + { + private Guid _currentUserId; + private readonly ISettingManager _settingManager; + + public SettingManager_User_Based_Tests() + { + _settingManager = GetRequiredService(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + ICurrentUser currentUser = Substitute.For(); + currentUser.Id.Returns(ci => _currentUserId); + services.AddSingleton(currentUser); + } + + [Fact] + public async Task Should_Get_From_Store_For_Given_User() + { + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)).ShouldBe("user1-store-value"); + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User2Id)).ShouldBe("user2-store-value"); + } + + [Fact] + public async Task Should_Get_From_Store_For_Current_User() + { + _currentUserId = AbpIdentityTestDataBuilder.User1Id; + (await _settingManager.GetOrNullAsync("MySetting2")).ShouldBe("user1-store-value"); + + _currentUserId = AbpIdentityTestDataBuilder.User2Id; + (await _settingManager.GetOrNullAsync("MySetting2")).ShouldBe("user2-store-value"); + } + + [Fact] + public async Task Should_Fallback_To_Default_Store_Value_When_No_Value_For_Given_User() + { + (await _settingManager.GetOrNullForUserAsync("MySetting2", Guid.NewGuid())).ShouldBe("default-store-value"); + } + + [Fact] + public async Task Should_Not_Fallback_To_Default_Store_Value_When_No_Value_For_Given_User_But_Specified_Fallback_As_False() + { + (await _settingManager.GetOrNullForUserAsync("MySetting2", Guid.NewGuid(), fallback: false)).ShouldBeNull(); + } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs index ea06231514..eaa29cd7da 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs @@ -1,12 +1,11 @@ -using System; - -namespace Volo.Abp.Settings +namespace Volo.Abp.Settings { public class TestSettingProvider : SettingProvider { public override void Define(ISettingDefinitionContext context) { context.Add(new SettingDefinition("MySetting1")); + context.Add(new SettingDefinition("MySetting2")); context.Add(new SettingDefinition("SettingNotSetInStore", defaultValue: "default-value")); } } From 89dd7f4970161bbefe1765edf17fb4e522eba248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 15:34:46 +0300 Subject: [PATCH 07/21] Simplify setting manager by extracting two default contributor. --- .../DefaultStoreSettingContributor.cs | 19 +++++++ .../DefaultValueSettingContributor.cs | 20 ++++++++ .../Volo/Abp/Settings/SettingManager.cs | 50 ++++++------------- .../Volo/Abp/Settings/SettingOptions.cs | 10 ++-- 4 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingContributor.cs create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingContributor.cs diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingContributor.cs new file mode 100644 index 0000000000..170909fbe4 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingContributor.cs @@ -0,0 +1,19 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Settings +{ + public class DefaultStoreSettingContributor : SettingContributor + { + public override string EntityType => null; + + public DefaultStoreSettingContributor(ISettingStore settingStore) + : base(settingStore) + { + } + + public override Task GetOrNullAsync(SettingDefinition setting, string entityId) + { + return SettingStore.GetOrNullAsync(setting.Name, null, null); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingContributor.cs new file mode 100644 index 0000000000..e32ad82a34 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingContributor.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Settings +{ + public class DefaultValueSettingContributor : SettingContributor + { + public override string EntityType => null; + + public DefaultValueSettingContributor(ISettingStore settingStore) + : base(settingStore) + { + + } + + public override Task GetOrNullAsync(SettingDefinition setting, string entityId) + { + return Task.FromResult(setting.DefaultValue); + } + } +} \ 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 18ebba6cc0..5daf45266a 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -43,7 +43,9 @@ namespace Volo.Abp.Settings var setting = SettingDefinitionManager.Get(name); - foreach (var contributor in Enumerable.Reverse(Contributors.Value)) + var contributors = Enumerable.Reverse(Contributors.Value); + + foreach (var contributor in contributors) { var value = await contributor.GetOrNullAsync(setting, null); if (value != null) @@ -52,13 +54,7 @@ namespace Volo.Abp.Settings } } - var defaultStoreValue = await SettingStore.GetOrNullAsync(name, null, null); - if (defaultStoreValue != null) - { - return defaultStoreValue; - } - - return setting.DefaultValue; + return null; } public virtual async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) @@ -68,27 +64,25 @@ namespace Volo.Abp.Settings var setting = SettingDefinitionManager.Get(name); - foreach (var contributor in GetFilteredContributors(entityType, fallback)) - { - var value = await contributor.GetOrNullAsync(setting, entityId); - if (value != null) - { - return value; - } - } + var contributors = Enumerable + .Reverse(Contributors.Value) + .SkipWhile(c => c.EntityType != entityType); if (!fallback) { - return null; + contributors = contributors.TakeWhile(c => c.EntityType == entityType); } - var defaultStoreValue = await SettingStore.GetOrNullAsync(name, null, null); - if (defaultStoreValue != null) + foreach (var contributor in contributors) { - return defaultStoreValue; + var value = await contributor.GetOrNullAsync(setting, entityId); + if (value != null) + { + return value; + } } - return setting.DefaultValue; + return null; } public Task> GetAllAsync() @@ -110,19 +104,5 @@ namespace Volo.Abp.Settings { throw new System.NotImplementedException(); } - - private IEnumerable GetFilteredContributors(string entityType, bool fallback) - { - var contributors = Enumerable - .Reverse(Contributors.Value) - .SkipWhile(c => c.EntityType != entityType); - - if (!fallback) - { - contributors = contributors.TakeWhile(c => c.EntityType == entityType); - } - - return contributors; - } } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs index eb4b65c44a..5e87bdcf0f 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs @@ -4,14 +4,18 @@ namespace Volo.Abp.Settings { public class SettingOptions { - public ITypeList Contributors { get; } - public ITypeList Providers { get; set; } + public ITypeList Contributors { get; } + public SettingOptions() { - Contributors = new TypeList(); Providers = new TypeList(); + Contributors = new TypeList + { + typeof(DefaultValueSettingContributor), + typeof(DefaultStoreSettingContributor) + }; } } } From fc3ca579adc83530771e4b4dbfa3d5c1a6531fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 15:47:38 +0300 Subject: [PATCH 08/21] Added annotations --- src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs | 6 +++--- src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs index 96f2f5648e..00b0a89e31 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -8,14 +8,14 @@ namespace Volo.Abp.Settings { Task GetOrNullAsync([NotNull]string name); - Task GetOrNullAsync([NotNull]string name, [NotNull] string entityType, string entityId, bool fallback = true); + Task GetOrNullAsync([NotNull]string name, [NotNull] string entityType, [CanBeNull] string entityId, bool fallback = true); Task> GetAllAsync(); - Task> GetAllAsync([NotNull] string entityType, string entityId, bool fallback = true); + Task> GetAllAsync([NotNull] string entityType, [CanBeNull] string entityId, bool fallback = true); Task SetAsync([NotNull] string name, string value, bool forceToSet = false); - Task SetAsync([NotNull] string name, string value, [NotNull] string entityType, string entityId, bool forceToSet = false); + Task SetAsync([NotNull] string name, 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 5daf45266a..db7cf2f7da 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -87,7 +87,7 @@ namespace Volo.Abp.Settings public Task> GetAllAsync() { - return GetAllAsync(null, null); + throw new System.NotImplementedException(); } public Task> GetAllAsync(string entityType, string entityId, bool fallback = true) From 6ccdd1a45602f2ce6e28ee53772f786f1a4cf055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 15:59:59 +0300 Subject: [PATCH 09/21] Added tests. --- .../Settings/UserSettingManagerExtensions.cs | 15 +++++++ .../Volo/Abp/Settings/ISettingContributor.cs | 2 +- .../Volo/Abp/Settings/ISettingManager.cs | 4 +- .../Volo/Abp/Settings/SettingManager.cs | 12 +++--- .../SettingManager_User_Based_Tests.cs | 39 +++++++++++++++++-- 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs index 62eafb94c3..03eec3a914 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs @@ -13,14 +13,29 @@ namespace Volo.Abp.Settings return settingManager.GetOrNullAsync(name, UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); } + public static Task GetOrNullForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, bool fallback = true) + { + return settingManager.GetOrNullAsync(name, UserSettingContributor.DefaultEntityType, null, fallback); + } + public static Task> GetAllForUserAsync(this ISettingManager settingManager, Guid userId, bool fallback = true) { return settingManager.GetAllAsync(UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); } + public static Task> GetAllForCurrentUserAsync(this ISettingManager settingManager, bool fallback = true) + { + return settingManager.GetAllAsync(UserSettingContributor.DefaultEntityType, null, fallback); + } + public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value) { return settingManager.SetAsync(name, value, UserSettingContributor.DefaultEntityType, userId.ToString()); } + + public static Task SetForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value) + { + return settingManager.SetAsync(name, value, UserSettingContributor.DefaultEntityType, null); + } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs index 9a90e42f8d..42bc9ef207 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs @@ -7,6 +7,6 @@ namespace Volo.Abp.Settings { string EntityType { get; } - Task GetOrNullAsync(SettingDefinition setting, [CanBeNull] string entityId); + Task GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string entityId); } } \ 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 00b0a89e31..4c3e64a3b3 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -14,8 +14,8 @@ namespace Volo.Abp.Settings Task> GetAllAsync([NotNull] string entityType, [CanBeNull] string entityId, bool fallback = true); - Task SetAsync([NotNull] string name, string value, bool forceToSet = false); + Task SetAsync([NotNull] string name, [CanBeNull] string value, bool forceToSet = false); - Task SetAsync([NotNull] string name, string value, [NotNull] string entityType, [CanBeNull] string entityId, 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 db7cf2f7da..76ce80bc4b 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -37,12 +37,11 @@ namespace Volo.Abp.Settings ); } - public async Task GetOrNullAsync(string name) + public virtual async Task GetOrNullAsync(string name) { Check.NotNull(name, nameof(name)); var setting = SettingDefinitionManager.Get(name); - var contributors = Enumerable.Reverse(Contributors.Value); foreach (var contributor in contributors) @@ -63,7 +62,6 @@ namespace Volo.Abp.Settings Check.NotNull(entityType, nameof(entityType)); var setting = SettingDefinitionManager.Get(name); - var contributors = Enumerable .Reverse(Contributors.Value) .SkipWhile(c => c.EntityType != entityType); @@ -85,22 +83,22 @@ namespace Volo.Abp.Settings return null; } - public Task> GetAllAsync() + public virtual Task> GetAllAsync() { throw new System.NotImplementedException(); } - public Task> GetAllAsync(string entityType, string entityId, bool fallback = true) + public virtual Task> GetAllAsync(string entityType, string entityId, bool fallback = true) { throw new System.NotImplementedException(); } - public Task SetAsync(string name, string value, bool forceToSet = false) + public virtual Task SetAsync(string name, string value, bool forceToSet = false) { throw new System.NotImplementedException(); } - public Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) + public virtual Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) { throw new System.NotImplementedException(); } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs index dd421b3243..6c3cc49e0a 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs @@ -32,6 +32,18 @@ namespace Volo.Abp.Settings (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User2Id)).ShouldBe("user2-store-value"); } + [Fact] + public async Task Should_Fallback_To_Default_Store_Value_When_No_Value_For_Given_User() + { + (await _settingManager.GetOrNullForUserAsync("MySetting2", Guid.NewGuid())).ShouldBe("default-store-value"); + } + + [Fact] + public async Task Should_Not_Fallback_To_Default_Store_Value_When_No_Value_For_Given_User_But_Specified_Fallback_As_False() + { + (await _settingManager.GetOrNullForUserAsync("MySetting2", Guid.NewGuid(), fallback: false)).ShouldBeNull(); + } + [Fact] public async Task Should_Get_From_Store_For_Current_User() { @@ -43,15 +55,34 @@ namespace Volo.Abp.Settings } [Fact] - public async Task Should_Fallback_To_Default_Store_Value_When_No_Value_For_Given_User() + public async Task Should_Fallback_To_Default_Store_Value_When_No_Value_For_Current_User() { - (await _settingManager.GetOrNullForUserAsync("MySetting2", Guid.NewGuid())).ShouldBe("default-store-value"); + _currentUserId = Guid.NewGuid(); + (await _settingManager.GetOrNullAsync("MySetting2")).ShouldBe("default-store-value"); } [Fact] - public async Task Should_Not_Fallback_To_Default_Store_Value_When_No_Value_For_Given_User_But_Specified_Fallback_As_False() + public async Task Should_Get_From_Store_For_Current_User_With_GetOrNullForCurrentUserAsync() { - (await _settingManager.GetOrNullForUserAsync("MySetting2", Guid.NewGuid(), fallback: false)).ShouldBeNull(); + _currentUserId = AbpIdentityTestDataBuilder.User1Id; + (await _settingManager.GetOrNullForCurrentUserAsync("MySetting2")).ShouldBe("user1-store-value"); + + _currentUserId = AbpIdentityTestDataBuilder.User2Id; + (await _settingManager.GetOrNullForCurrentUserAsync("MySetting2")).ShouldBe("user2-store-value"); + } + + [Fact] + public async Task Should_Fallback_To_Default_Store_Value_When_No_Value_For_Current_User_With_GetOrNullForCurrentUserAsync() + { + _currentUserId = Guid.NewGuid(); + (await _settingManager.GetOrNullAsync("MySetting2")).ShouldBe("default-store-value"); + } + + [Fact] + public async Task Should_Not_Fallback_To_Default_Store_Value_When_No_Value_For_Current_User_But_Specified_Fallback_As_False() + { + _currentUserId = Guid.NewGuid(); + (await _settingManager.GetOrNullForCurrentUserAsync("MySetting2", fallback: false)).ShouldBeNull(); } } } \ No newline at end of file From 5b77271e490a5c512abdfb2ba2f1e068936d8c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 16:44:14 +0300 Subject: [PATCH 10/21] Renamed ISettingContributor and ISettingProvider. --- .../Volo/Abp/Session/AbpSessionModule.cs | 2 +- ...ributor.cs => UserSettingValueProvider.cs} | 4 ++-- .../Settings/UserSettingManagerExtensions.cs | 12 +++++------ ...cs => DefaultStoreSettingValueProvider.cs} | 4 ++-- ...cs => DefaultValueSettingValueProvider.cs} | 4 ++-- ...vider.cs => ISettingDefinitionProvider.cs} | 2 +- ...ontributor.cs => ISettingValueProvider.cs} | 2 +- .../Abp/Settings/SettingDefinitionManager.cs | 10 +++++----- ...ovider.cs => SettingDefinitionProvider.cs} | 2 +- .../Volo/Abp/Settings/SettingManager.cs | 20 +++++++++---------- .../Volo/Abp/Settings/SettingOptions.cs | 12 +++++------ ...Contributor.cs => SettingValueProvider.cs} | 4 ++-- .../AbpIdentityApplicationTestModule.cs | 2 +- .../Settings/AbpIdentityTestDataBuilder.cs | 4 ++-- ...er.cs => TestSettingDefinitionProvider.cs} | 2 +- 15 files changed, 43 insertions(+), 43 deletions(-) rename src/Volo.Abp.Session/Volo/Abp/Session/{UserSettingContributor.cs => UserSettingValueProvider.cs} (84%) rename src/Volo.Abp.Settings/Volo/Abp/Settings/{DefaultStoreSettingContributor.cs => DefaultStoreSettingValueProvider.cs} (71%) rename src/Volo.Abp.Settings/Volo/Abp/Settings/{DefaultValueSettingContributor.cs => DefaultValueSettingValueProvider.cs} (70%) rename src/Volo.Abp.Settings/Volo/Abp/Settings/{ISettingProvider.cs => ISettingDefinitionProvider.cs} (67%) rename src/Volo.Abp.Settings/Volo/Abp/Settings/{ISettingContributor.cs => ISettingValueProvider.cs} (84%) rename src/Volo.Abp.Settings/Volo/Abp/Settings/{SettingProvider.cs => SettingDefinitionProvider.cs} (59%) rename src/Volo.Abp.Settings/Volo/Abp/Settings/{SettingContributor.cs => SettingValueProvider.cs} (70%) rename test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/{TestSettingProvider.cs => TestSettingDefinitionProvider.cs} (82%) diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs b/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs index 2f9a3e7cf4..33cc5e2a88 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/AbpSessionModule.cs @@ -13,7 +13,7 @@ namespace Volo.Abp.Session { services.Configure(options => { - options.Contributors.Add(); + options.ValueProviders.Add(); }); services.AddAssemblyOf(); diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs similarity index 84% rename from src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs rename to src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs index 931e08ef7f..39adb5cdd0 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs @@ -6,7 +6,7 @@ namespace Volo.Abp.Session { //TODO: Optimization: Get all settings and cache it! - public class UserSettingContributor : SettingContributor + public class UserSettingValueProvider : SettingValueProvider { public const string DefaultEntityType = "User"; @@ -14,7 +14,7 @@ namespace Volo.Abp.Session protected ICurrentUser CurrentUser { get; } - public UserSettingContributor(ISettingStore settingStore, ICurrentUser currentUser) + public UserSettingValueProvider(ISettingStore settingStore, ICurrentUser currentUser) : base(settingStore) { CurrentUser = currentUser; diff --git a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs index 03eec3a914..019cf7eadf 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs @@ -10,32 +10,32 @@ namespace Volo.Abp.Settings { public static Task GetOrNullForUserAsync(this ISettingManager settingManager, [NotNull] string name, Guid userId, bool fallback = true) { - return settingManager.GetOrNullAsync(name, UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); + return settingManager.GetOrNullAsync(name, UserSettingValueProvider.DefaultEntityType, userId.ToString(), fallback); } public static Task GetOrNullForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, bool fallback = true) { - return settingManager.GetOrNullAsync(name, UserSettingContributor.DefaultEntityType, null, fallback); + return settingManager.GetOrNullAsync(name, UserSettingValueProvider.DefaultEntityType, null, fallback); } public static Task> GetAllForUserAsync(this ISettingManager settingManager, Guid userId, bool fallback = true) { - return settingManager.GetAllAsync(UserSettingContributor.DefaultEntityType, userId.ToString(), fallback); + return settingManager.GetAllAsync(UserSettingValueProvider.DefaultEntityType, userId.ToString(), fallback); } public static Task> GetAllForCurrentUserAsync(this ISettingManager settingManager, bool fallback = true) { - return settingManager.GetAllAsync(UserSettingContributor.DefaultEntityType, null, fallback); + return settingManager.GetAllAsync(UserSettingValueProvider.DefaultEntityType, null, fallback); } public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value) { - return settingManager.SetAsync(name, value, UserSettingContributor.DefaultEntityType, userId.ToString()); + return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, userId.ToString()); } public static Task SetForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value) { - return settingManager.SetAsync(name, value, UserSettingContributor.DefaultEntityType, null); + return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, null); } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs similarity index 71% rename from src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingContributor.cs rename to src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs index 170909fbe4..3369e987d2 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs @@ -2,11 +2,11 @@ namespace Volo.Abp.Settings { - public class DefaultStoreSettingContributor : SettingContributor + public class DefaultStoreSettingValueProvider : SettingValueProvider { public override string EntityType => null; - public DefaultStoreSettingContributor(ISettingStore settingStore) + public DefaultStoreSettingValueProvider(ISettingStore settingStore) : base(settingStore) { } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs similarity index 70% rename from src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingContributor.cs rename to src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs index e32ad82a34..09a81028bf 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs @@ -2,11 +2,11 @@ namespace Volo.Abp.Settings { - public class DefaultValueSettingContributor : SettingContributor + public class DefaultValueSettingValueProvider : SettingValueProvider { public override string EntityType => null; - public DefaultValueSettingContributor(ISettingStore settingStore) + public DefaultValueSettingValueProvider(ISettingStore settingStore) : base(settingStore) { diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionProvider.cs similarity index 67% rename from src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingProvider.cs rename to src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionProvider.cs index d3334b3b2a..383b54088f 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionProvider.cs @@ -1,6 +1,6 @@ namespace Volo.Abp.Settings { - public interface ISettingProvider + public interface ISettingDefinitionProvider { void Define(ISettingDefinitionContext context); } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs similarity index 84% rename from src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs rename to src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs index 42bc9ef207..05eda04da3 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs @@ -3,7 +3,7 @@ using JetBrains.Annotations; namespace Volo.Abp.Settings { - public interface ISettingContributor + public interface ISettingValueProvider { string EntityType { get; } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs index a80ee47ab2..ec6660a870 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs @@ -9,7 +9,7 @@ namespace Volo.Abp.Settings { public class SettingDefinitionManager : ISettingDefinitionManager, ISingletonDependency { - protected Lazy> Providers { get; } + protected Lazy> Providers { get; } protected Lazy> SettingDefinitions { get; } @@ -24,7 +24,7 @@ namespace Volo.Abp.Settings _serviceProvider = serviceProvider; Options = options.Value; - Providers = new Lazy>(CreateSettingProviders, true); + Providers = new Lazy>(CreateSettingProviders, true); SettingDefinitions = new Lazy>(CreateSettingDefinitions, true); } @@ -47,11 +47,11 @@ namespace Volo.Abp.Settings return SettingDefinitions.Value.GetOrDefault(name); } - private List CreateSettingProviders() + private List CreateSettingProviders() { return Options - .Providers - .Select(p => _serviceProvider.GetRequiredService(p) as ISettingProvider) + .DefinitionProviders + .Select(p => _serviceProvider.GetRequiredService(p) as ISettingDefinitionProvider) .ToList(); } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionProvider.cs similarity index 59% rename from src/Volo.Abp.Settings/Volo/Abp/Settings/SettingProvider.cs rename to src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionProvider.cs index bc5a436682..04e6fc7eb2 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionProvider.cs @@ -2,7 +2,7 @@ namespace Volo.Abp.Settings { - public abstract class SettingProvider : ISettingProvider, ISingletonDependency + public abstract class SettingDefinitionProvider : ISettingDefinitionProvider, ISingletonDependency { public abstract void Define(ISettingDefinitionContext context); } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index 76ce80bc4b..f67c1bba00 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -12,7 +12,7 @@ namespace Volo.Abp.Settings { protected ISettingDefinitionManager SettingDefinitionManager { get; } - protected Lazy> Contributors { get; } + protected Lazy> Providers { get; } protected SettingOptions Options { get; } @@ -28,10 +28,10 @@ namespace Volo.Abp.Settings SettingDefinitionManager = settingDefinitionManager; Options = options.Value; - Contributors = new Lazy>( + Providers = new Lazy>( () => Options - .Contributors - .Select(c => serviceProvider.GetRequiredService(c) as ISettingContributor) + .ValueProviders + .Select(c => serviceProvider.GetRequiredService(c) as ISettingValueProvider) .ToList(), true ); @@ -42,7 +42,7 @@ namespace Volo.Abp.Settings Check.NotNull(name, nameof(name)); var setting = SettingDefinitionManager.Get(name); - var contributors = Enumerable.Reverse(Contributors.Value); + var contributors = Enumerable.Reverse(Providers.Value); foreach (var contributor in contributors) { @@ -62,18 +62,18 @@ namespace Volo.Abp.Settings Check.NotNull(entityType, nameof(entityType)); var setting = SettingDefinitionManager.Get(name); - var contributors = Enumerable - .Reverse(Contributors.Value) + var providers = Enumerable + .Reverse(Providers.Value) .SkipWhile(c => c.EntityType != entityType); if (!fallback) { - contributors = contributors.TakeWhile(c => c.EntityType == entityType); + providers = providers.TakeWhile(c => c.EntityType == entityType); } - foreach (var contributor in contributors) + foreach (var provider in providers) { - var value = await contributor.GetOrNullAsync(setting, entityId); + var value = await provider.GetOrNullAsync(setting, entityId); 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 index 5e87bdcf0f..cfc98e58e6 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingOptions.cs @@ -4,17 +4,17 @@ namespace Volo.Abp.Settings { public class SettingOptions { - public ITypeList Providers { get; set; } + public ITypeList DefinitionProviders { get; } - public ITypeList Contributors { get; } + public ITypeList ValueProviders { get; } public SettingOptions() { - Providers = new TypeList(); - Contributors = new TypeList + DefinitionProviders = new TypeList(); + ValueProviders = new TypeList { - typeof(DefaultValueSettingContributor), - typeof(DefaultStoreSettingContributor) + typeof(DefaultValueSettingValueProvider), + typeof(DefaultStoreSettingValueProvider) }; } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs similarity index 70% rename from src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs rename to src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs index ba8129e972..c4ab0bba2b 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs @@ -3,13 +3,13 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Settings { - public abstract class SettingContributor : ISettingContributor, ISingletonDependency + public abstract class SettingValueProvider : ISettingValueProvider, ISingletonDependency { public abstract string EntityType { get; } protected ISettingStore SettingStore { get; } - protected SettingContributor(ISettingStore settingStore) + protected SettingValueProvider(ISettingStore settingStore) { SettingStore = settingStore; } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs index 871aeab2f5..a231bab412 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs @@ -39,7 +39,7 @@ namespace Volo.Abp.Settings services.Configure(options => { - options.Providers.Add(); + options.DefinitionProviders.Add(); }); } 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 a1e5850c23..2cfc147126 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs @@ -24,8 +24,8 @@ namespace Volo.Abp.Settings _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting1", "42")); _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "default-store-value")); - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user1-store-value", UserSettingContributor.DefaultEntityType, User1Id.ToString())); - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user2-store-value", UserSettingContributor.DefaultEntityType, User2Id.ToString())); + _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())); } } } \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingDefinitionProvider.cs similarity index 82% rename from test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs rename to test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingDefinitionProvider.cs index eaa29cd7da..8d26340096 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingDefinitionProvider.cs @@ -1,6 +1,6 @@ namespace Volo.Abp.Settings { - public class TestSettingProvider : SettingProvider + public class TestSettingDefinitionProvider : SettingDefinitionProvider { public override void Define(ISettingDefinitionContext context) { From 387dcae2e21cfb00212184a18aa5256b981827f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 7 Feb 2018 18:04:56 +0300 Subject: [PATCH 11/21] Implemented settingmanager's GetAll --- .../Abp/Settings/ISettingDefinitionManager.cs | 5 +- .../Abp/Settings/SettingDefinitionContext.cs | 6 +++ .../Abp/Settings/SettingDefinitionManager.cs | 6 +++ .../Volo/Abp/Settings/SettingManager.cs | 50 +++++++++++++++++-- .../Volo/Abp/Settings/SettingManager_Tests.cs | 9 ++++ ... => SettingManager_User_GetValue_Tests.cs} | 24 +++++++-- 6 files changed, 92 insertions(+), 8 deletions(-) rename test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/{SettingManager_User_Based_Tests.cs => SettingManager_User_GetValue_Tests.cs} (75%) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs index e8795e283b..176f5b314c 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingDefinitionManager.cs @@ -1,4 +1,5 @@ -using JetBrains.Annotations; +using System.Collections.Generic; +using JetBrains.Annotations; namespace Volo.Abp.Settings { @@ -6,5 +7,7 @@ namespace Volo.Abp.Settings { [NotNull] SettingDefinition Get([NotNull] string name); + + IReadOnlyList GetAll(); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs index 9ab4369b57..f7d993945e 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionContext.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Collections.Immutable; namespace Volo.Abp.Settings { @@ -16,6 +17,11 @@ namespace Volo.Abp.Settings return Settings.GetOrDefault(name); } + public virtual IReadOnlyList GetAll() + { + return Settings.Values.ToImmutableList(); + } + public virtual void Add(params SettingDefinition[] definitions) { if (definitions.IsNullOrEmpty()) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs index ec6660a870..4615d1c147 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinitionManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -42,6 +43,11 @@ namespace Volo.Abp.Settings return settingDefinition; } + public IReadOnlyList GetAll() + { + return SettingDefinitions.Value.Values.ToImmutableList(); + } + public virtual SettingDefinition GetOrNull(string name) { return SettingDefinitions.Value.GetOrDefault(name); diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index f67c1bba00..dd315e48f9 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; @@ -83,14 +84,55 @@ namespace Volo.Abp.Settings return null; } - public virtual Task> GetAllAsync() + public virtual async Task> GetAllAsync() { - throw new System.NotImplementedException(); + var settingValues = new Dictionary(); + var settingDefinitions = SettingDefinitionManager.GetAll(); + + foreach (var provider in Providers.Value) + { + foreach (var setting in settingDefinitions) + { + var value = await provider.GetOrNullAsync(setting, null); + if (value != null) + { + settingValues[setting.Name] = new SettingValue(setting.Name, value); + } + } + } + + return settingValues.Values.ToList(); } - public virtual Task> GetAllAsync(string entityType, string entityId, bool fallback = true) + public virtual async Task> GetAllAsync(string entityType, string entityId, bool fallback = true) { - throw new System.NotImplementedException(); + Check.NotNull(entityType, nameof(entityType)); + + var settingValues = new Dictionary(); + var settingDefinitions = SettingDefinitionManager.GetAll(); + var providers = Enumerable.Reverse(Providers.Value) + .SkipWhile(c => c.EntityType != entityType); + + if (!fallback) + { + providers = providers.TakeWhile(c => c.EntityType == entityType); + } + + providers = providers.Reverse(); + + foreach (var provider in providers) + { + foreach (var setting in settingDefinitions) + { + var value = await provider.GetOrNullAsync(setting, entityId); + if (value != null) + { + settingValues[setting.Name] = new SettingValue(setting.Name, value); + } + } + } + + return settingValues.Values.ToList(); } public virtual Task SetAsync(string name, string value, bool forceToSet = false) 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 1196910994..b690fe3895 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 @@ -33,5 +33,14 @@ namespace Volo.Abp.Settings { (await _settingManager.GetOrNullAsync("MySetting1")).ShouldBe("42"); } + + [Fact] + public async Task Should_Get_All_Base_Store_Values() + { + var settingValues = await _settingManager.GetAllAsync(); + settingValues.ShouldContain(sv => sv.Name == "MySetting1" && sv.Value == "42"); + settingValues.ShouldContain(sv => sv.Name == "MySetting2" && sv.Value == "default-store-value"); + settingValues.ShouldContain(sv => sv.Name == "SettingNotSetInStore" && sv.Value == "default-value"); + } } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_GetValue_Tests.cs similarity index 75% rename from test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs rename to test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_GetValue_Tests.cs index 6c3cc49e0a..650e242d9f 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_GetValue_Tests.cs @@ -8,19 +8,20 @@ using Xunit; namespace Volo.Abp.Settings { - public class SettingManager_User_Based_Tests: AbpSettingsTestBase + public class SettingManager_User_GetValue_Tests: AbpSettingsTestBase { private Guid _currentUserId; + private readonly ISettingManager _settingManager; - public SettingManager_User_Based_Tests() + public SettingManager_User_GetValue_Tests() { _settingManager = GetRequiredService(); } protected override void AfterAddApplication(IServiceCollection services) { - ICurrentUser currentUser = Substitute.For(); + var currentUser = Substitute.For(); currentUser.Id.Returns(ci => _currentUserId); services.AddSingleton(currentUser); } @@ -84,5 +85,22 @@ namespace Volo.Abp.Settings _currentUserId = Guid.NewGuid(); (await _settingManager.GetOrNullForCurrentUserAsync("MySetting2", fallback: false)).ShouldBeNull(); } + + [Fact] + public async Task Should_Get_All_From_Store_For_Given_User() + { + var settingValues = await _settingManager.GetAllForUserAsync(AbpIdentityTestDataBuilder.User1Id); + settingValues.ShouldContain(sv => sv.Name == "MySetting1" && sv.Value == "42"); + settingValues.ShouldContain(sv => sv.Name == "MySetting2" && sv.Value == "user1-store-value"); + settingValues.ShouldContain(sv => sv.Name == "SettingNotSetInStore" && sv.Value == "default-value"); + } + + [Fact] + public async Task Should_Get_All_From_Store_For_Given_User_Without_Fallback() + { + var settingValues = await _settingManager.GetAllForUserAsync(AbpIdentityTestDataBuilder.User1Id, fallback: false); + settingValues.Count.ShouldBe(1); + settingValues.ShouldContain(sv => sv.Name == "MySetting2" && sv.Value == "user1-store-value"); + } } } \ No newline at end of file From 2e4ef14a77d7cb63444a13800732cce7ae6d568e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 8 Feb 2018 11:42:57 +0300 Subject: [PATCH 12/21] Improved setting system. --- .../Abp/Session/UserSettingValueProvider.cs | 13 ++- .../Volo/Abp/Settings/SettingStore.cs | 9 +++ .../DefaultStoreSettingValueProvider.cs | 10 +++ .../DefaultValueSettingValueProvider.cs | 12 +++ .../Volo/Abp/Settings/ISettingStore.cs | 2 + .../Abp/Settings/ISettingValueProvider.cs | 4 + .../Volo/Abp/Settings/NullSettingStore.cs | 29 +++++++ .../Volo/Abp/Settings/SettingManager.cs | 79 ++++++++++++++----- .../Volo/Abp/Settings/SettingValueProvider.cs | 4 + ...Tests.cs => SettingManager_Basic_Tests.cs} | 13 ++- ..._Tests.cs => SettingManager_User_Tests.cs} | 4 +- 11 files changed, 154 insertions(+), 25 deletions(-) create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs rename test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/{SettingManager_Tests.cs => SettingManager_Basic_Tests.cs} (79%) rename test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/{SettingManager_User_GetValue_Tests.cs => SettingManager_User_Tests.cs} (97%) diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs index 39adb5cdd0..56e1992f73 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Volo.Abp.Settings; namespace Volo.Abp.Session @@ -34,5 +33,15 @@ namespace Volo.Abp.Session return await SettingStore.GetOrNullAsync(setting.Name, EntityType, entityId); } + + public override Task SetAsync(SettingDefinition setting, string value, string entityId) + { + return SettingStore.SetAsync(setting.Name, value, EntityType, entityId); + } + + public override Task ClearAsync(SettingDefinition setting, string entityId) + { + return SettingStore.DeleteAsync(setting.Name, EntityType, entityId); + } } } \ 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 5131a71116..231e097775 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs @@ -38,5 +38,14 @@ namespace Volo.Abp.Settings var setting = await _settingRepository.GetListAsync(entityType, entityId); return setting.Select(s => new SettingValue(s.Name, s.Value)).ToList(); } + + public async Task DeleteAsync(string name, string entityType, string entityId) + { + var setting = await _settingRepository.FindAsync(name, entityType, entityId); + if (setting != null) + { + await _settingRepository.DeleteAsync(setting); + } + } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs index 3369e987d2..86166f1eb7 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultStoreSettingValueProvider.cs @@ -15,5 +15,15 @@ namespace Volo.Abp.Settings { 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/DefaultValueSettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs index 09a81028bf..f6d6df3b91 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs @@ -16,5 +16,17 @@ namespace Volo.Abp.Settings { return Task.FromResult(setting.DefaultValue); } + + public override Task SetAsync(SettingDefinition setting, string value, string entityId) + { + setting.DefaultValue = value; + return Task.CompletedTask; + } + + public override Task ClearAsync(SettingDefinition setting, string entityId) + { + setting.DefaultValue = null; + return Task.CompletedTask; + } } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs index 1c4924b1e3..523da0c38c 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs @@ -11,5 +11,7 @@ namespace Volo.Abp.Settings Task SetAsync([NotNull] string name, [NotNull] string value, [CanBeNull] string entityType, [CanBeNull] string entityId); Task> GetListAsync([CanBeNull] string entityType, [CanBeNull] string entityId); + + Task DeleteAsync([NotNull] string name, [CanBeNull]string entityType, [CanBeNull]string entityId); } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs index 05eda04da3..9065b4bba7 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs @@ -8,5 +8,9 @@ namespace Volo.Abp.Settings string EntityType { get; } Task GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string entityId); + + Task SetAsync([NotNull] SettingDefinition setting, [NotNull] string value, [CanBeNull] string entityId); + + Task ClearAsync([NotNull] SettingDefinition setting, [CanBeNull] string entityId); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs new file mode 100644 index 0000000000..898e2d9f51 --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Settings +{ + public class NullSettingStore : ISettingStore, ISingletonDependency + { + public Task GetOrNullAsync(string name, string entityType, string entityId) + { + return Task.FromResult((string) null); + } + + public Task SetAsync(string name, string value, string entityType, string entityId) + { + return Task.CompletedTask; + } + + public Task> GetListAsync(string entityType, string entityId) + { + return Task.FromResult(new List()); + } + + public Task DeleteAsync(string name, string entityType, string entityId) + { + return Task.CompletedTask; + } + } +} \ 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 dd315e48f9..4795c086cb 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -38,34 +38,31 @@ namespace Volo.Abp.Settings ); } - public virtual async Task GetOrNullAsync(string name) + public virtual Task GetOrNullAsync(string name) { Check.NotNull(name, nameof(name)); - var setting = SettingDefinitionManager.Get(name); - var contributors = Enumerable.Reverse(Providers.Value); - - foreach (var contributor in contributors) - { - var value = await contributor.GetOrNullAsync(setting, null); - if (value != null) - { - return value; - } - } - - return null; + return GetOrNullInternalAsync(name, null, null); } - public virtual async Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) + public virtual Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) { Check.NotNull(name, nameof(name)); Check.NotNull(entityType, nameof(entityType)); + + return GetOrNullInternalAsync(name, entityType, entityId, fallback); + } + public virtual async Task GetOrNullInternalAsync(string name, string entityType, string entityId, bool fallback = true) + { var setting = SettingDefinitionManager.Get(name); var providers = Enumerable - .Reverse(Providers.Value) - .SkipWhile(c => c.EntityType != entityType); + .Reverse(Providers.Value); + + if (entityType != null) + { + providers = providers.SkipWhile(c => c.EntityType != entityType); + } if (!fallback) { @@ -137,12 +134,56 @@ namespace Volo.Abp.Settings public virtual Task SetAsync(string name, string value, bool forceToSet = false) { - throw new System.NotImplementedException(); + 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) { - throw new System.NotImplementedException(); + Check.NotNull(name, nameof(name)); + Check.NotNull(entityType, nameof(entityType)); + + return SetInternalAsync(name, value, entityType, entityId, forceToSet); + } + + protected virtual async Task SetInternalAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) + { + var setting = SettingDefinitionManager.Get(name); + + if (!forceToSet) + { + var currentValue = await GetOrNullInternalAsync(name, entityType, entityId); + if (currentValue == value) + { + return; + } + } + + var providers = Enumerable + .Reverse(Providers.Value) + .SkipWhile(p => p.EntityType != entityType) + .ToList(); + + if (!providers.Any()) + { + return; + } + + if (value == null) + { + foreach (var provider in providers) + { + await provider.ClearAsync(setting, entityId); + } + } + else + { + foreach (var provider in providers) + { + await provider.SetAsync(setting, value, entityId); + } + } } } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs index c4ab0bba2b..5af5b45471 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs @@ -15,5 +15,9 @@ namespace Volo.Abp.Settings } public abstract Task GetOrNullAsync(SettingDefinition setting, string entityId); + + public abstract Task SetAsync(SettingDefinition setting, string value, string entityId); + + public abstract Task ClearAsync(SettingDefinition setting, string entityId); } } \ 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_Basic_Tests.cs similarity index 79% rename from test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs rename to test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Basic_Tests.cs index b690fe3895..184fd3bb80 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Basic_Tests.cs @@ -4,11 +4,11 @@ using Xunit; namespace Volo.Abp.Settings { - public class SettingManager_Tests : AbpSettingsTestBase + public class SettingManager_Basic_Tests : AbpSettingsTestBase { private readonly ISettingManager _settingManager; - public SettingManager_Tests() + public SettingManager_Basic_Tests() { _settingManager = GetRequiredService(); } @@ -42,5 +42,14 @@ namespace Volo.Abp.Settings settingValues.ShouldContain(sv => sv.Name == "MySetting2" && sv.Value == "default-store-value"); settingValues.ShouldContain(sv => sv.Name == "SettingNotSetInStore" && sv.Value == "default-value"); } + + [Fact] + public async Task Should_Set_Value() + { + await _settingManager.SetAsync("MySetting1", "43"); + + (await _settingManager.GetOrNullAsync("MySetting1")).ShouldBe("43"); + //TODO: Also check database! + } } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_GetValue_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs similarity index 97% rename from test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_GetValue_Tests.cs rename to test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs index 650e242d9f..af6e7e195d 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_GetValue_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs @@ -8,13 +8,13 @@ using Xunit; namespace Volo.Abp.Settings { - public class SettingManager_User_GetValue_Tests: AbpSettingsTestBase + public class SettingManager_User_Tests: AbpSettingsTestBase { private Guid _currentUserId; private readonly ISettingManager _settingManager; - public SettingManager_User_GetValue_Tests() + public SettingManager_User_Tests() { _settingManager = GetRequiredService(); } 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 13/21] 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! } } } From ab479e90ca260f1d4ef497ed7bc56b19e4d7a12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 08:56:41 +0300 Subject: [PATCH 14/21] Added tests for settings. --- .../Volo/Abp/Settings/SettingManager.cs | 7 +--- .../AbpIdentityApplicationTestBase.cs | 21 +++++++++- .../Abp/Settings/SettingManager_User_Tests.cs | 41 +++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index b90666bfe3..d426661417 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -131,16 +131,11 @@ namespace Volo.Abp.Settings return settingValues.Values.ToList(); } - public virtual Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) + public virtual async Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) { Check.NotNull(name, nameof(name)); Check.NotNull(entityType, nameof(entityType)); - return SetInternalAsync(name, value, entityType, entityId, forceToSet); - } - - protected virtual async Task SetInternalAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) - { var setting = SettingDefinitionManager.Get(name); if (!forceToSet) diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs index 4f2f07fcfb..c8e3d391b9 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs @@ -1,4 +1,6 @@ -using Volo.Abp.TestBase; +using System; +using Volo.Abp.Settings.EntityFrameworkCore; +using Volo.Abp.TestBase; namespace Volo.Abp.Settings { @@ -8,5 +10,22 @@ namespace Volo.Abp.Settings { options.UseAutofac(); } + + protected virtual void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } + + protected virtual T UsingDbContext(Func action) + { + using (var dbContext = GetRequiredService()) + { + return action.Invoke(dbContext); + } + } + } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs index af6e7e195d..a570fbbab7 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using NSubstitute; @@ -102,5 +103,45 @@ namespace Volo.Abp.Settings settingValues.Count.ShouldBe(1); settingValues.ShouldContain(sv => sv.Name == "MySetting2" && sv.Value == "user1-store-value"); } + + [Fact] + public async Task Should_Delete_Setting_Record_When_Set_To_Null() + { + await _settingManager.SetForUserAsync(AbpIdentityTestDataBuilder.User1Id, "MySetting2", null); + + UsingDbContext(context => + { + context.Settings.Count( + s => + s.EntityType == UserSettingValueProvider.DefaultEntityType && + s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && + s.Name == "MySetting2" + ).ShouldBe(0); + }); + } + + [Fact] + public async Task Should_Change_User_Setting() + { + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) + .ShouldBe("user1-store-value"); + + await _settingManager.SetForUserAsync(AbpIdentityTestDataBuilder.User1Id, "MySetting2", "user1-new-store-value"); + + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) + .ShouldBe("user1-new-store-value"); + + UsingDbContext(context => + { + var setting = context.Settings.Single( + s => + s.EntityType == UserSettingValueProvider.DefaultEntityType && + s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && + s.Name == "MySetting2" + ); + + setting.Value.ShouldBe("user1-new-store-value"); + }); + } } } \ No newline at end of file From 8496a258dd915f89ca4ea52b52d1d8c6d6708ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 09:42:38 +0300 Subject: [PATCH 15/21] Added unit tests and fixed the setting manager. --- .../Settings/UserSettingManagerExtensions.cs | 8 +-- .../Volo/Abp/Settings/SettingManager.cs | 23 ++++--- .../AbpIdentityApplicationTestBase.cs | 14 ++++ .../Abp/Settings/SettingManager_User_Tests.cs | 69 +++++++++++++------ 4 files changed, 81 insertions(+), 33 deletions(-) diff --git a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs index 019cf7eadf..569bde8395 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs @@ -28,14 +28,14 @@ namespace Volo.Abp.Settings return settingManager.GetAllAsync(UserSettingValueProvider.DefaultEntityType, null, fallback); } - public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value) + public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) { - return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, userId.ToString()); + return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, userId.ToString(), forceToSet); } - public static Task SetForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value) + public static Task SetForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) { - return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, null); + return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, null, forceToSet); } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index d426661417..ca4108f554 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -138,15 +138,6 @@ namespace Volo.Abp.Settings var setting = SettingDefinitionManager.Get(name); - if (!forceToSet) - { - var currentValue = await GetOrNullInternalAsync(name, entityType, entityId); - if (currentValue == value) - { - return; - } - } - var providers = Enumerable .Reverse(Providers.Value) .SkipWhile(p => p.EntityType != entityType) @@ -157,6 +148,20 @@ namespace Volo.Abp.Settings return; } + //Clear the value if it's same as it's fallback value + if (providers.Count > 1 && !forceToSet && value != null) + { + var fallbackValue = await GetOrNullInternalAsync(name, providers[1].EntityType, entityId); + if (fallbackValue == value) + { + value = null; + } + } + + providers = providers + .TakeWhile(p => p.EntityType == entityType) + .ToList(); + if (value == null) { foreach (var provider in providers) diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs index c8e3d391b9..5077ad8e3c 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.Linq; +using Volo.Abp.Session; using Volo.Abp.Settings.EntityFrameworkCore; using Volo.Abp.TestBase; @@ -27,5 +30,16 @@ namespace Volo.Abp.Settings } } + protected List GetSettingsFromDbContext(string entityType, string entityId, string name) + { + return UsingDbContext(context => + context.Settings.Where( + s => + s.EntityType == UserSettingValueProvider.DefaultEntityType && + s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && + s.Name == "MySetting2" + ).ToList() + ); + } } } diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs index a570fbbab7..49e5201562 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs @@ -109,15 +109,11 @@ namespace Volo.Abp.Settings { await _settingManager.SetForUserAsync(AbpIdentityTestDataBuilder.User1Id, "MySetting2", null); - UsingDbContext(context => - { - context.Settings.Count( - s => - s.EntityType == UserSettingValueProvider.DefaultEntityType && - s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && - s.Name == "MySetting2" - ).ShouldBe(0); - }); + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Count.ShouldBe(0); } [Fact] @@ -131,17 +127,50 @@ namespace Volo.Abp.Settings (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) .ShouldBe("user1-new-store-value"); - UsingDbContext(context => - { - var setting = context.Settings.Single( - s => - s.EntityType == UserSettingValueProvider.DefaultEntityType && - s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && - s.Name == "MySetting2" - ); - - setting.Value.ShouldBe("user1-new-store-value"); - }); + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Single().Value.ShouldBe("user1-new-store-value"); + } + + [Fact] + public async Task Should_Delete_Setting_Record_When_Set_To_Fallback_Value() + { + await _settingManager.SetForUserAsync( + AbpIdentityTestDataBuilder.User1Id, + "MySetting2", + "default-store-value" + ); + + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Count.ShouldBe(0); + + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) + .ShouldBe("default-store-value"); + } + + [Fact] + public async Task Should_Not_Delete_Setting_Record_When_Set_To_Fallback_Value_If_Forced() + { + await _settingManager.SetForUserAsync( + AbpIdentityTestDataBuilder.User1Id, + "MySetting2", + "default-store-value", + forceToSet: true + ); + + GetSettingsFromDbContext( + UserSettingValueProvider.DefaultEntityType, + AbpIdentityTestDataBuilder.User1Id.ToString(), + "MySetting2" + ).Single().Value.ShouldBe("default-store-value"); + + (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) + .ShouldBe("default-store-value"); } } } \ No newline at end of file From e14a3e3654a07eaa727b47ac9a16bc2ab029f64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 09:46:34 +0300 Subject: [PATCH 16/21] Added settings module to the common module. --- src/Volo.Abp/Volo.Abp.csproj | 1 + src/Volo.Abp/Volo/Abp/AbpCommonModule.cs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/Volo.Abp/Volo.Abp.csproj b/src/Volo.Abp/Volo.Abp.csproj index f241708cd0..168637c9e8 100644 --- a/src/Volo.Abp/Volo.Abp.csproj +++ b/src/Volo.Abp/Volo.Abp.csproj @@ -24,6 +24,7 @@ + diff --git a/src/Volo.Abp/Volo/Abp/AbpCommonModule.cs b/src/Volo.Abp/Volo/Abp/AbpCommonModule.cs index ca9b4d9c66..12ea477ed8 100644 --- a/src/Volo.Abp/Volo/Abp/AbpCommonModule.cs +++ b/src/Volo.Abp/Volo/Abp/AbpCommonModule.cs @@ -9,6 +9,7 @@ using Volo.Abp.MultiTenancy; using Volo.Abp.ObjectMapping; using Volo.Abp.Security; using Volo.Abp.Session; +using Volo.Abp.Settings; using Volo.Abp.Validation; using Volo.Abp.VirtualFileSystem; @@ -20,6 +21,7 @@ namespace Volo.Abp [DependsOn(typeof(AbpObjectMappingModule))] [DependsOn(typeof(AbpSecurityModule))] [DependsOn(typeof(AbpSessionModule))] + [DependsOn(typeof(AbpSettingsModule))] [DependsOn(typeof(AbpDataModule))] [DependsOn(typeof(AbpJsonModule))] [DependsOn(typeof(AbpVirtualFileSystemModule))] From e9ad96c510a5a31cd9b0ead3eabd6864ab42a413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 10:20:38 +0300 Subject: [PATCH 17/21] Added IsVisibleToClients, IsInherited and CustomData to the SettingDefinition. --- .../Volo/Abp/Settings/SettingDefinition.cs | 35 +++++++++++++++++-- .../Volo/Abp/Settings/SettingManager.cs | 33 +++++++++++------ .../Settings/SettingManagerSyncExtensions.cs | 8 +++++ .../Settings/AbpIdentityTestDataBuilder.cs | 3 ++ .../Abp/Settings/SettingManager_User_Tests.cs | 11 +++++- .../Settings/TestSettingDefinitionProvider.cs | 1 + 6 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManagerSyncExtensions.cs diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs index f025276b5e..d0ef3247f4 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs @@ -2,15 +2,46 @@ { public class SettingDefinition { + /// + /// Unique name of the setting. + /// public string Name { get; } + /// + /// Default value of the setting. + /// public string DefaultValue { get; set; } - public SettingDefinition(string name, string defaultValue = null) + /// + /// Can clients see this setting and it's value. + /// It maybe dangerous for some settings to be visible to clients (such as email server password). + /// Default: false. + /// + public bool IsVisibleToClients { get; set; } + + /// + /// Is this setting inherited from parent scopes. + /// Default: True. + /// + public bool IsInherited { get; set; } + + /// + /// Can be used to store a custom object related to this setting. + /// + public object CustomData { get; set; } + + public SettingDefinition( + string name, + string defaultValue = null, + bool isVisibleToClients = false, + bool isInherited = true, + object customData = null) { Name = name; - DefaultValue = defaultValue; + IsVisibleToClients = isVisibleToClients; + IsInherited = isInherited; + CustomData = customData; } } } \ 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 ca4108f554..23fff238fc 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -48,7 +48,7 @@ namespace Volo.Abp.Settings { Check.NotNull(name, nameof(name)); Check.NotNull(entityType, nameof(entityType)); - + return GetOrNullInternalAsync(name, entityType, entityId, fallback); } @@ -63,7 +63,7 @@ namespace Volo.Abp.Settings providers = providers.SkipWhile(c => c.EntityType != entityType); } - if (!fallback) + if (!fallback || !setting.IsInherited) { providers = providers.TakeWhile(c => c.EntityType == entityType); } @@ -114,16 +114,29 @@ namespace Volo.Abp.Settings providers = providers.TakeWhile(c => c.EntityType == entityType); } - providers = providers.Reverse(); + var providerList = providers.Reverse().ToList(); - foreach (var provider in providers) + if (providerList.Any()) { foreach (var setting in settingDefinitions) { - var value = await provider.GetOrNullAsync(setting, entityId); - if (value != null) + if (setting.IsInherited) { - settingValues[setting.Name] = new SettingValue(setting.Name, value); + foreach (var provider in providerList) + { + var value = await provider.GetOrNullAsync(setting, entityId); + if (value != null) + { + settingValues[setting.Name] = new SettingValue(setting.Name, value); + } + } + } + else + { + settingValues[setting.Name] = new SettingValue( + setting.Name, + await providerList[0].GetOrNullAsync(setting, entityId) + ); } } } @@ -148,9 +161,9 @@ namespace Volo.Abp.Settings return; } - //Clear the value if it's same as it's fallback value - if (providers.Count > 1 && !forceToSet && value != null) + if (providers.Count > 1 && !forceToSet && setting.IsInherited && value != null) { + //Clear the value if it's same as it's fallback value var fallbackValue = await GetOrNullInternalAsync(name, providers[1].EntityType, entityId); if (fallbackValue == value) { @@ -160,7 +173,7 @@ namespace Volo.Abp.Settings providers = providers .TakeWhile(p => p.EntityType == entityType) - .ToList(); + .ToList(); //Getting list for case of there are more than one provider with same EntityType if (value == null) { diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManagerSyncExtensions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManagerSyncExtensions.cs new file mode 100644 index 0000000000..f9fa53ea4c --- /dev/null +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManagerSyncExtensions.cs @@ -0,0 +1,8 @@ +namespace Volo.Abp.Settings +{ + public static class SettingManagerSyncExtensions + { + //TODO: Add sync extension methods for all setting manager methods. + //TODO: Also add sync extension methods for all value provider extensions (like GlobalSettingManagerExtensions). + } +} 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 06b54bbbd3..5405edf567 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs @@ -26,6 +26,9 @@ namespace Volo.Abp.Settings _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())); + + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySettingWithoutInherit", "default-store-value", GlobalSettingValueProvider.DefaultEntityType)); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySettingWithoutInherit", "user1-store-value", UserSettingValueProvider.DefaultEntityType, User1Id.ToString())); } } } \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs index 49e5201562..621c31f0a9 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs @@ -100,8 +100,9 @@ namespace Volo.Abp.Settings public async Task Should_Get_All_From_Store_For_Given_User_Without_Fallback() { var settingValues = await _settingManager.GetAllForUserAsync(AbpIdentityTestDataBuilder.User1Id, fallback: false); - settingValues.Count.ShouldBe(1); settingValues.ShouldContain(sv => sv.Name == "MySetting2" && sv.Value == "user1-store-value"); + settingValues.ShouldContain(sv => sv.Name == "MySettingWithoutInherit" && sv.Value == "user1-store-value"); + settingValues.ShouldNotContain(sv => sv.Name == "MySetting1"); } [Fact] @@ -172,5 +173,13 @@ namespace Volo.Abp.Settings (await _settingManager.GetOrNullForUserAsync("MySetting2", AbpIdentityTestDataBuilder.User1Id)) .ShouldBe("default-store-value"); } + + [Fact] + public async Task Should_Get_For_Given_User_For_Non_Inherited_Setting() + { + (await _settingManager.GetOrNullForUserAsync("MySettingWithoutInherit", AbpIdentityTestDataBuilder.User1Id)).ShouldBe("user1-store-value"); + (await _settingManager.GetOrNullForUserAsync("MySettingWithoutInherit", AbpIdentityTestDataBuilder.User2Id)).ShouldBeNull(); //Does not inherit! + (await _settingManager.GetOrNullGlobalAsync("MySettingWithoutInherit")).ShouldBe("default-store-value"); + } } } \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingDefinitionProvider.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingDefinitionProvider.cs index 8d26340096..761b383448 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingDefinitionProvider.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingDefinitionProvider.cs @@ -6,6 +6,7 @@ { context.Add(new SettingDefinition("MySetting1")); context.Add(new SettingDefinition("MySetting2")); + context.Add(new SettingDefinition("MySettingWithoutInherit", isInherited: false)); context.Add(new SettingDefinition("SettingNotSetInStore", defaultValue: "default-value")); } } From 7c3860477fd4732875adc6e1f51ad4a5eaa0485d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 10:33:59 +0300 Subject: [PATCH 18/21] Renaming in localization. --- .../Volo/Abp/Localization/IInheritedResourceTypesProvider.cs | 2 +- .../Volo/Abp/Localization/InheritResourceAttribute.cs | 2 +- .../Volo/Abp/Localization/LocalizationResource.cs | 2 +- src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/IInheritedResourceTypesProvider.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/IInheritedResourceTypesProvider.cs index c9e41774cd..ca3325f808 100644 --- a/src/Volo.Abp.Localization/Volo/Abp/Localization/IInheritedResourceTypesProvider.cs +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/IInheritedResourceTypesProvider.cs @@ -6,6 +6,6 @@ namespace Volo.Abp.Localization public interface IInheritedResourceTypesProvider { [NotNull] - Type[] GetInheritedModuleTypes(); + Type[] GetInheritedResourceTypes(); } } \ No newline at end of file diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/InheritResourceAttribute.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/InheritResourceAttribute.cs index 1ee8aa85e9..3783ac0da3 100644 --- a/src/Volo.Abp.Localization/Volo/Abp/Localization/InheritResourceAttribute.cs +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/InheritResourceAttribute.cs @@ -12,7 +12,7 @@ namespace Volo.Abp.Localization ResourceTypes = resourceTypes ?? new Type[0]; } - public Type[] GetInheritedModuleTypes() + public virtual Type[] GetInheritedResourceTypes() { return ResourceTypes; } diff --git a/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs index 82f876d6e0..547035fbaf 100644 --- a/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs +++ b/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResource.cs @@ -82,7 +82,7 @@ namespace Volo.Abp.Localization foreach (var descriptor in descriptors) { - foreach (var baseResourceType in descriptor.GetInheritedModuleTypes()) + foreach (var baseResourceType in descriptor.GetInheritedResourceTypes()) { BaseResourceTypes.AddIfNotContains(baseResourceType); } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs index d0ef3247f4..2de5a690a7 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingDefinition.cs @@ -14,7 +14,7 @@ /// /// Can clients see this setting and it's value. - /// It maybe dangerous for some settings to be visible to clients (such as email server password). + /// It maybe dangerous for some settings to be visible to clients (such as an email server password). /// Default: false. /// public bool IsVisibleToClients { get; set; } From 17b92883276214d6024b381efb3723dee3be06dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 10:39:30 +0300 Subject: [PATCH 19/21] Add ConnectionStringName to AbpSettingsDbContext --- .../Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs index 19150e366b..cc6ed9ce6f 100644 --- a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs @@ -1,8 +1,10 @@ using Microsoft.EntityFrameworkCore; +using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; namespace Volo.Abp.Settings.EntityFrameworkCore { + [ConnectionStringName("AbpSettings")] public class AbpSettingsDbContext : AbpDbContext, IAbpSettingsDbContext { public DbSet Settings { get; set; } From 008f42f08fbf2f5c0328d50df77dcfacaae52b15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 12:56:35 +0300 Subject: [PATCH 20/21] Enhancements on settings. --- Volo.Abp.sln | 7 ++++ .../EntityFrameworkCore/IdentityDbContext.cs | 6 --- .../Abp/Session/UserSettingValueProvider.cs | 20 ++++----- .../Settings/UserSettingManagerExtensions.cs | 12 +++--- .../Volo.Abp.Settings.Domain.Shared.csproj | 20 +++++++++ .../Settings/AbpSettingsDomainSharedModule.cs | 13 ++++++ .../Volo/Abp/Settings/SettingsConsts.cs | 10 +++++ .../Volo.Abp.Settings.Domain.csproj | 1 + .../Volo/Abp/Settings/AbpSettingsConsts.cs | 9 ++++ .../Abp/Settings/AbpSettingsDomainModule.cs | 1 + .../Volo/Abp/Settings/ISettingRepository.cs | 4 +- .../Volo/Abp/Settings/Setting.cs | 12 +++--- .../Volo/Abp/Settings/SettingStore.cs | 18 ++++---- .../AbpSettingsDbContext.cs | 10 +++++ ...SettingsDbContextModelBuilderExtensions.cs | 33 +++++++++++++++ .../EfCoreSettingRepository.cs | 8 ++-- .../DefaultValueSettingValueProvider.cs | 10 +++-- .../GlobalSettingManagerExtensions.cs | 6 +-- .../Settings/GlobalSettingValueProvider.cs | 16 +++---- .../Volo/Abp/Settings/ISettingManager.cs | 6 +-- .../Volo/Abp/Settings/ISettingStore.cs | 8 ++-- .../Abp/Settings/ISettingValueProvider.cs | 8 ++-- .../Volo/Abp/Settings/NullSettingStore.cs | 8 ++-- .../Volo/Abp/Settings/SettingManager.cs | 42 +++++++++---------- .../Volo/Abp/Settings/SettingValueProvider.cs | 8 ++-- .../AbpIdentityApplicationTestBase.cs | 4 +- .../Settings/AbpIdentityTestDataBuilder.cs | 12 +++--- .../Abp/Settings/SettingManager_User_Tests.cs | 8 ++-- 28 files changed, 210 insertions(+), 110 deletions(-) create mode 100644 src/Volo.Abp.Settings.Domain.Shared/Volo.Abp.Settings.Domain.Shared.csproj create mode 100644 src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/AbpSettingsDomainSharedModule.cs create mode 100644 src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/SettingsConsts.cs create mode 100644 src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsConsts.cs create mode 100644 src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContextModelBuilderExtensions.cs diff --git a/Volo.Abp.sln b/Volo.Abp.sln index 2aebb01576..b709a43440 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -254,6 +254,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Settings.Domain", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Abp.Settings.Tests", "test\Volo.Abp.Settings.Tests\Volo.Abp.Settings.Tests.csproj", "{61422EC8-8FFB-48EA-B4B5-527E29077930}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.Settings.Domain.Shared", "src\Volo.Abp.Settings.Domain.Shared\Volo.Abp.Settings.Domain.Shared.csproj", "{27F990E2-6820-4159-B54A-59C8ADED8D28}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -664,6 +666,10 @@ Global {61422EC8-8FFB-48EA-B4B5-527E29077930}.Debug|Any CPU.Build.0 = Debug|Any CPU {61422EC8-8FFB-48EA-B4B5-527E29077930}.Release|Any CPU.ActiveCfg = Release|Any CPU {61422EC8-8FFB-48EA-B4B5-527E29077930}.Release|Any CPU.Build.0 = Release|Any CPU + {27F990E2-6820-4159-B54A-59C8ADED8D28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {27F990E2-6820-4159-B54A-59C8ADED8D28}.Debug|Any CPU.Build.0 = Debug|Any CPU + {27F990E2-6820-4159-B54A-59C8ADED8D28}.Release|Any CPU.ActiveCfg = Release|Any CPU + {27F990E2-6820-4159-B54A-59C8ADED8D28}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -787,6 +793,7 @@ Global {1DCD9600-215A-4A94-AEFD-577568D62633} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {837BC537-3AEC-4F6D-9D64-9704F64D0E1E} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {61422EC8-8FFB-48EA-B4B5-527E29077930} = {37087D1B-3693-4E96-983D-A69F210BDE53} + {27F990E2-6820-4159-B54A-59C8ADED8D28} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5} diff --git a/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs b/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs index 18aa2952c1..03b5dea2af 100644 --- a/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs +++ b/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityDbContext.cs @@ -59,12 +59,6 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } - /// - /// Configures the schema needed for the identity framework. - /// - /// - /// The builder being used to construct the model for this context. - /// protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs index 56e1992f73..87217d3478 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/UserSettingValueProvider.cs @@ -7,9 +7,9 @@ namespace Volo.Abp.Session public class UserSettingValueProvider : SettingValueProvider { - public const string DefaultEntityType = "User"; + public const string ProviderName = "User"; - public override string EntityType => DefaultEntityType; + public override string Name => ProviderName; protected ICurrentUser CurrentUser { get; } @@ -19,29 +19,29 @@ namespace Volo.Abp.Session CurrentUser = currentUser; } - public override async Task GetOrNullAsync(SettingDefinition setting, string entityId) + public override async Task GetOrNullAsync(SettingDefinition setting, string providerKey) { - if (entityId == null) + if (providerKey == null) { if (CurrentUser.Id == null) { return null; } - entityId = CurrentUser.Id.ToString(); + providerKey = CurrentUser.Id.ToString(); } - return await SettingStore.GetOrNullAsync(setting.Name, EntityType, entityId); + return await SettingStore.GetOrNullAsync(setting.Name, Name, providerKey); } - public override Task SetAsync(SettingDefinition setting, string value, string entityId) + public override Task SetAsync(SettingDefinition setting, string value, string providerKey) { - return SettingStore.SetAsync(setting.Name, value, EntityType, entityId); + return SettingStore.SetAsync(setting.Name, value, Name, providerKey); } - public override Task ClearAsync(SettingDefinition setting, string entityId) + public override Task ClearAsync(SettingDefinition setting, string providerKey) { - return SettingStore.DeleteAsync(setting.Name, EntityType, entityId); + return SettingStore.DeleteAsync(setting.Name, Name, providerKey); } } } \ No newline at end of file diff --git a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs index 569bde8395..9387c5c607 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Settings/UserSettingManagerExtensions.cs @@ -10,32 +10,32 @@ namespace Volo.Abp.Settings { public static Task GetOrNullForUserAsync(this ISettingManager settingManager, [NotNull] string name, Guid userId, bool fallback = true) { - return settingManager.GetOrNullAsync(name, UserSettingValueProvider.DefaultEntityType, userId.ToString(), fallback); + return settingManager.GetOrNullAsync(name, UserSettingValueProvider.ProviderName, userId.ToString(), fallback); } public static Task GetOrNullForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, bool fallback = true) { - return settingManager.GetOrNullAsync(name, UserSettingValueProvider.DefaultEntityType, null, fallback); + return settingManager.GetOrNullAsync(name, UserSettingValueProvider.ProviderName, null, fallback); } public static Task> GetAllForUserAsync(this ISettingManager settingManager, Guid userId, bool fallback = true) { - return settingManager.GetAllAsync(UserSettingValueProvider.DefaultEntityType, userId.ToString(), fallback); + return settingManager.GetAllAsync(UserSettingValueProvider.ProviderName, userId.ToString(), fallback); } public static Task> GetAllForCurrentUserAsync(this ISettingManager settingManager, bool fallback = true) { - return settingManager.GetAllAsync(UserSettingValueProvider.DefaultEntityType, null, fallback); + return settingManager.GetAllAsync(UserSettingValueProvider.ProviderName, null, fallback); } public static Task SetForUserAsync(this ISettingManager settingManager, Guid userId, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) { - return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, userId.ToString(), forceToSet); + return settingManager.SetAsync(name, value, UserSettingValueProvider.ProviderName, userId.ToString(), forceToSet); } public static Task SetForCurrentUserAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) { - return settingManager.SetAsync(name, value, UserSettingValueProvider.DefaultEntityType, null, forceToSet); + return settingManager.SetAsync(name, value, UserSettingValueProvider.ProviderName, null, forceToSet); } } } diff --git a/src/Volo.Abp.Settings.Domain.Shared/Volo.Abp.Settings.Domain.Shared.csproj b/src/Volo.Abp.Settings.Domain.Shared/Volo.Abp.Settings.Domain.Shared.csproj new file mode 100644 index 0000000000..1ae08840a7 --- /dev/null +++ b/src/Volo.Abp.Settings.Domain.Shared/Volo.Abp.Settings.Domain.Shared.csproj @@ -0,0 +1,20 @@ + + + + + + netstandard2.0 + Volo.Abp.Settings.Domain.Shared + Volo.Abp.Settings.Domain.Shared + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + false + false + false + + + + + + + + diff --git a/src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/AbpSettingsDomainSharedModule.cs b/src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/AbpSettingsDomainSharedModule.cs new file mode 100644 index 0000000000..fdf5f9e41c --- /dev/null +++ b/src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/AbpSettingsDomainSharedModule.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; + +namespace Volo.Abp.Settings +{ + public class AbpSettingsDomainSharedModule : AbpModule + { + public override void ConfigureServices(IServiceCollection services) + { + services.AddAssemblyOf(); + } + } +} diff --git a/src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/SettingsConsts.cs b/src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/SettingsConsts.cs new file mode 100644 index 0000000000..0b25a74bf8 --- /dev/null +++ b/src/Volo.Abp.Settings.Domain.Shared/Volo/Abp/Settings/SettingsConsts.cs @@ -0,0 +1,10 @@ +namespace Volo.Abp.Settings +{ + public static class SettingsConsts + { + public const int MaxNameLength = 128; + public const int MaxValueLength = 2048; + public const int MaxProviderNameLength = 64; + public const int MaxProviderKeyLength = 64; + } +} 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 425916670d..b980a78f55 100644 --- a/src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj +++ b/src/Volo.Abp.Settings.Domain/Volo.Abp.Settings.Domain.csproj @@ -15,6 +15,7 @@ + diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsConsts.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsConsts.cs new file mode 100644 index 0000000000..f3a9f4653c --- /dev/null +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsConsts.cs @@ -0,0 +1,9 @@ +namespace Volo.Abp.Settings +{ + public static class AbpSettingsConsts + { + public const string DefaultDbTablePrefix = "Abp"; + + public const string DefaultDbSchema = null; + } +} 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 a6edc3f87a..7bbb6659b8 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/AbpSettingsDomainModule.cs @@ -5,6 +5,7 @@ namespace Volo.Abp.Settings { [DependsOn(typeof(AbpSettingsModule))] [DependsOn(typeof(AbpDddModule))] + [DependsOn(typeof(AbpSettingsDomainSharedModule))] public class AbpSettingsDomainModule : AbpModule { public override void ConfigureServices(IServiceCollection services) diff --git a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs index 7c7164204a..5084e51ec5 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/ISettingRepository.cs @@ -7,8 +7,8 @@ namespace Volo.Abp.Settings { public interface ISettingRepository : IBasicRepository { - Task FindAsync(string name, string entityType, string entityId); + Task FindAsync(string name, string providerName, string providerKey); - Task> GetListAsync(string entityType, string entityId); + Task> GetListAsync(string providerName, string providerKey); } } \ No newline at end of file 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 c2a8318fc6..9a04000f50 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/Setting.cs @@ -13,10 +13,10 @@ namespace Volo.Abp.Settings public virtual string Value { get; internal set; } [CanBeNull] - public virtual string EntityType { get; protected set; } + public virtual string ProviderName { get; protected set; } [CanBeNull] - public virtual string EntityId { get; protected set; } + public virtual string ProviderKey { get; protected set; } protected Setting() { @@ -27,8 +27,8 @@ namespace Volo.Abp.Settings Guid id, [NotNull] string name, [NotNull] string value, - [CanBeNull] string entityType = null, - [CanBeNull] string entityId = null) + [CanBeNull] string providerName = null, + [CanBeNull] string providerKey = null) { Check.NotNull(name, nameof(name)); Check.NotNull(value, nameof(value)); @@ -36,8 +36,8 @@ namespace Volo.Abp.Settings Id = id; Name = name; Value = value; - EntityType = entityType; - EntityId = entityId; + ProviderName = providerName; + ProviderKey = providerKey; } } } \ 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 231e097775..a1bb9b3c3d 100644 --- a/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs +++ b/src/Volo.Abp.Settings.Domain/Volo/Abp/Settings/SettingStore.cs @@ -14,18 +14,18 @@ namespace Volo.Abp.Settings _settingRepository = settingRepository; } - public async Task GetOrNullAsync(string name, string entityType, string entityId) + public async Task GetOrNullAsync(string name, string providerName, string providerKey) { - var setting = await _settingRepository.FindAsync(name, entityType, entityId); + var setting = await _settingRepository.FindAsync(name, providerName, providerKey); return setting?.Value; } - public async Task SetAsync(string name, string value, string entityType, string entityId) + public async Task SetAsync(string name, string value, string providerName, string providerKey) { - var setting = await _settingRepository.FindAsync(name, entityType, entityId); + var setting = await _settingRepository.FindAsync(name, providerName, providerKey); if (setting == null) { - setting = new Setting(GuidGenerator.Create(), name, value, entityType, entityId); + setting = new Setting(GuidGenerator.Create(), name, value, providerName, providerKey); await _settingRepository.InsertAsync(setting); } @@ -33,15 +33,15 @@ namespace Volo.Abp.Settings await _settingRepository.UpdateAsync(setting); } - public async Task> GetListAsync(string entityType, string entityId) + public async Task> GetListAsync(string providerName, string providerKey) { - var setting = await _settingRepository.GetListAsync(entityType, entityId); + var setting = await _settingRepository.GetListAsync(providerName, providerKey); return setting.Select(s => new SettingValue(s.Name, s.Value)).ToList(); } - public async Task DeleteAsync(string name, string entityType, string entityId) + public async Task DeleteAsync(string name, string providerName, string providerKey) { - var setting = await _settingRepository.FindAsync(name, entityType, entityId); + var setting = await _settingRepository.FindAsync(name, providerName, providerKey); if (setting != null) { await _settingRepository.DeleteAsync(setting); diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs index cc6ed9ce6f..0ce713d764 100644 --- a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContext.cs @@ -7,6 +7,10 @@ namespace Volo.Abp.Settings.EntityFrameworkCore [ConnectionStringName("AbpSettings")] public class AbpSettingsDbContext : AbpDbContext, IAbpSettingsDbContext { + public static string TablePrefix { get; set; } = AbpSettingsConsts.DefaultDbTablePrefix; + + public static string Schema { get; set; } = AbpSettingsConsts.DefaultDbSchema; + public DbSet Settings { get; set; } public AbpSettingsDbContext(DbContextOptions options) @@ -14,5 +18,11 @@ namespace Volo.Abp.Settings.EntityFrameworkCore { } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + builder.ConfigureAbpSettings(TablePrefix, Schema); + } } } diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContextModelBuilderExtensions.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContextModelBuilderExtensions.cs new file mode 100644 index 0000000000..15b91d3cf7 --- /dev/null +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/AbpSettingsDbContextModelBuilderExtensions.cs @@ -0,0 +1,33 @@ +using JetBrains.Annotations; +using Microsoft.EntityFrameworkCore; + +namespace Volo.Abp.Settings.EntityFrameworkCore +{ + public static class AbpSettingsDbContextModelBuilderExtensions + { + public static void ConfigureAbpSettings( + [NotNull] this ModelBuilder builder, + [CanBeNull] string tablePrefix = AbpSettingsConsts.DefaultDbTablePrefix, + [CanBeNull] string schema = AbpSettingsConsts.DefaultDbSchema) + { + Check.NotNull(builder, nameof(builder)); + + if (tablePrefix == null) + { + tablePrefix = ""; + } + + builder.Entity(b => + { + b.ToTable(tablePrefix + "Settings", schema); + + b.Property(x => x.Name).HasMaxLength(SettingsConsts.MaxNameLength).IsRequired(); + b.Property(x => x.Value).HasMaxLength(SettingsConsts.MaxValueLength).IsRequired(); + b.Property(x => x.ProviderName).HasMaxLength(SettingsConsts.MaxProviderNameLength); + b.Property(x => x.ProviderKey).HasMaxLength(SettingsConsts.MaxProviderKeyLength); + + b.HasIndex(x => new {x.Name, x.ProviderName, x.ProviderKey}); + }); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs index 2073f36269..3f31147390 100644 --- a/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs +++ b/src/Volo.Abp.Settings.EntityFrameworkCore/Volo/Abp/Settings/EntityFrameworkCore/EfCoreSettingRepository.cs @@ -15,14 +15,14 @@ namespace Volo.Abp.Settings.EntityFrameworkCore { } - public async Task FindAsync(string name, string entityType, string entityId) + public async Task FindAsync(string name, string providerName, string providerKey) { - return await DbSet.FirstOrDefaultAsync(s => s.Name == name && s.EntityType == entityType && s.EntityId == entityId); + return await DbSet.FirstOrDefaultAsync(s => s.Name == name && s.ProviderName == providerName && s.ProviderKey == providerKey); } - public async Task> GetListAsync(string entityType, string entityId) + public async Task> GetListAsync(string providerName, string providerKey) { - return await DbSet.Where(s => s.EntityType == entityType && s.EntityId == entityId).ToListAsync(); + return await DbSet.Where(s => s.ProviderName == providerName && s.ProviderKey == providerKey).ToListAsync(); } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs index f6d6df3b91..0d236a4336 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/DefaultValueSettingValueProvider.cs @@ -4,7 +4,9 @@ namespace Volo.Abp.Settings { public class DefaultValueSettingValueProvider : SettingValueProvider { - public override string EntityType => null; + public const string ProviderName = null; + + public override string Name => ProviderName; public DefaultValueSettingValueProvider(ISettingStore settingStore) : base(settingStore) @@ -12,18 +14,18 @@ namespace Volo.Abp.Settings } - public override Task GetOrNullAsync(SettingDefinition setting, string entityId) + public override Task GetOrNullAsync(SettingDefinition setting, string providerKey) { return Task.FromResult(setting.DefaultValue); } - public override Task SetAsync(SettingDefinition setting, string value, string entityId) + public override Task SetAsync(SettingDefinition setting, string value, string providerKey) { setting.DefaultValue = value; return Task.CompletedTask; } - public override Task ClearAsync(SettingDefinition setting, string entityId) + public override Task ClearAsync(SettingDefinition setting, string providerKey) { setting.DefaultValue = null; return Task.CompletedTask; diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs index ce22151a38..d5dabccfe8 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingManagerExtensions.cs @@ -8,17 +8,17 @@ namespace Volo.Abp.Settings { public static Task GetOrNullGlobalAsync(this ISettingManager settingManager, [NotNull] string name, bool fallback = true) { - return settingManager.GetOrNullAsync(name, GlobalSettingValueProvider.DefaultEntityType, null, fallback); + return settingManager.GetOrNullAsync(name, GlobalSettingValueProvider.ProviderName, null, fallback); } public static Task> GetAllGlobalAsync(this ISettingManager settingManager, bool fallback = true) { - return settingManager.GetAllAsync(GlobalSettingValueProvider.DefaultEntityType, null, fallback); + return settingManager.GetAllAsync(GlobalSettingValueProvider.ProviderName, null, fallback); } public static Task SetGlobalAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value) { - return settingManager.SetAsync(name, value, GlobalSettingValueProvider.DefaultEntityType, null); + return settingManager.SetAsync(name, value, GlobalSettingValueProvider.ProviderName, null); } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs index c5c628cffd..78a38cc703 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/GlobalSettingValueProvider.cs @@ -4,28 +4,28 @@ namespace Volo.Abp.Settings { public class GlobalSettingValueProvider : SettingValueProvider { - public const string DefaultEntityType = "Global"; + public const string ProviderName = "Global"; - public override string EntityType => DefaultEntityType; + public override string Name => ProviderName; public GlobalSettingValueProvider(ISettingStore settingStore) : base(settingStore) { } - public override Task GetOrNullAsync(SettingDefinition setting, string entityId) + public override Task GetOrNullAsync(SettingDefinition setting, string providerKey) { - return SettingStore.GetOrNullAsync(setting.Name, EntityType, null); + return SettingStore.GetOrNullAsync(setting.Name, Name, null); } - public override Task SetAsync(SettingDefinition setting, string value, string entityId) + public override Task SetAsync(SettingDefinition setting, string value, string providerKey) { - return SettingStore.SetAsync(setting.Name, value, EntityType, null); + return SettingStore.SetAsync(setting.Name, value, Name, null); } - public override Task ClearAsync(SettingDefinition setting, string entityId) + public override Task ClearAsync(SettingDefinition setting, string providerKey) { - return SettingStore.DeleteAsync(setting.Name, EntityType, null); + return SettingStore.DeleteAsync(setting.Name, Name, 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 4d60e5a61c..9c794a2c9b 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs @@ -8,12 +8,12 @@ namespace Volo.Abp.Settings { Task GetOrNullAsync([NotNull]string name); - Task GetOrNullAsync([NotNull]string name, [NotNull] string entityType, [CanBeNull] string entityId, bool fallback = true); + Task GetOrNullAsync([NotNull]string name, [NotNull] string providerName, [CanBeNull] string providerKey, bool fallback = true); Task> GetAllAsync(); - Task> GetAllAsync([NotNull] string entityType, [CanBeNull] string entityId, bool fallback = true); + Task> GetAllAsync([NotNull] string providerName, [CanBeNull] string providerKey, bool fallback = true); - Task SetAsync([NotNull] string name, [CanBeNull] string value, [NotNull] string entityType, [CanBeNull] string entityId, bool forceToSet = false); + Task SetAsync([NotNull] string name, [CanBeNull] string value, [NotNull] string providerName, [CanBeNull] string providerKey, bool forceToSet = false); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs index 523da0c38c..40ee68f462 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingStore.cs @@ -6,12 +6,12 @@ namespace Volo.Abp.Settings { public interface ISettingStore { - Task GetOrNullAsync([NotNull] string name, [CanBeNull] string entityType, [CanBeNull] string entityId); + Task GetOrNullAsync([NotNull] string name, [CanBeNull] string providerName, [CanBeNull] string providerKey); - Task SetAsync([NotNull] string name, [NotNull] string value, [CanBeNull] string entityType, [CanBeNull] string entityId); + Task SetAsync([NotNull] string name, [NotNull] string value, [CanBeNull] string providerName, [CanBeNull] string providerKey); - Task> GetListAsync([CanBeNull] string entityType, [CanBeNull] string entityId); + Task> GetListAsync([CanBeNull] string providerName, [CanBeNull] string providerKey); - Task DeleteAsync([NotNull] string name, [CanBeNull]string entityType, [CanBeNull]string entityId); + Task DeleteAsync([NotNull] string name, [CanBeNull]string providerName, [CanBeNull]string providerKey); } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs index 9065b4bba7..830546b23d 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs @@ -5,12 +5,12 @@ namespace Volo.Abp.Settings { public interface ISettingValueProvider { - string EntityType { get; } + string Name { get; } - Task GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string entityId); + Task GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey); - Task SetAsync([NotNull] SettingDefinition setting, [NotNull] string value, [CanBeNull] string entityId); + Task SetAsync([NotNull] SettingDefinition setting, [NotNull] string value, [CanBeNull] string providerKey); - Task ClearAsync([NotNull] SettingDefinition setting, [CanBeNull] string entityId); + Task ClearAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey); } } \ No newline at end of file diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs index 898e2d9f51..ade721431d 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/NullSettingStore.cs @@ -6,22 +6,22 @@ namespace Volo.Abp.Settings { public class NullSettingStore : ISettingStore, ISingletonDependency { - public Task GetOrNullAsync(string name, string entityType, string entityId) + public Task GetOrNullAsync(string name, string providerName, string providerKey) { return Task.FromResult((string) null); } - public Task SetAsync(string name, string value, string entityType, string entityId) + public Task SetAsync(string name, string value, string providerName, string providerKey) { return Task.CompletedTask; } - public Task> GetListAsync(string entityType, string entityId) + public Task> GetListAsync(string providerName, string providerKey) { return Task.FromResult(new List()); } - public Task DeleteAsync(string name, string entityType, string entityId) + public Task DeleteAsync(string name, string providerName, string providerKey) { return Task.CompletedTask; } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs index 23fff238fc..fb3f6cca0f 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs @@ -44,33 +44,33 @@ namespace Volo.Abp.Settings return GetOrNullInternalAsync(name, null, null); } - public virtual Task GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true) + public virtual Task GetOrNullAsync(string name, string providerName, string providerKey, bool fallback = true) { Check.NotNull(name, nameof(name)); - Check.NotNull(entityType, nameof(entityType)); + Check.NotNull(providerName, nameof(providerName)); - return GetOrNullInternalAsync(name, entityType, entityId, fallback); + return GetOrNullInternalAsync(name, providerName, providerKey, fallback); } - public virtual async Task GetOrNullInternalAsync(string name, string entityType, string entityId, bool fallback = true) + public virtual async Task GetOrNullInternalAsync(string name, string providerName, string providerKey, bool fallback = true) { var setting = SettingDefinitionManager.Get(name); var providers = Enumerable .Reverse(Providers.Value); - if (entityType != null) + if (providerName != null) { - providers = providers.SkipWhile(c => c.EntityType != entityType); + providers = providers.SkipWhile(c => c.Name != providerName); } if (!fallback || !setting.IsInherited) { - providers = providers.TakeWhile(c => c.EntityType == entityType); + providers = providers.TakeWhile(c => c.Name == providerName); } foreach (var provider in providers) { - var value = await provider.GetOrNullAsync(setting, entityId); + var value = await provider.GetOrNullAsync(setting, providerKey); if (value != null) { return value; @@ -100,18 +100,18 @@ namespace Volo.Abp.Settings return settingValues.Values.ToList(); } - public virtual async Task> GetAllAsync(string entityType, string entityId, bool fallback = true) + public virtual async Task> GetAllAsync(string providerName, string providerKey, bool fallback = true) { - Check.NotNull(entityType, nameof(entityType)); + Check.NotNull(providerName, nameof(providerName)); var settingValues = new Dictionary(); var settingDefinitions = SettingDefinitionManager.GetAll(); var providers = Enumerable.Reverse(Providers.Value) - .SkipWhile(c => c.EntityType != entityType); + .SkipWhile(c => c.Name != providerName); if (!fallback) { - providers = providers.TakeWhile(c => c.EntityType == entityType); + providers = providers.TakeWhile(c => c.Name == providerName); } var providerList = providers.Reverse().ToList(); @@ -124,7 +124,7 @@ namespace Volo.Abp.Settings { foreach (var provider in providerList) { - var value = await provider.GetOrNullAsync(setting, entityId); + var value = await provider.GetOrNullAsync(setting, providerKey); if (value != null) { settingValues[setting.Name] = new SettingValue(setting.Name, value); @@ -135,7 +135,7 @@ namespace Volo.Abp.Settings { settingValues[setting.Name] = new SettingValue( setting.Name, - await providerList[0].GetOrNullAsync(setting, entityId) + await providerList[0].GetOrNullAsync(setting, providerKey) ); } } @@ -144,16 +144,16 @@ namespace Volo.Abp.Settings return settingValues.Values.ToList(); } - public virtual async Task SetAsync(string name, string value, string entityType, string entityId, bool forceToSet = false) + public virtual async Task SetAsync(string name, string value, string providerName, string providerKey, bool forceToSet = false) { Check.NotNull(name, nameof(name)); - Check.NotNull(entityType, nameof(entityType)); + Check.NotNull(providerName, nameof(providerName)); var setting = SettingDefinitionManager.Get(name); var providers = Enumerable .Reverse(Providers.Value) - .SkipWhile(p => p.EntityType != entityType) + .SkipWhile(p => p.Name != providerName) .ToList(); if (!providers.Any()) @@ -164,7 +164,7 @@ namespace Volo.Abp.Settings if (providers.Count > 1 && !forceToSet && setting.IsInherited && value != null) { //Clear the value if it's same as it's fallback value - var fallbackValue = await GetOrNullInternalAsync(name, providers[1].EntityType, entityId); + var fallbackValue = await GetOrNullInternalAsync(name, providers[1].Name, providerKey); if (fallbackValue == value) { value = null; @@ -172,21 +172,21 @@ namespace Volo.Abp.Settings } providers = providers - .TakeWhile(p => p.EntityType == entityType) + .TakeWhile(p => p.Name == providerName) .ToList(); //Getting list for case of there are more than one provider with same EntityType if (value == null) { foreach (var provider in providers) { - await provider.ClearAsync(setting, entityId); + await provider.ClearAsync(setting, providerKey); } } else { foreach (var provider in providers) { - await provider.SetAsync(setting, value, entityId); + await provider.SetAsync(setting, value, providerKey); } } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs index 5af5b45471..dcd98945e4 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/SettingValueProvider.cs @@ -5,7 +5,7 @@ namespace Volo.Abp.Settings { public abstract class SettingValueProvider : ISettingValueProvider, ISingletonDependency { - public abstract string EntityType { get; } + public abstract string Name { get; } protected ISettingStore SettingStore { get; } @@ -14,10 +14,10 @@ namespace Volo.Abp.Settings SettingStore = settingStore; } - public abstract Task GetOrNullAsync(SettingDefinition setting, string entityId); + public abstract Task GetOrNullAsync(SettingDefinition setting, string providerKey); - public abstract Task SetAsync(SettingDefinition setting, string value, string entityId); + public abstract Task SetAsync(SettingDefinition setting, string value, string providerKey); - public abstract Task ClearAsync(SettingDefinition setting, string entityId); + public abstract Task ClearAsync(SettingDefinition setting, string providerKey); } } \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs index 5077ad8e3c..d30568aca4 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestBase.cs @@ -35,8 +35,8 @@ namespace Volo.Abp.Settings return UsingDbContext(context => context.Settings.Where( s => - s.EntityType == UserSettingValueProvider.DefaultEntityType && - s.EntityId == AbpIdentityTestDataBuilder.User1Id.ToString() && + s.ProviderName == UserSettingValueProvider.ProviderName && + s.ProviderKey == AbpIdentityTestDataBuilder.User1Id.ToString() && s.Name == "MySetting2" ).ToList() ); 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 5405edf567..73488df1b4 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs @@ -21,14 +21,14 @@ namespace Volo.Abp.Settings public void Build() { - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting1", "42", GlobalSettingValueProvider.DefaultEntityType)); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting1", "42", GlobalSettingValueProvider.ProviderName)); - _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())); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "default-store-value", GlobalSettingValueProvider.ProviderName)); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user1-store-value", UserSettingValueProvider.ProviderName, User1Id.ToString())); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySetting2", "user2-store-value", UserSettingValueProvider.ProviderName, User2Id.ToString())); - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySettingWithoutInherit", "default-store-value", GlobalSettingValueProvider.DefaultEntityType)); - _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySettingWithoutInherit", "user1-store-value", UserSettingValueProvider.DefaultEntityType, User1Id.ToString())); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySettingWithoutInherit", "default-store-value", GlobalSettingValueProvider.ProviderName)); + _settingRepository.InsertAsync(new Setting(_guidGenerator.Create(), "MySettingWithoutInherit", "user1-store-value", UserSettingValueProvider.ProviderName, User1Id.ToString())); } } } \ No newline at end of file diff --git a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs index 621c31f0a9..767a10bd94 100644 --- a/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs +++ b/test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Tests.cs @@ -111,7 +111,7 @@ namespace Volo.Abp.Settings await _settingManager.SetForUserAsync(AbpIdentityTestDataBuilder.User1Id, "MySetting2", null); GetSettingsFromDbContext( - UserSettingValueProvider.DefaultEntityType, + UserSettingValueProvider.ProviderName, AbpIdentityTestDataBuilder.User1Id.ToString(), "MySetting2" ).Count.ShouldBe(0); @@ -129,7 +129,7 @@ namespace Volo.Abp.Settings .ShouldBe("user1-new-store-value"); GetSettingsFromDbContext( - UserSettingValueProvider.DefaultEntityType, + UserSettingValueProvider.ProviderName, AbpIdentityTestDataBuilder.User1Id.ToString(), "MySetting2" ).Single().Value.ShouldBe("user1-new-store-value"); @@ -145,7 +145,7 @@ namespace Volo.Abp.Settings ); GetSettingsFromDbContext( - UserSettingValueProvider.DefaultEntityType, + UserSettingValueProvider.ProviderName, AbpIdentityTestDataBuilder.User1Id.ToString(), "MySetting2" ).Count.ShouldBe(0); @@ -165,7 +165,7 @@ namespace Volo.Abp.Settings ); GetSettingsFromDbContext( - UserSettingValueProvider.DefaultEntityType, + UserSettingValueProvider.ProviderName, AbpIdentityTestDataBuilder.User1Id.ToString(), "MySetting2" ).Single().Value.ShouldBe("default-store-value"); From 169d019f7db7cd0ce15c5ff7c490462b211cb3cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 9 Feb 2018 13:11:16 +0300 Subject: [PATCH 21/21] Created tenant setting value provider. --- .../Volo.Abp.MultiTenancy.Abstractions.csproj | 1 + .../AbpMultiTenancyAbstractionsModule.cs | 7 +++ .../TenantSettingValueProvider.cs | 45 +++++++++++++++++++ .../TenantSettingManagerExtensions.cs | 41 +++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantSettingValueProvider.cs create mode 100644 src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/Settings/TenantSettingManagerExtensions.cs diff --git a/src/Volo.Abp.MultiTenancy.Abstractions/Volo.Abp.MultiTenancy.Abstractions.csproj b/src/Volo.Abp.MultiTenancy.Abstractions/Volo.Abp.MultiTenancy.Abstractions.csproj index 13c109ad2a..07f95cb1ee 100644 --- a/src/Volo.Abp.MultiTenancy.Abstractions/Volo.Abp.MultiTenancy.Abstractions.csproj +++ b/src/Volo.Abp.MultiTenancy.Abstractions/Volo.Abp.MultiTenancy.Abstractions.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs index 767fd2a6a0..efb8d4cff4 100644 --- a/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs +++ b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/AbpMultiTenancyAbstractionsModule.cs @@ -1,14 +1,21 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Data; using Volo.Abp.Modularity; +using Volo.Abp.Settings; namespace Volo.Abp.MultiTenancy { [DependsOn(typeof(AbpDataModule))] + [DependsOn(typeof(AbpSettingsModule))] public class AbpMultiTenancyAbstractionsModule : AbpModule { public override void ConfigureServices(IServiceCollection services) { + services.Configure(options => + { + options.ValueProviders.Add(); + }); + services.AddAssemblyOf(); } } diff --git a/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantSettingValueProvider.cs b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantSettingValueProvider.cs new file mode 100644 index 0000000000..2f5ff72a6d --- /dev/null +++ b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantSettingValueProvider.cs @@ -0,0 +1,45 @@ +using System.Threading.Tasks; +using Volo.Abp.Settings; + +namespace Volo.Abp.MultiTenancy +{ + public class TenantSettingValueProvider : SettingValueProvider + { + public const string ProviderName = "Tenant"; + + public override string Name => ProviderName; + + protected ICurrentTenant CurrentTenant { get; } + + public TenantSettingValueProvider(ISettingStore settingStore, ICurrentTenant currentTenant) + : base(settingStore) + { + CurrentTenant = currentTenant; + } + + public override async Task GetOrNullAsync(SettingDefinition setting, string providerKey) + { + if (providerKey == null) + { + if (CurrentTenant.Id == null) + { + return null; + } + + providerKey = CurrentTenant.Id.ToString(); + } + + return await SettingStore.GetOrNullAsync(setting.Name, Name, providerKey); + } + + public override Task SetAsync(SettingDefinition setting, string value, string providerKey) + { + return SettingStore.SetAsync(setting.Name, value, Name, providerKey); + } + + public override Task ClearAsync(SettingDefinition setting, string providerKey) + { + return SettingStore.DeleteAsync(setting.Name, Name, providerKey); + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/Settings/TenantSettingManagerExtensions.cs b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/Settings/TenantSettingManagerExtensions.cs new file mode 100644 index 0000000000..f3500bf80a --- /dev/null +++ b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/Settings/TenantSettingManagerExtensions.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.Settings +{ + public static class TenantSettingManagerExtensions + { + public static Task GetOrNullForTenantAsync(this ISettingManager settingManager, [NotNull] string name, Guid tenantId, bool fallback = true) + { + return settingManager.GetOrNullAsync(name, TenantSettingValueProvider.ProviderName, tenantId.ToString(), fallback); + } + + public static Task GetOrNullForCurrentTenantAsync(this ISettingManager settingManager, [NotNull] string name, bool fallback = true) + { + return settingManager.GetOrNullAsync(name, TenantSettingValueProvider.ProviderName, null, fallback); + } + + public static Task> GetAllForTenantAsync(this ISettingManager settingManager, Guid tenantId, bool fallback = true) + { + return settingManager.GetAllAsync(TenantSettingValueProvider.ProviderName, tenantId.ToString(), fallback); + } + + public static Task> GetAllForCurrentTenantAsync(this ISettingManager settingManager, bool fallback = true) + { + return settingManager.GetAllAsync(TenantSettingValueProvider.ProviderName, null, fallback); + } + + public static Task SetForTenantAsync(this ISettingManager settingManager, Guid tenantId, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) + { + return settingManager.SetAsync(name, value, TenantSettingValueProvider.ProviderName, tenantId.ToString(), forceToSet); + } + + public static Task SetForCurrentTenantAsync(this ISettingManager settingManager, [NotNull] string name, [CanBeNull] string value, bool forceToSet = false) + { + return settingManager.SetAsync(name, value, TenantSettingValueProvider.ProviderName, null, forceToSet); + } + } +}