Ejemplo n.º 1
0
        /// <summary>
        /// 检测是否某一项改变
        /// </summary>
        /// <param name="hashModel"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CheckChanged(this IHashDictionary hashModel, string key)
        {
            if (hashModel == null || key == null)
            {
                return(false);
            }

            var hashValues = hashModel.GetHashValues();

            if (!hashValues.ContainsKey(key))
            {
                return(false);
            }
            var hashValue  = hashValues[key];
            var properties = hashModel.GetProperties();

            if (!properties.ContainsKey(key))
            {
                //be removed!
                return(true);
            }

            var sameHash = properties[key].VerifyObjectHash(hashValue.Hash);

            if (!sameHash)
            {
                //hash不一样,一定变化
                return(true);
            }

            //哈希值虽然一样,但只要不是初始版本的,都视为变化(新增)
            var initVersion = hashModel.GetVersions().LastOrDefault();

            return(initVersion != hashValue.Version);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 按照指示决定是否添加值到属性中
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="hashModel"></param>
 /// <param name="key"></param>
 /// <param name="shouldInclude"></param>
 /// <param name="getFunc"></param>
 /// <returns></returns>
 public static IHashDictionary AutoAddOrUpdate <T>(this IHashDictionary hashModel, string key, Func <string, bool> shouldInclude, Func <T> getFunc)
 {
     if (shouldInclude(key))
     {
         var value      = getFunc();
         var properties = hashModel.GetProperties();
         properties[key] = value;
     }
     return(hashModel);
 }