开发者

Would this memoization be faster than just creating the DbProviderFactory every time?

开发者 https://www.devze.com 2023-03-09 08:05 出处:网络
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.

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>>.

0

精彩评论

暂无评论...
验证码 换一张
取 消