Skip to content

Instantly share code, notes, and snippets.

@dealproc
Last active December 14, 2015 15:29
Show Gist options
  • Save dealproc/d0cdf3fbd4a5528c1a70 to your computer and use it in GitHub Desktop.
Save dealproc/d0cdf3fbd4a5528c1a70 to your computer and use it in GitHub Desktop.
app settings helper
public class ApplicationSettingsHelper : IApplicationSettingsHelper
{
readonly IApplicationSettingRepository _applicationSettingRepository;
/// <summary>
/// Default constructor
/// </summary>
public ApplicationSettingsHelper(IApplicationSettingRepository applicationSettingsRepository)
{
_applicationSettingRepository = applicationSettingsRepository;
}
/// <summary>
/// Returns the raw value (string) of an application setting or null if not set
/// </summary>
public string GetSettingValue(ApplicationSettingName settingName, bool throwExceptionNotSet = false)
{
var rawValue = GetRawValue(settingName);
var val = string.IsNullOrWhiteSpace(rawValue) ? null : rawValue;
if (val == null && throwExceptionNotSet)
throw new ApplicationException("Application setting: " + settingName.GetDisplayName() + " does not have a value/undefined.");
return val;
}
/// <summary>
/// Returns a nullable-typed value of an application setting or null if not set (use the string function for strings)
/// </summary>
public T? GetSettingValueNullable<T>(ApplicationSettingName settingName) where T : struct
{
var rawValue = GetRawValue(settingName);
return string.IsNullOrWhiteSpace(rawValue) ? null : (T?)Convert.ChangeType(rawValue, (typeof(T).UnderlyingSystemType));
}
/// <summary>
/// Returns a typed value of an application setting or the default for the struct if not set (use the string function for strings)
/// </summary>
public T GetSettingValue<T>(ApplicationSettingName settingName, bool throwExceptionNotSet = false) where T : struct
{
var val = GetSettingValue(settingName);
return val == null ? new T() : (T)Convert.ChangeType(val, (typeof(T).UnderlyingSystemType));
}
private string GetRawValue(ApplicationSettingName settingName)
{
var defaultValue = settingName.GetCustomAttribute<DefaultValueAttribute>();
//Otherwise, look it up in the db
return _applicationSettingRepository.All().Where(s => s.ApplicationSettingName == settingName).Select(s => s.RawValue).FirstOrDefault()
?? (defaultValue == null ? null : defaultValue.Value.ToString());
}
}
/// <summary>
/// Application Level settings within the system.
/// </summary>
public enum ApplicationSettingName
{
/// <summary>
/// The time, in seconds, that the S3 Client application has to respond to a request.
/// </summary>
[
Display(
Name = "Director Request Timeout",
Description = "The time, in seconds, that the S3 Client application has to respond to a request."),
DataType(DataType.Custom), //
DefaultValue(30)
]
DirectorRequestTimeout = 1,
/// <summary>
/// The maximum size of a file chunk (in bytes) that can be pushed to the broker at a given time.
/// </summary>
[
Display(
Name = "Maximum File Chunk Size",
Description = "The maximum size of a file chunk (in bytes) that can be pushed to the broker at a given time."),
DataType(DataType.Custom),
DefaultValue(1024 * 1024)
]
MaxUploadChunkSize = 2,
/// <summary>
/// The timeout, in seconds, that the S3 Client application has to finish uploading a file.
/// </summary>
[
Display(
Name = "File Upload Request Timeout",
Description = "The timeout, in seconds, that the S3 Client application has to finish uploading a file."),
DataType(DataType.Custom),
DefaultValue(300) // 5 minutes
]
FileUploadRequestTimeout = 3,
[
Display(
Name = "Client Heartbeat Threshold",
Description = "The number of seconds that a client's heartbeat isn't received from the client before the client is considered as offline."
),
DataType(DataType.Custom),
DefaultValue(30)
]
ClientHeartbeatThreshold = 4,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment