//private static Dictionary<Type, Dictionary<string, Func<object, object>>> _objectGetters = new Dictionary<Type, Dictionary<string, Func<object, object>>>(); //private static Dictionary<Type, Dictionary<string, Action<object, object[]>>> _objectMethods = new Dictionary<Type, Dictionary<string, Action<object, object[]>>>(); //private static Dictionary<Type, Dictionary<string, PropertyInfo>> _objectProperties = new Dictionary<Type, Dictionary<string, PropertyInfo>>(); //private static Dictionary<Type, Dictionary<string, Action<object, object>>> _objectSetters = new Dictionary<Type, Dictionary<string, Action<object, object>>>(); //private static Type _objectType = typeof(object); //public static Dictionary<string, PropertyInfo> GetProperties(Type type) //{ // if (_objectProperties.ContainsKey(type)) // { // return _objectProperties[type]; // } // PropertyInfo[] properties = type.GetProperties(BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance); // Dictionary<string, PropertyInfo> dictionary = new Dictionary<string, PropertyInfo>(); // foreach (PropertyInfo info in properties) // { // dictionary.Add(info.Name, info); // } // _objectProperties.Add(type, dictionary); // return dictionary; //} //public static Dictionary<string, Func<object, object>> GetPropertyGetters(Type objectType) //{ // Type key = objectType; // if (_objectGetters.ContainsKey(key)) // { // return _objectGetters[key]; // } // Dictionary<string, Func<object, object>> dictionary = new Dictionary<string, Func<object, object>>(); // foreach (PropertyInfo info in GetProperties(key).Values) // { // ParameterExpression expression = Expression.Parameter(_objectType); // dictionary.Add(info.Name, Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.MakeMemberAccess(Expression.Convert(expression, info.DeclaringType), info), _objectType), new ParameterExpression[] { expression }).Compile()); // } // _objectGetters.Add(key, dictionary); // return dictionary; //} //public static Dictionary<string, Action<object, object>> GetPropertySetters(Type objectType) //{ // if (_objectSetters.ContainsKey(objectType)) // { // return _objectSetters[objectType]; // } // Dictionary<string, Action<object, object>> dictionary = new Dictionary<string, Action<object, object>>(); // foreach (PropertyInfo info in GetProperties(objectType).Values) // { // ParameterExpression expression = Expression.Parameter(_objectType); // Expression instance = Expression.Convert(expression, objectType); // ParameterExpression expression3 = Expression.Parameter(_objectType); // UnaryExpression expression4 = Expression.Convert(expression3, info.PropertyType); // Action<object, object> action = Expression.Lambda<Action<object, object>>(Expression.Call(instance, info.GetSetMethod(), new Expression[] { expression4 }), new ParameterExpression[] { expression, expression3 }).Compile(); // dictionary.Add(info.Name, action); // } // lock (_objectSetters) // { // _objectSetters.Add(objectType, dictionary); // } // return dictionary; //} public static Dictionary <string, object> GetPropertyValues(object entity) { Dictionary <string, object> dictionary = new Dictionary <string, object>(); Dictionary <string, Func <object, object> > propertyGetters = ExpressionReflector.GetGetters(entity.GetType()); foreach (KeyValuePair <string, Func <object, object> > pair in propertyGetters) { dictionary.Add(pair.Key, pair.Value(entity)); } return(dictionary); }
public static void Map <TSource, TTarget>(TSource source, TTarget target) { Type objectType = typeof(TSource); Type type2 = typeof(TTarget); Dictionary <string, Func <object, object> > propertyGetters = ExpressionReflector.GetGetters(objectType); Dictionary <string, Action <object, object> > propertySetters = ExpressionReflector.GetSetters(type2); foreach (string str in propertyGetters.Keys) { if (propertySetters.ContainsKey(str)) { propertySetters[str](target, propertyGetters[str](source)); } } }
int IEntityOperator.InsertEntities(ArrayList list) { if (list.Count <= 0) { return(0); } var type = list[0].GetType(); var table = TableInfoManager.GetTable(type); var columns = table.Columns; var keyColumn = table.Columns.FirstOrDefault(x => x.Value.IsKey).Value; var count = 0; var maxIndex = 0; bool autoIncreament = keyColumn != null && keyColumn.IsAutoIncreament; //SqlExecutor executor = null; var getters = ExpressionReflector.GetGetters(type); var setters = ExpressionReflector.GetSetters(type); Action <object, object> keySetter = null; if (keyColumn != null) { keySetter = setters.Get(keyColumn.PropertyInfo.Name); } if (!autoIncreament) { var obj = _sqlExecutor.ExecuteScalar(string.Format("select max(Count) from {0} where Name='{1}'", ConfigManager.SequenceTable, table.Name), new Dictionary <string, object>()); if (obj == DBNull.Value) { _sqlExecutor.ExecuteNonQuery(string.Format("insert into {0}(Name,Count) values('{1}',{2})", ConfigManager.SequenceTable, table.Name, 0), new Dictionary <string, object>()); } else { maxIndex = Convert.ToInt32(obj); } } #region 使用Insert语句插入 int page, limit = 10; page = (int)Math.Ceiling(list.Count / (double)limit); int pageIndex = 1; var insertStart = "insert into {0}({1}) values{2}"; var tableName = string.Empty; if (!string.IsNullOrWhiteSpace(table.DataBase)) { tableName = string.Format("[{0}].", table.DataBase); } tableName = string.Format("[{0}]", table.Name); var fields = new List <string>(); var autoincreamentColumn = string.Empty; foreach (var item in table.Columns.Values) { if (item.IsAutoIncreament) { autoincreamentColumn = item.Name; continue; } fields.Add(item.Name); } while (pageIndex <= page) { var start = (pageIndex - 1) * limit; ArrayList entities = null; if (start + limit > list.Count) { entities = list.GetRange(start, list.Count - start); } else { entities = list.GetRange(start, limit); } var values = new List <string>(); var index = 0; var sqlParameters = new Dictionary <string, object>(); foreach (var entity in entities) { var value = new List <string>(); if (!autoIncreament && keySetter != null) { keySetter(entity, ++maxIndex); } foreach (var key in getters.Keys) { if (autoincreamentColumn == key) { continue; } value.Add(string.Format("@{0}{1}", key, index)); var valueParam = getters.Get(key)(entity); var dateValue = valueParam as DateTime?; if (dateValue != null) { if (dateValue.Value.Date == dateValue.Value) { valueParam = dateValue.Value.ToString("yyyy-MM-dd"); } else { valueParam = dateValue.Value.ToString("yyyy-MM-dd HH:mm:ss"); } } sqlParameters.Add(key + index, valueParam); } index++; values.Add(string.Format("({0})", string.Join(",", value))); } insertStart = string.Format(insertStart, tableName, string.Join(",", fields), string.Join(",", values)); count += _sqlExecutor.ExecuteNonQuery(insertStart, sqlParameters); pageIndex++; } #endregion if (!autoIncreament) { _sqlExecutor.ExecuteNonQuery(string.Format("update {0} set [Count]={1} where Name='{2}'", ConfigManager.SequenceTable, maxIndex, table.Name), new Dictionary <string, object>()); } return(count); }
public int SaveChanges() { Dictionary <string, object> dbSets; if (!_dbSets.TryGetValue(_dataContextType, out dbSets)) { throw new Exception("初始有问题"); } var provider = ProviderFactory.CreateProvider(ConfigManager.DataBaseType); var count = 0; using (var scope = new TransactionScope()) { var op = provider.CreateEntityOperator(); foreach (IEntityOperator dbSet in dbSets.Values) { var list = dbSet.GetAdding(); var total = op.InsertEntities(list); if (total != list.Count) { throw new Exception("批量插入失败"); } count += total; var editings = dbSet.GetEditing(); var entityType = dbSet.GetEntityType(); var table = TableInfoManager.GetTable(entityType); var keyColumn = table.Columns.FirstOrDefault(x => x.Value.IsKey).Value; if (keyColumn == null) { throw new InvalidOperationException("实体" + entityType.FullName + "不存在主键,无法更新"); } var getters = ExpressionReflector.GetGetters(entityType); var keyGetter = getters.Get(keyColumn.PropertyInfo.Name); if (keyGetter == null) { throw new InvalidOperationException("keyGetter为null"); } foreach (var editing in editings) { var iGetUpdatedValue = editing as IGetUpdatedValues; if (iGetUpdatedValue == null) { continue; } var values = iGetUpdatedValue.GetUpdatedValues(); if (values.Count <= 0) { continue; } if (values.Get(keyColumn.PropertyInfo.Name) != null) { throw new InvalidOperationException("不允许更新主键"); } var keyValue = keyGetter(editing); values.Add(keyColumn.Name, keyValue); op.UpdateValues(keyColumn, table, values); } var removings = dbSet.GetRemoving(); var ids = new List <int>(); foreach (var removing in removings) { var kv = keyGetter(removing); if (kv == null) { throw new InvalidOperationException("删除时主键必须有值"); } ids.Add(Convert.ToInt32(kv)); } if (ids.Any()) { op.Delete(keyColumn, table, ids.ToArray()); } } scope.Complete(); foreach (IEntityOperator item in dbSets.Values) { item.ClearAdding(); item.ClearEditing(); item.ClearRemoving(); } } return(count); }
int IEntityOperator.InsertEntities(ArrayList list) { if (list.Count <= 0) { return(0); } var type = list[0].GetType(); var table = TableInfoManager.GetTable(type); var columns = table.Columns; var keyColumn = table.Columns.FirstOrDefault(x => x.Value.IsKey).Value; var count = 0; var maxIndex = 0; bool autoIncreament = keyColumn != null && keyColumn.IsAutoIncreament; //SqlExecutor executor = null; var getters = ExpressionReflector.GetGetters(type); var setters = ExpressionReflector.GetSetters(type); Action <object, object> keySetter = null; if (keyColumn != null) { keySetter = setters.Get(keyColumn.PropertyInfo.Name); } if (!autoIncreament) { var obj = _sqlExecutor.ExecuteScalar(string.Format("select max(Count) from {0} where Name='{1}'", ConfigManager.SequenceTable, table.Name), new Dictionary <string, object>()); if (obj == DBNull.Value) { _sqlExecutor.ExecuteNonQuery(string.Format("insert into {0}(Name,Count) values('{1}',{2})", ConfigManager.SequenceTable, table.Name, 0), new Dictionary <string, object>()); } else { maxIndex = Convert.ToInt32(obj); } } if (list.Count <= 10) { #region 使用Insert语句插入 var insertStart = "insert into {0}({1}) values{2}"; var tableName = string.Empty; if (!string.IsNullOrWhiteSpace(table.DataBase)) { tableName = string.Format("[{0}].", table.DataBase); } tableName = string.Format("[{0}]", table.Name); var fields = new List <string>(); var autoincreamentColumn = string.Empty; foreach (var item in table.Columns.Values) { if (item.IsAutoIncreament) { autoincreamentColumn = item.Name; continue; } fields.Add(item.Name); } var values = new List <string>(); var index = 0; var sqlParameters = new Dictionary <string, object>(); foreach (var entity in list) { var value = new List <string>(); if (!autoIncreament && keySetter != null) { keySetter(entity, ++maxIndex); } foreach (var key in getters.Keys) { if (autoincreamentColumn == key) { continue; } value.Add(string.Format("@{0}{1}", key, index)); sqlParameters.Add(key + index, getters.Get(key)(entity)); } index++; values.Add(string.Format("({0})", string.Join(",", value))); } insertStart = string.Format(insertStart, tableName, string.Join(",", fields), string.Join(",", values)); count = _sqlExecutor.ExecuteNonQuery(insertStart, sqlParameters); #endregion } else { #region 使用SqlBulkCopy插入 var sqlBulkCopy = new SqlBulkCopy(DataContext.ConnectionString); sqlBulkCopy.DestinationTableName = "dbo.[" + table.Name + "]"; if (list.Count > 500000) { sqlBulkCopy.BatchSize = list.Count / 10; } var dataTable = new DataTable(); foreach (var column in table.Columns.Values) { var dataColumn = new DataColumn(); dataColumn.ColumnName = column.Name; dataColumn.DataType = TypeHelper.GetUnderlyingType(column.PropertyInfo.PropertyType); dataTable.Columns.Add(dataColumn); sqlBulkCopy.ColumnMappings.Add(column.Name, column.Name); } foreach (var item in list) { var row = dataTable.NewRow(); if (!autoIncreament && keySetter != null) { keySetter(item, ++maxIndex); } foreach (var key in getters.Keys) { row[columns.Get(key).Name] = getters.Get(key)(item); } dataTable.Rows.Add(row); } sqlBulkCopy.WriteToServer(dataTable); sqlBulkCopy.Close(); #endregion count = list.Count; } if (!autoIncreament) { _sqlExecutor.ExecuteNonQuery(string.Format("update {0} set [Count]={1} where Name='{2}'", ConfigManager.SequenceTable, maxIndex, table.Name), new Dictionary <string, object>()); } return(count); }