/// <summary> /// Gets the cached field definitions of the entity. /// </summary> /// <typeparam name="TDbConnection">The type of <see cref="IDbConnection"/> object.</typeparam> /// <param name="connection">The instance of the <see cref="IDbConnection"/> object.</param> /// <param name="tableName">The name of the target table.</param> /// <param name="transaction">The transaction object that is currently in used.</param> /// <returns>The cached field definitions of the entity.</returns> internal static IEnumerable <DbField> GetInternal <TDbConnection>(TDbConnection connection, string tableName, IDbTransaction transaction) where TDbConnection : IDbConnection { var type = connection.GetType(); var key = (long)type.FullName.GetHashCode(); var result = (IEnumerable <DbField>)null; // Note: For SqlConnection, the ConnectionString is changing if the (Integrated Security=False). Actually for this isolation, the database name is enough. if (!string.IsNullOrEmpty(connection?.Database)) { key += connection.Database.GetHashCode(); } // Add the hashcode of the table name if (string.IsNullOrEmpty(tableName) == false) { key += tableName.GetHashCode(); } // Try get the value if (m_cache.TryGetValue(key, out result) == false) { var dbHelper = DbHelperMapper.Get(type); result = dbHelper?.GetFields(connection, tableName, transaction); m_cache.TryAdd(key, result); } // Return the value return(result); }
/// <summary> /// Gets the cached field definitions of the entity. /// </summary> /// <param name="type">The type of <see cref="IDbConnection"/> object.</param> /// <param name="connectionString">The connection string to be used.</param> /// <param name="tableName">The name of the target table.</param> /// <returns>The cached field definitions of the entity.</returns> internal static IEnumerable <DbField> Get(Type type, string connectionString, string tableName) { var key = (long)type?.FullName.GetHashCode(); var result = (IEnumerable <DbField>)null; // Set the keys if (string.IsNullOrEmpty(connectionString) == false) { key ^= connectionString.GetHashCode(); } if (string.IsNullOrEmpty(tableName) == false) { key ^= tableName.GetHashCode(); } // Try get the value if (m_cache.TryGetValue(key, out result) == false) { var dbHelper = DbHelperMapper.Get(type); result = dbHelper?.GetFields(connectionString, tableName); m_cache.TryAdd(key, result); } // Return the value return(result); }
/// <summary> /// Gets the cached field definitions of the entity in an asychronous way. /// </summary> /// <typeparam name="TDbConnection">The type of <see cref="IDbConnection"/> object.</typeparam> /// <param name="connection">The instance of the <see cref="IDbConnection"/> object.</param> /// <param name="tableName">The name of the target table.</param> /// <param name="transaction">The transaction object that is currently in used.</param> /// <param name="enableValidation">Enables the validation after retrieving the database fields.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param> /// <returns>The cached field definitions of the entity.</returns> internal static async Task <IEnumerable <DbField> > GetAsyncInternal <TDbConnection>(TDbConnection connection, string tableName, IDbTransaction transaction, bool enableValidation, CancellationToken cancellationToken = default) where TDbConnection : IDbConnection { var type = connection.GetType(); var key = (long)type.GetHashCode(); var result = (IEnumerable <DbField>)null; // Note: For SqlConnection, the ConnectionString is changing if the (Integrated Security=False). Actually for this isolation, the database name is enough. if (!string.IsNullOrWhiteSpace(connection?.Database)) { key += connection.Database.GetHashCode(); } // Add the hashcode of the table name if (string.IsNullOrWhiteSpace(tableName) == false) { key += tableName.GetHashCode(); } // Try get the value if (cache.TryGetValue(key, out result) == false) { // Get from DB var dbHelper = DbHelperMapper.Get(type); result = await dbHelper?.GetFieldsAsync(connection, tableName, transaction, cancellationToken); // Validate if (enableValidation) { ValidateDbFields(tableName, result); } // Add to cache cache.TryAdd(key, result); } // Return the value return(result); }