Implemented most basic setting manager to get a setting's value.

pull/204/head
Halil İbrahim Kalkan 8 years ago
parent 284ee092a5
commit bda90d6497

@ -15,7 +15,6 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Ddd\Volo.Abp.Ddd.csproj" />
<ProjectReference Include="..\Volo.Abp.MultiTenancy.Abstractions\Volo.Abp.MultiTenancy.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Settings\Volo.Abp.Settings.csproj" />
</ItemGroup>

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

@ -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<Guid>, IMultiTenant
public class Setting : Entity<Guid>
{
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;
}
}
}

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

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

@ -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<string> GetOrNull(string name)
public async Task<string> 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);
}
}

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

@ -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<List<ISettingContributor>> Contributors { get; }
protected SettingOptions Options { get; }
public SettingManager(IOptions<SettingOptions> options, IServiceProvider serviceProvider)
{
Options = options.Value;
Contributors = new Lazy<List<ISettingContributor>>(
() => Options
.Contributors
.Select(c => serviceProvider.GetRequiredService(c) as ISettingContributor)
.ToList(),
true
);
}
public Task<string> GetOrNullAsync(string name)
{
return GetOrNullAsync(name, null, null);
@ -13,11 +34,9 @@ namespace Volo.Abp.Settings
public Task<string> GetOrNullAsync(string name, string entityType, string entityId, bool fallback = true)
{
var contributors = new List<ISettingContributor>();
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;

@ -0,0 +1,17 @@
using Volo.Abp.Collections;
namespace Volo.Abp.Settings
{
public class SettingOptions
{
public ITypeList<ISettingContributor> Contributors { get; }
public SettingOptions()
{
Contributors = new TypeList<ISettingContributor>
{
typeof(DefaultSettingContributor)
};
}
}
}

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

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

Loading…
Cancel
Save