Would this memoization be faster than just creating the DbProviderFactory every time?
I don't know if the thread locks would be slower than just creating the DbProviderFactory every time.
pub开发者_JAVA百科lic abstract class Connections {
private class Memoization
: ConcurrentDictionary<string, Tuple<string, DbProviderFactory>> { }
static Memoization _connectionInfo = new Memoization();
static DbConnection CreateConnection(string connectionStringSettingsName) {
var tmp = _connectionInfo.GetOrAdd(connectionStringSettingsName, csn => {
var config = ConfigurationManager.ConnectionStrings[csn];
var factory = DbProviderFactories.GetFactory(config.ProviderName);
var connectionString = config.ConnectionString;
return Tuple.Create(connectionString, factory);
});
var conn = tmp.Item2.CreateConnection();
conn.ConnectionString = tmp.Item1;
return conn;
}
}
Update
Memoization
is just a ConcurrentDictionary<string,Tuple<string,DbProviderFactory>>
.
精彩评论