开发者

Would this memoization be faster 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜