/// <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); }
/// <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); }