Taylohtio/GeneralApi/GeneralApi.Core/Services/Impl/SettingsProvider.cs

48 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Taloyhtio.GeneralApi.Common.Extensions;
namespace Taloyhtio.GeneralApi.Core.Services.Impl
{
public class SettingsProvider : ISettingsProvider
{
private IConverter converter;
public SettingsProvider(IConverter converter)
{
this.converter = converter;
}
public T Get<T>(string key)
{
string val = getVal(key);
return this.converter.Convert<string, T>(val);
}
public IEnumerable<T> GetArray<T>(string key)
{
string val = getVal(key);
if (string.IsNullOrEmpty(val))
{
return new List<T>();
}
var vals = val.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
if (vals.IsNullOrEmpty())
{
return new List<T>();
}
var converted = vals.Select(c => this.converter.Convert<string, T>(c));
return converted;
}
private string getVal(string key)
{
return ConfigurationManager.AppSettings[key];
}
}
}