Revised setting system.

pull/204/head
Halil İbrahim Kalkan 7 years ago
parent 68b88ff6a8
commit 99d6556a16

@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" />
<ProjectReference Include="..\Volo.Abp.Security\Volo.Abp.Security.csproj" />
<ProjectReference Include="..\Volo.Abp.Settings\Volo.Abp.Settings.csproj" />
</ItemGroup>
</Project>

@ -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<SettingOptions>(options =>
{
options.Contributors.Add<UserSettingContributor>();
});
services.AddAssemblyOf<AbpSessionModule>();
}
}

@ -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<string> 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<string> GetOrNullAsync(string name, string entityId, bool fallback = true)
{
return await SettingStore.GetOrNullAsync(name, EntityType, entityId);
}
}
}

@ -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<string> GetOrNullForUserAsync(this ISettingManager settingManager, [NotNull] string name, Guid userId, bool fallback = true)
{
return settingManager.GetOrNullAsync(name, UserSettingContributor.DefaultEntityType, userId.ToString(), fallback);
}
public static Task<List<SettingValue>> 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());
}
}
}

@ -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<string> 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);
}
}
}

@ -4,6 +4,10 @@ namespace Volo.Abp.Settings
{
public interface ISettingContributor
{
Task<string> GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true);
string EntityType { get; }
Task<string> GetOrNullAsync(string name, bool fallback);
Task<string> GetOrNullAsync(string name, string entityId, bool fallback = true);
}
}

@ -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);
}
}

@ -5,7 +5,7 @@ namespace Volo.Abp.Settings
{
public interface ISettingManager
{
Task<string> GetOrNullAsync(string name);
Task<string> GetOrNullAsync(string name, bool fallback = true);
Task<string> GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true);

@ -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<string> GetOrNullAsync(string name, bool fallback);
public abstract Task<string> GetOrNullAsync(string name, string entityId, bool fallback = true);
}
}

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

@ -16,11 +16,15 @@ namespace Volo.Abp.Settings
protected SettingOptions Options { get; }
protected ISettingStore SettingStore { get; }
public SettingManager(
IOptions<SettingOptions> options,
IServiceProvider serviceProvider,
ISettingDefinitionManager settingDefinitionManager)
IOptions<SettingOptions> options,
IServiceProvider serviceProvider,
ISettingDefinitionManager settingDefinitionManager,
ISettingStore settingStore)
{
SettingStore = settingStore;
SettingDefinitionManager = settingDefinitionManager;
Options = options.Value;
@ -33,27 +37,38 @@ namespace Volo.Abp.Settings
);
}
public Task<string> GetOrNullAsync(string name)
public Task<string> GetOrNullAsync(string name, bool fallback = true)
{
return GetOrNullAsync(name, null, null);
return GetOrNullAsync(name, null, null, fallback);
}
public async Task<string> 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<List<SettingValue>> GetAllAsync()
{
throw new System.NotImplementedException();
@ -73,5 +88,34 @@ namespace Volo.Abp.Settings
{
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)
{
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;
}
}
}

@ -10,11 +10,7 @@ namespace Volo.Abp.Settings
public SettingOptions()
{
Contributors = new TypeList<ISettingContributor>
{
typeof(DefaultSettingContributor)
};
Contributors = new TypeList<ISettingContributor>();
Providers = new TypeList<ISettingProvider>();
}
}

Loading…
Cancel
Save