Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping JsonMergePatchDocument when working with DTO #16

Closed
manifoldhiker opened this issue Jan 14, 2019 · 4 comments
Closed

Mapping JsonMergePatchDocument when working with DTO #16

manifoldhiker opened this issue Jan 14, 2019 · 4 comments

Comments

@manifoldhiker
Copy link

Thank you for this project!

I want to implement a partial update for my domain model, but I don't want to expose this model to the outside world and restrict what fields could be updated. It could be achieved with DTO.

As described in this stackoverflow answer, it is possible to use automapper to map JsonPatchDocument<AccountDTO> to JsonPatchDocument<Account>

It works when map JsonMergePatchDocument.JsonPatchDocument in such way - all operations are mapped.

But when I map JsonMergePatchDocument , operations are not mapped and lost.

I guess, I cant use JsonPatchDocument directly, because it needs to go through ClearAddOperation procedure.

Please, suggest how can I use JsonMergePatchDocument with DTOs and automapper?

@RoboBurned
Copy link

I am working on the same issue now.
I don't like the idea to add AutoMapper just for that reason. There is no reason for adding another layer of transformation. We just need to apply a set of changes described in one object our model.
I would like to have JsonMergePatchDocument.ApplyTo(Account) method. Is it possible?

@Morcatko
Copy link
Owner

I never thought about this use case. ApplyTo is based on duck typing. TModel is there only to have some type safety on the API level, but internally it can apply the operations to any object as long as it has correct property names.
These ideas came to my mind

  1. add ApplyTo(object xxx)
  2. expose JsonPatchDocument - most generic solution, but would still need AutoMapper or similar
  3. same way to cast JsonMergePatchDocument<SomeType> to JsonMergePatchDocument<AnyOtherType> which would expose ApplyTo(AnyOtherType xxx) method. There could even be a helper/extension method that would allow JsonMergePatchDocument<SomeType>.ApplyTo<AnyOtherType>(AnyOtherType xx)

None of these solutions is thread safe, but I guess it is not a big deal since it is parsed from JSON anyway. I personally tend to implement option 3) but let me know if you have any other ideas.

@manifoldhiker
Copy link
Author

Option 3 looks fine.

@Morcatko
Copy link
Owner

Morcatko commented Apr 2, 2019

Implemented in 2.0.0-beta08

@Morcatko Morcatko closed this as completed Apr 2, 2019
@jleck jleck mentioned this issue Jul 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants