Skip to content

Commit

Permalink
Minor code cleanup for #96
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-roberts committed Jul 8, 2015
1 parent 02a13b3 commit d6a1564
Showing 1 changed file with 85 additions and 44 deletions.
129 changes: 85 additions & 44 deletions src/FeatureToggle.Shared/Providers/BooleanSqlServerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ namespace FeatureToggle.Providers
{
public class BooleanSqlServerProvider : IBooleanToggleValueProvider
{
private const string AppSettingsKeyValuesIsEmptyErrorMessage = "The <appSettings> value for key '{0}' is empty.";
private const string NamedConnectionStringsValueIsEmptyErrorMessage = "The <connectionStrings> value for connection named '{0}' is empty.";

public bool EvaluateBooleanToggleValue(IFeatureToggle toggle)
{
var connectionString = GetConnectionStringFromConfig(toggle);
var sqlCommandText = GetCommandTextFromAppConfig(toggle);


using (var connection = new SqlConnection(connectionString))
{
connection.Open();
Expand All @@ -29,9 +31,6 @@ public bool EvaluateBooleanToggleValue(IFeatureToggle toggle)

private string GetConnectionStringFromConfig(IFeatureToggle toggle)
{
const string emptyConnectionStringsValueError = "The <connectionStrings> value for connection named '{0}' is empty.";
const string emptyValueForAppSettingsKeyError = "The <appSettings> value for key '{0}' is empty.";

var prefixedToggleConfigName = ToggleConfigurationSettings.Prefix + toggle.GetType().Name;
var appSettingsConnectionStringKey = prefixedToggleConfigName + ".ConnectionString";
var appSettingsConnectionStringNameKey = prefixedToggleConfigName + ".ConnectionStringName";
Expand All @@ -40,75 +39,117 @@ private string GetConnectionStringFromConfig(IFeatureToggle toggle)
var isConfiguredViaAppSettingsConnectionStringName = ConfigurationManager.AppSettings.AllKeys.Contains(appSettingsConnectionStringNameKey);
var isConfiguredViaConnectionStrings = ConfigurationManager.ConnectionStrings[prefixedToggleConfigName] != null;

if (CountConnectionStringConfigurations(isConfiguredViaAppSettingsConnectionString,
isConfiguredViaAppSettingsConnectionStringName, isConfiguredViaConnectionStrings) > 1)
ValidateConnectionStringSpecifiedOnlyOnce(isConfiguredViaAppSettingsConnectionString, isConfiguredViaAppSettingsConnectionStringName, isConfiguredViaConnectionStrings, prefixedToggleConfigName);
ValidateConnectionStringNotMissing(isConfiguredViaAppSettingsConnectionString, isConfiguredViaAppSettingsConnectionStringName, isConfiguredViaConnectionStrings, appSettingsConnectionStringKey, appSettingsConnectionStringNameKey, prefixedToggleConfigName);


if (isConfiguredViaAppSettingsConnectionString)
{
throw new ToggleConfigurationError(
string.Format(
"The connection string for '{0}' is configured multiple times.",
prefixedToggleConfigName));
return GetAppSettingsConnectionString(appSettingsConnectionStringKey);
}

if (isConfiguredViaAppSettingsConnectionStringName)
{
return GetConnectionStringFromAppSettingsValueThatPointsToNamedConnectionString(appSettingsConnectionStringNameKey);
}

return GetConnectionStringDirectlyFromNamedConnectionStrings(prefixedToggleConfigName);
}

if (CountConnectionStringConfigurations(isConfiguredViaAppSettingsConnectionString, isConfiguredViaAppSettingsConnectionStringName, isConfiguredViaConnectionStrings) == 0)
private static void ValidateConnectionStringNotMissing(bool isConfiguredViaAppSettingsConnectionString,
bool isConfiguredViaAppSettingsConnectionStringName, bool isConfiguredViaConnectionStrings,
string appSettingsConnectionStringKey, string appSettingsConnectionStringNameKey, string prefixedToggleConfigName)
{
var noConnectionStringConfigured = !isConfiguredViaAppSettingsConnectionString &&
!isConfiguredViaAppSettingsConnectionStringName &&
!isConfiguredViaConnectionStrings;

if (noConnectionStringConfigured)
{
throw new ToggleConfigurationError(
string.Format(
"The connection string was not configured in <appSettings> with a key of '{0}' or '{1}' nor in <connectionStrings> with a name of '{2}'.",
appSettingsConnectionStringKey, appSettingsConnectionStringNameKey, prefixedToggleConfigName));
}
}

string configuredConnectionString = String.Empty;
private void ValidateConnectionStringSpecifiedOnlyOnce(bool isConfiguredViaAppSettingsConnectionString,
bool isConfiguredViaAppSettingsConnectionStringName, bool isConfiguredViaConnectionStrings,
string prefixedToggleConfigName)
{
var connectionStringConfiguredInMultiplePlaces = new[]
{
isConfiguredViaAppSettingsConnectionString,
isConfiguredViaAppSettingsConnectionStringName,
isConfiguredViaConnectionStrings
}.Count(x => x == true) > 1;

if (isConfiguredViaAppSettingsConnectionString)
{
configuredConnectionString = ConfigurationManager.AppSettings[appSettingsConnectionStringKey];

if (string.IsNullOrWhiteSpace(configuredConnectionString))
{
throw new ToggleConfigurationError(string.Format(emptyValueForAppSettingsKeyError, appSettingsConnectionStringKey));
}
}
else if (isConfiguredViaAppSettingsConnectionStringName)
if (connectionStringConfiguredInMultiplePlaces)
{
var connectionStringName = ConfigurationManager.AppSettings[appSettingsConnectionStringNameKey];
throw new ToggleConfigurationError(
string.Format(
"The connection string for '{0}' is configured multiple times.",
prefixedToggleConfigName));
}
}

if (string.IsNullOrWhiteSpace(connectionStringName))
{
throw new ToggleConfigurationError(string.Format(emptyValueForAppSettingsKeyError, appSettingsConnectionStringNameKey));
}

var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionStringName];
private static string GetConnectionStringDirectlyFromNamedConnectionStrings(string prefixedToggleConfigName)
{
var configuredConnectionString = ConfigurationManager.ConnectionStrings[prefixedToggleConfigName].ConnectionString;

if (connectionStringSettings == null)
{
throw new ToggleConfigurationError(string.Format("No entry named '{0}' exists in <connectionStrings>.", connectionStringName));
}
if (string.IsNullOrWhiteSpace(configuredConnectionString))
{
throw new ToggleConfigurationError(string.Format(NamedConnectionStringsValueIsEmptyErrorMessage, prefixedToggleConfigName));
}

configuredConnectionString = connectionStringSettings.ConnectionString;
return configuredConnectionString;
}

if (string.IsNullOrWhiteSpace(configuredConnectionString))
{
throw new ToggleConfigurationError(string.Format(emptyConnectionStringsValueError, connectionStringName));
}
private static string GetConnectionStringFromAppSettingsValueThatPointsToNamedConnectionString(string appSettingsConnectionStringNameKey)
{
var connectionStringName = ConfigurationManager.AppSettings[appSettingsConnectionStringNameKey];

if (string.IsNullOrWhiteSpace(connectionStringName))
{
throw new ToggleConfigurationError(string.Format(AppSettingsKeyValuesIsEmptyErrorMessage,
appSettingsConnectionStringNameKey));
}
else

var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionStringName];

if (connectionStringSettings == null)
{
configuredConnectionString = ConfigurationManager.ConnectionStrings[prefixedToggleConfigName].ConnectionString;
throw new ToggleConfigurationError(string.Format("No entry named '{0}' exists in <connectionStrings>.",
connectionStringName));
}

if (string.IsNullOrWhiteSpace(configuredConnectionString))
{
throw new ToggleConfigurationError(string.Format(emptyConnectionStringsValueError, prefixedToggleConfigName));
}
var configuredConnectionString = connectionStringSettings.ConnectionString;

if (string.IsNullOrWhiteSpace(configuredConnectionString))
{
throw new ToggleConfigurationError(string.Format(NamedConnectionStringsValueIsEmptyErrorMessage,
connectionStringName));
}

return configuredConnectionString;
}

private int CountConnectionStringConfigurations(params bool[] configurations)
private static string GetAppSettingsConnectionString(string appSettingsConnectionStringKey)
{
return configurations.Count(c => c);
var configuredConnectionString = ConfigurationManager.AppSettings[appSettingsConnectionStringKey];

if (string.IsNullOrWhiteSpace(configuredConnectionString))
{
throw new ToggleConfigurationError(string.Format(AppSettingsKeyValuesIsEmptyErrorMessage,
appSettingsConnectionStringKey));
}
return configuredConnectionString;
}



private string GetCommandTextFromAppConfig(IFeatureToggle toggle)
{
var sqlCommandKey = ToggleConfigurationSettings.Prefix + toggle.GetType().Name + ".SqlStatement";
Expand Down

0 comments on commit d6a1564

Please sign in to comment.