Created
June 14, 2017 14:41
-
-
Save mowensoft/316d2ee0999c2a892f5153684bde278c to your computer and use it in GitHub Desktop.
Windsor dependency extension for connection strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class DependencyEx | |
{ | |
public static Parameter OnConnectionString(string connectionStringName) | |
{ | |
return OnConnectionString("connectionString", connectionStringName); | |
} | |
public static Parameter OnConnectionString(string dependencyName, string connectionStringName) | |
{ | |
if (string.IsNullOrWhiteSpace(connectionStringName)) | |
{ | |
throw new ArgumentNullException("connectionStringName", "\"connectionStringName\" cannot be null or empty"); | |
} | |
var connectionStringSetting = ConfigurationManager.ConnectionStrings[connectionStringName]; | |
if (connectionStringSetting == null) | |
{ | |
throw new ConfigurationErrorsException( | |
$"Could not find a connection string named \"{connectionStringName}\" in the <connectionStrings> section of the config file"); | |
} | |
var connectionString = connectionStringSetting.ConnectionString; | |
return Parameter.ForKey(dependencyName).Eq(connectionString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment