这个memoization会不会比每次创建DbProviderFactory更快?

时间:2011-05-31 19:52:14

标签: c# multithreading memoization dbproviderfactories

这个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>>

0 个答案:

没有答案