Closed noufionline closed 5 years ago
Sure :) In your case you could run through that ModifiedProperties list, use reflection to get the actual value of those properties on your DTO, and add a replace operation for each change.
Thanks, @KevinDockx. I have another issue as well. JsonPatchDocument is updating all the values in the child collection items instead of only the changed one. It works perfectly with the Parent object where the actual Apply operation is done. But the child items it replaces every single property with the default value if not in patch document
Hmm, that's strange... do you have a bit of sample code I can use to check?
How to get Replace method's path argument using reflection?
Replace method's signature:
public JsonPatchDocument<TModel> Replace<TProp>(Expression<Func<TModel, TProp>> path, TProp value)
Adding operations to patchDoc:
var patchDoc = new JsonPatchDocument<PointOfInterest>();
foreach (string updatedPropertyName in ModifiedProperties)
{
patchDoc.Replace(path using reflection, value using reflection);
}
Model:
public class PointOfInterest
{
public string Name {set; get;}
public string Description {set; get;}
}
I used following helper function to obtain changed property's path & value:
// Finds changed property's path & value from property's name
// w/ the help of reflection
(Expression<Func<TModel, object>> path, object value) FindOperationArguments<TModel>(TModel model,
string propertyName)
{
// Get info about changed property
PropertyInfo propertyInfo = typeof(TModel).GetProperty(propertyName,
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
// Creates Expression<Func<TModel, object>> for obtaining path
ParameterExpression parameter = Expression.Parameter(typeof(TModel), "p");
MemberExpression property = Expression.Property(parameter, propertyInfo);
Expression<Func<TModel, object>> path = Expression.Lambda<Func<TModel, object>>(property, parameter);
object value = propertyInfo.GetValue(model); // Gets changed property's value
return (path, value);
}
Hi @KevinDockx ,
I am using an open source tool called Trackable Entities, which has a TrackableCollection implementation which gives me the DTO with a collection of Modified Properties.
I want to make a patch document for all the modified properties using reflection. Is it possible?