/// <summary> /// Checks against the old database version to see which fields/properties have been updated and need to be written to the database /// Note: Any dynamic objects are probably going to get updated every time. It's hard to compare them. /// </summary> /// <returns>UpdateOperation with the necessary updates</returns> internal virtual UpdateOperation GetPendingUpdates() { var pendingUpdates = new UpdateOperation(); var type = this.GetType(); var writeableMemberNames = this.GetWriteableMembers(); List <MemberInfo> fieldsAndProperties = // First collect all the fields and properties type.GetFields().Cast <MemberInfo>() .Concat(type.GetProperties().Cast <MemberInfo>()) // Only include those columns which are explicitly marked as writeable .Where(member => writeableMemberNames.Contains(member.Name)) // Only include the ones which are serialized to JSON .Where(member => member.CustomAttributes.Any(attr => attr.AttributeType.FullName == "Newtonsoft.Json.JsonPropertyAttribute")) .ToList(); // Iterate over and process each automatically handled field/property // to see if its value has been modified since the last time we read/wrote to the database foreach (var memberInfo in fieldsAndProperties) { object oldValue; object newValue; if (memberInfo is FieldInfo) { var fieldInfo = (FieldInfo)memberInfo; oldValue = fieldInfo.GetValue(this.DatabaseVersion); newValue = fieldInfo.GetValue(this); } else { // We know that everything here is supposed to be either a FieldInfo or PropertyInfo, // so if it's not FieldInfo, it should be a propertyInfo var propertyInfo = (PropertyInfo)memberInfo; if (this.DatabaseVersion == null) { oldValue = null; } else { oldValue = propertyInfo.GetValue(this.DatabaseVersion); } newValue = propertyInfo.GetValue(this); } // Record an update if the value has been modified if (!AreObjectsEqual(oldValue, newValue)) { string propertyName = GetMemberJsonName(memberInfo); pendingUpdates.UpdateFieldWithObject(propertyName, newValue); } } return(pendingUpdates); }
public void UpdateOperation_UpdateFieldWithObject_String_SameAsUpdateFieldWithString(string inputValue) { var updateOp1 = new UpdateOperation(); updateOp1.UpdateFieldWithString("field", inputValue); string json1 = updateOp1.ToJson(); var updateOp2 = new UpdateOperation(); updateOp2.UpdateFieldWithObject("field", inputValue); string json2 = updateOp2.ToJson(); Assert.AreEqual(json1, json2); }
public void UpdateOperation_UpdateFieldWithObject_Double_SameAsUpdateFieldWithNumber() { double value = 3.14; var updateOp1 = new UpdateOperation(); updateOp1.UpdateFieldWithNumber("field", value); string json1 = updateOp1.ToJson(); var updateOp2 = new UpdateOperation(); updateOp2.UpdateFieldWithObject("field", value); string json2 = updateOp2.ToJson(); Assert.AreEqual(json1, json2); }