Closed MikeAlhayek closed 4 years ago
Glad you like JsonPatch :) There's really no need for the object you want to apply the patch to to be an entity model. Any type of object can be patched. But you'll have to do the mapping yourself - you cannot apply a patch document made for one type of object to another object type.
Another approach you could take is installing the marvin.jsonpatch.dynamic extension package (https://github.com/KevinDockx/JsonPatch.Dynamic). That includes support for non-typed patch docs, which sounds like what you need :)
Or, preferably: switch to .NET Core ;-) The implementation there is based on Marvin.JsonPatch & Marvin.JsonPatch.Dynamic, so it already includes all you need.
Hope this helps! :)
First of all, thank you for this great package!
It seems that
JsonPatchDocument<>
expect an entity-model as a generic type in order to call theApplyTo<>
method to patch the entity-model.This works great for the most part. However, I personally don't like to add validation rules to my entity-model. I rather add validation rules to a view-model/request-model. This way I have one entity-model representing the data being stored, and one entity-model per request representing incoming data from a request. The request-model would contain data annotation validation rules which are specific to the user's request.
Let's take the following code as example. In this example, I pass EntityModel to the
JsonPatchDocument
and the code works perfectly. However, in order for the request to work, you must passEntityModel
as a generic to theJsonPatchDocument<>
method. However, the request must be validated before the data is persisted. This setup requires the user to add validation rules to the entity-model which will mean the same rules for every request!Since I like to keep the validation rules in a request-model, not in entity-model, it would be important to have an
.ApplyTo()
method that would accept anobject
(as source) not as the generic type found in theJsonPatchDocument<>
and ModelState to track errors.So the above example will look like this instead
Will it be possible to add
.ApplyTo<TType>(TType requestModel, ModelState)
or is there a better approach for my request?Here is an example if EntityModel
Here is few examples of different request-models, in the above example
PatchUser
would be an example of RequestModel that I would pass toJsonPatchDocument<PatchUser>