Exemplo n.º 1
0
        /// <summary>
        /// get has modifed values
        /// </summary>
        /// <returns>values</returns>
        internal Dictionary <string, dynamic> GetModifyValues()
        {
            if (_oldEntityData == null || _oldEntityData.PropertyValues == null)
            {
                return(valueDic);
            }
            Dictionary <string, dynamic> modifyValues = new Dictionary <string, dynamic>(valueDic.Count);
            var oldValues    = _oldEntityData.PropertyValues;
            var versionField = QueryConfig.GetVersionField(typeof(T));

            foreach (var valueItem in valueDic)
            {
                if (valueItem.Key != versionField && (!oldValues.ContainsKey(valueItem.Key) || oldValues[valueItem.Key] != valueItem.Value))
                {
                    modifyValues.Add(valueItem.Key, valueItem.Value);
                }
            }
            return(modifyValues);
        }
        /// <summary>
        /// edit data with expression
        /// </summary>
        /// <param name="modifyExpression">modify expression</param>
        /// <param name="query">query object</param>
        /// <returns>ICommand object</returns>
        public virtual ICommand Modify(IModify modifyExpression, IQuery query)
        {
            Dictionary <string, dynamic> fieldAndValues = modifyExpression.GetModifyValues();

            if (fieldAndValues == null || fieldAndValues.Count <= 0)
            {
                return(null);
            }

            #region 版本控制

            string versionFieldName = QueryConfig.GetVersionField(typeof(T));
            if (!string.IsNullOrWhiteSpace(versionFieldName))
            {
                if (!fieldAndValues.ContainsKey(versionFieldName))
                {
                    fieldAndValues.Add(versionFieldName, new CalculateModify()
                    {
                        Calculate = CalculateOperator.Add,
                        Value     = 1
                    });
                }
            }

            #endregion

            #region 更新时间

            string refreshFieldName = QueryConfig.GetRefreshDateField(typeof(T));
            if (!string.IsNullOrWhiteSpace(refreshFieldName))
            {
                if (!fieldAndValues.ContainsKey(refreshFieldName))
                {
                    fieldAndValues.Add(refreshFieldName, DateTime.Now);
                }
            }

            #endregion

            return(Update(fieldAndValues.Keys, fieldAndValues, query));
        }
Exemplo n.º 3
0
        /// <summary>
        /// edit data with expression
        /// </summary>
        /// <param name="modifyExpression">modify expression</param>
        /// <param name="query">query object</param>
        /// <returns>ICommand object</returns>
        public virtual async Task <ICommand> ModifyAsync(IModify modifyExpression, IQuery query)
        {
            Dictionary <string, dynamic> fieldAndValues = modifyExpression.GetModifyValues();

            if (fieldAndValues == null || fieldAndValues.Count <= 0)
            {
                return(null);
            }

            #region control version

            string versionFieldName = QueryConfig.GetVersionField(typeof(T));
            if (!string.IsNullOrWhiteSpace(versionFieldName))
            {
                if (!fieldAndValues.ContainsKey(versionFieldName))
                {
                    fieldAndValues.Add(versionFieldName, new CalculateModify()
                    {
                        Calculate = CalculateOperator.Add,
                        Value     = 1
                    });
                }
            }

            #endregion

            #region update date

            string refreshFieldName = QueryConfig.GetRefreshDateField(typeof(T));
            if (!string.IsNullOrWhiteSpace(refreshFieldName))
            {
                if (!fieldAndValues.ContainsKey(refreshFieldName))
                {
                    fieldAndValues.Add(refreshFieldName, DateTime.Now);
                }
            }

            #endregion

            return(await UpdateAsync(fieldAndValues.Keys, fieldAndValues, query).ConfigureAwait(false));
        }
        /// <summary>
        /// edit data
        /// </summary>
        /// <param name="obj">object</param>
        /// <param name="query">query object</param>
        /// <returns>ICommand object</returns>
        public virtual ICommand Modify(T obj, IQuery query)
        {
            Dictionary <string, dynamic> modifyValues = obj.GetModifyValues();

            if (modifyValues == null || modifyValues.Count <= 0)
            {
                return(null);
            }

            #region 版本控制

            string versionFieldName = QueryConfig.GetVersionField(typeof(T));
            if (!string.IsNullOrWhiteSpace(versionFieldName))
            {
                if (!modifyValues.ContainsKey(versionFieldName))
                {
                    modifyValues.Add(versionFieldName, obj.PropertyValues[versionFieldName] + 1);
                }
                query = query ?? QueryFactory.Create();
                query.And(versionFieldName, CriteriaOperator.Equal, obj.PropertyValues[versionFieldName]);
            }

            #endregion

            #region 更新时间

            string refreshFieldName = QueryConfig.GetRefreshDateField(typeof(T));
            if (!string.IsNullOrWhiteSpace(refreshFieldName))
            {
                if (!modifyValues.ContainsKey(refreshFieldName))
                {
                    modifyValues.Add(refreshFieldName, DateTime.Now);
                }
            }

            #endregion

            return(Update(modifyValues.Keys, modifyValues, query));
        }
        /// <summary>
        /// init version value
        /// </summary>
        internal void InitVersionFieldValue(T obj)
        {
            if (obj == null)
            {
                return;
            }
            string versionField = QueryConfig.GetVersionField(typeof(T));

            if (string.IsNullOrWhiteSpace(versionField))
            {
                return;
            }
            long initValue = 1;

            if (obj.PropertyValues.ContainsKey(versionField))
            {
                obj.PropertyValues[versionField] = initValue;
            }
            else
            {
                obj.PropertyValues.Add(versionField, initValue);
            }
        }