コード例 #1
0
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">对象</param>
        /// <param name="fields">更新字段</param>
        /// <param name="idPropertyName">Id字段名</param>
        /// <param name="sameValuePropertyName">检测相同值字段名</param>
        /// <returns></returns>
        public bool Update(T model, string[] fields = null, string idPropertyName = "Id", string sameValuePropertyName = "")
        {
            if (BeginUpdate != null)
            {
                BeginUpdate.Invoke(model);
            }

            PropertyInfo editProperty = model.GetType().GetProperty("EditTime");

            if (editProperty != null)
            {
                editProperty.SetValue(model, DateTime.Now, null);
            }

            PropertyInfo propertyId = model.GetType().GetProperty(idPropertyName);

            if (!string.IsNullOrEmpty(sameValuePropertyName))
            {
                PropertyInfo propertySameValue = model.GetType().GetProperty(sameValuePropertyName);
                string       check             = new ConditionHelper().And(idPropertyName, propertyId.GetValue(model, null), CompareType.Unequal)
                                                 .And(sameValuePropertyName, propertySameValue.GetValue(model, null), CompareType.Equal).ToString();

                DynamicParameters p = new DynamicParameters();
                p.Add($"@{idPropertyName}", propertyId.GetValue(model, null));
                p.Add($"@{sameValuePropertyName}", propertySameValue.GetValue(model, null));

                if (_provider.GetItem <T>(check, p) != null)
                {
                    throw new SameValueException();
                }
            }
            string fieldsStr = "";

            if (fields != null)
            {
                List <string> builder = new List <string>();
                foreach (var field in fields)
                {
                    builder.Add($"{field} = @{field}");
                }
                fieldsStr = string.Join(",", builder.ToArray());
            }
            else
            {
                Type           type      = typeof(T);
                PropertyInfo[] propertys = type.GetProperties();
                List <string>  builder   = new List <string>();
                foreach (var property in propertys)
                {
                    if (property.Name != sameValuePropertyName)
                    {
                        builder.Add($"{property.Name} = @{property.Name}");
                    }
                }
                fieldsStr = string.Join(",", builder);
            }
            string condition = new ConditionHelper().And(idPropertyName, propertyId.GetValue(model, null), CompareType.Equal).ToString();

            if (_provider.Update(condition, fieldsStr, model))
            {
                if (EndUpdate != null)
                {
                    EndUpdate.Invoke(model);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
 public void refreshData()
 {
     dataProvider.Update();
 }