/// <summary> /// By default, use reflection to update an object. If performance is /// unacceptable, this method can be overwritten with an implementation /// that doesn't use reflection. /// </summary> /// <param name="updated">an updated object</param> public static void DefaultUpdate(this ICrudEntity entity, object updated) { foreach (var prop in entity.GetType().GetProperties()) { prop.SetValue(entity, prop.GetValue(updated)); } }
/// <summary> /// By default, use reflection to patch the object. If performance is /// unacceptable, this method can be overwritten with an implementation /// that doesn't use reflection. /// </summary> /// <param name="jsonElement">The updated data as a JsonElement</param> public static void DefaultPatch(this ICrudEntity entity, JsonElement jsonElement, ModelStateDictionary modelState) { var camelCase = false; foreach (var prop in entity.GetType().GetProperties()) { try { if (!camelCase && jsonElement.TryGetProperty(prop.Name, out JsonElement value)) { prop.SetValue(entity, DeserializeJsonValue(prop.PropertyType, value)); } else if (jsonElement.TryGetProperty(CamelCase(prop.Name), out JsonElement value2)) { camelCase = true; prop.SetValue(entity, DeserializeJsonValue(prop.PropertyType, value2)); } } catch (InvalidOperationException ex) { modelState.AddModelError(prop.Name, ex.Message); } } }
public static string DefaultToString(this ICrudEntity entity) { return(JsonSerializer.Serialize(entity)); }