mirror of https://github.com/abpframework/abp.git
14 changed files with 200 additions and 7 deletions
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.Settings |
||||
|
{ |
||||
|
public interface ISettingDefinitionContext |
||||
|
{ |
||||
|
SettingDefinition GetOrNull(string name); |
||||
|
|
||||
|
void Add(params SettingDefinition[] definitions); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.Settings |
||||
|
{ |
||||
|
public interface ISettingDefinitionManager |
||||
|
{ |
||||
|
SettingDefinition Get(string name); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.Settings |
||||
|
{ |
||||
|
public interface ISettingProvider |
||||
|
{ |
||||
|
void Define(ISettingDefinitionContext context); |
||||
|
} |
||||
|
} |
||||
@ -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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Settings |
||||
|
{ |
||||
|
public class SettingDefinitionContext : ISettingDefinitionContext |
||||
|
{ |
||||
|
protected Dictionary<string, SettingDefinition> Settings { get; } |
||||
|
|
||||
|
public SettingDefinitionContext(Dictionary<string, SettingDefinition> 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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<List<ISettingProvider>> Providers { get; } |
||||
|
|
||||
|
protected Lazy<IDictionary<string, SettingDefinition>> SettingDefinitions { get; } |
||||
|
|
||||
|
protected SettingOptions Options { get; } |
||||
|
|
||||
|
private readonly IServiceProvider _serviceProvider; |
||||
|
|
||||
|
public SettingDefinitionManager( |
||||
|
IOptions<SettingOptions> options, |
||||
|
IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
_serviceProvider = serviceProvider; |
||||
|
Options = options.Value; |
||||
|
|
||||
|
Providers = new Lazy<List<ISettingProvider>>(CreateSettingProviders, true); |
||||
|
SettingDefinitions = new Lazy<IDictionary<string, SettingDefinition>>(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<ISettingProvider> CreateSettingProviders() |
||||
|
{ |
||||
|
return Options |
||||
|
.Providers |
||||
|
.Select(p => _serviceProvider.GetRequiredService(p) as ISettingProvider) |
||||
|
.ToList(); |
||||
|
} |
||||
|
|
||||
|
private IDictionary<string, SettingDefinition> CreateSettingDefinitions() |
||||
|
{ |
||||
|
var settings = new Dictionary<string, SettingDefinition>(); |
||||
|
|
||||
|
foreach (var provider in Providers.Value) |
||||
|
{ |
||||
|
provider.Define(new SettingDefinitionContext(settings)); |
||||
|
} |
||||
|
|
||||
|
return settings; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Settings |
||||
|
{ |
||||
|
public abstract class SettingProvider : ISettingProvider, ISingletonDependency |
||||
|
{ |
||||
|
public abstract void Define(ISettingDefinitionContext context); |
||||
|
} |
||||
|
} |
||||
@ -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")); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue