public static void SetPropertyValueFast(this object src, string propertyName, object value, bool failIfPropertyNameNotFound = true) { var adapter = PropertyCallAdapterProvider.GetInstance(src.GetType(), propertyName, failIfPropertyNameNotFound); if (adapter != null && adapter.HasSetter) { adapter.InvokeSet(src, value); } }
public static PropertyInfo[] GetPropertiesFast(this Type forType, BindingFlags bindingFlags, Func <PropertyInfo, bool> selectorFunc = null, string cacheKey = null) { if (cacheKey == null) { return(Resolve()); } return(_CachedProperties.GetOrAdd(cacheKey, x => Resolve())); PropertyInfo[] Resolve() { var props = PropertyCallAdapterProvider.GetProperties(forType, bindingFlags); if (selectorFunc != null) { props = props.Where(selectorFunc).ToArray(); } return(props); } }
public static object GetPropertyValueFast(this object src, string propertyName) { return(PropertyCallAdapterProvider.GetInstance(src.GetType(), propertyName).InvokeGet(src)); }
public void InsertItems <T>(IEnumerable <T> items, string schema, string tableName, IList <ColumnMapping> properties, DbConnection storeConnection, int?batchSize, bool isUpdate = false) { var itemsToInsert = items as T[] ?? items.ToArray(); var con = storeConnection as MySqlConnection; const int buffer = 65536; int batchTimes = batchSize.HasValue ? itemsToInsert.Length / batchSize.Value + 1 : 1; batchSize = batchTimes == 1 ? itemsToInsert.Length : batchSize; for (int i = 0; i < batchTimes; i++) { //1. write to temp csv file. var dbColumns = !isUpdate?properties.Where(x => !x.IsPrimaryKey).ToList() : properties.ToList(); string path = $"{Path.GetTempPath()}{Guid.NewGuid()}.csv"; string columns = string.Join(",", dbColumns.Select(x => x.NameInDatabase)); File.WriteAllText(path, $"{columns}{Environment.NewLine}"); var reflectedObjs = new object[dbColumns.Count]; using (var stream = new StreamWriter(path, true, Encoding.UTF8, buffer)) { foreach (var x in itemsToInsert .Skip(batchSize.GetValueOrDefault() * i) // Arbitrary .Take(batchSize.GetValueOrDefault())) // Batching { for (var index = 0; index < dbColumns.Count; index++) { var columnMapping = dbColumns[index]; reflectedObjs[index] = PropertyCallAdapterProvider <T> .GetInstance(columnMapping.NameOnObject) .InvokeGet(x); var val = reflectedObjs[index]?.ToString(); if (val != null && columnMapping.DataType.Equals("bool", StringComparison.OrdinalIgnoreCase) && (val.Equals("False") || val.Equals("True"))) { reflectedObjs[index] = val.Equals("False") ? 0 : 1; } if (columnMapping.DataType.Equals("varchar", StringComparison.OrdinalIgnoreCase) || columnMapping.DataType.Equals("text", StringComparison.OrdinalIgnoreCase)) { reflectedObjs[index] = $@"""{reflectedObjs[index]}"""; } if (!columnMapping.DataType.Equals("datetime", StringComparison.OrdinalIgnoreCase)) { continue; } var reflectedObj = reflectedObjs[index]; if (reflectedObj != null) { reflectedObjs[index] = ((DateTime)reflectedObj).ToString("yyyy-MM-dd HH:mm:ss.fff"); } } stream.WriteLine(string.Join(",", reflectedObjs)); } } if (con != null && con.State != ConnectionState.Open) { con.Open(); } //2. bulk import mysql from file MySqlBulkLoader loader = new MySqlBulkLoader(con) { TableName = tableName, FieldTerminator = ",", LineTerminator = Environment.NewLine, FileName = path, NumberOfLinesToSkip = 1, FieldQuotationCharacter = '"' }; // we do not know columns a priori // and Columns is readonly - so this. loader.Columns.AddRange(dbColumns.Select(x => x.NameInDatabase).ToList()); loader.Load(); //3. remove temporary file after usage File.Delete(path); } }