这个memoization会不会比每次创建DbProviderFactory更快?
我不知道线程锁是否比每次创建DbProviderFactory都要慢。
public 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;
}
}
更新
Memoization
只是ConcurrentDictionary<string,Tuple<string,DbProviderFactory>>
。