Browse Source

Refactored setting system.

pull/204/head
Halil İbrahim Kalkan 9 years ago
parent
commit
1d212e090b
  1. 21
      src/Volo.Abp.Session/Volo/Abp/Session/UserSettingContributor.cs
  2. 5
      src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingContributor.cs
  3. 11
      src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingManager.cs
  4. 4
      src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs
  5. 71
      src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs
  6. 6
      test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityApplicationTestModule.cs
  7. 11
      test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/AbpIdentityTestDataBuilder.cs
  8. 5
      test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_Tests.cs
  9. 57
      test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/SettingManager_User_Based_Tests.cs
  10. 5
      test/Volo.Abp.Settings.Tests/Volo/Abp/Settings/TestSettingProvider.cs

21
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<string> GetOrNullAsync(string name, bool fallback)
public override async Task<string> 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<string> GetOrNullAsync(string name, string entityId, bool fallback = true)
{
return await SettingStore.GetOrNullAsync(name, EntityType, entityId);
return await SettingStore.GetOrNullAsync(setting.Name, EntityType, entityId);
}
}
}

5
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<string> GetOrNullAsync(string name, bool fallback);
Task<string> GetOrNullAsync(string name, string entityId, bool fallback = true);
Task<string> GetOrNullAsync(SettingDefinition setting, [CanBeNull] string entityId);
}
}

11
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<string> GetOrNullAsync(string name, bool fallback = true);
Task<string> GetOrNullAsync([NotNull]string name);
Task<string> GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true);
Task<string> GetOrNullAsync([NotNull]string name, [NotNull] string entityType, string entityId, bool fallback = true);
Task<List<SettingValue>> GetAllAsync();
Task<List<SettingValue>> GetAllAsync(string entityType, string entityId, bool fallback = true);
Task<List<SettingValue>> 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);
}
}

4
src/Volo.Abp.Settings/Volo/Abp/Settings/SettingContributor.cs

@ -14,8 +14,6 @@ namespace Volo.Abp.Settings
SettingStore = settingStore;
}
public abstract Task<string> GetOrNullAsync(string name, bool fallback);
public abstract Task<string> GetOrNullAsync(string name, string entityId, bool fallback = true);
public abstract Task<string> GetOrNullAsync(SettingDefinition setting, string entityId);
}
}

71
src/Volo.Abp.Settings/Volo/Abp/Settings/SettingManager.cs

@ -37,18 +37,15 @@ namespace Volo.Abp.Settings
);
}
public Task<string> GetOrNullAsync(string name, bool fallback = true)
public async Task<string> GetOrNullAsync(string name)
{
return GetOrNullAsync(name, null, null, fallback);
}
Check.NotNull(name, nameof(name));
public async Task<string> 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<string> 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<List<SettingValue>> GetAllAsync()
{
throw new System.NotImplementedException();
return GetAllAsync(null, null);
}
public Task<List<SettingValue>> 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<string> 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<ISettingContributor> GetContributors(string entityType, bool fallback)
private IEnumerable<ISettingContributor> 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)
{

6
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)

11
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()));
}
}
}

5
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");
}
}
}

57
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<ISettingManager>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
ICurrentUser currentUser = Substitute.For<ICurrentUser>();
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();
}
}
}

5
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"));
}
}

Loading…
Cancel
Save