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

Why are owned types required to use ToJson feature? #35259

Closed
ktownsend-personal opened this issue Dec 3, 2024 · 2 comments
Closed

Why are owned types required to use ToJson feature? #35259

ktownsend-personal opened this issue Dec 3, 2024 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ktownsend-personal
Copy link

Question

Why are owned types required to use the ToJson feature?
I suspect this has been asked, but I cannot find any discussion of it.

Scenario

I have a scenario where I am keeping a draft version of an entity as a child property of the entity and stored as JSON. Because the child and the parent are the same type, the property type cannot be owned with OwnsOne or OwnsMany. This is from an MVC5 project I am converting to EF Core 8.

Failed Attempt

I attempted subclassing the entity type so it could be owned, which didn't quite work out. I have two list properties that had to be hidden in the new subclass with new versions of the properties using a subclass of the type in the lists. While it compiled and ran, it created a big complication for my business logic that treats the entity and the draft as the same base type, as it would not see the new properties unless I detect the true type and cast to that type conditionally to reach the right properties. Yuck.

More Questions

I solved my problem by using the tried and true ValueConverter trick, but it left me wondering:

  1. why owned types are required for the ToJson feature in the first place?
  2. why does it matter that the type is used elsewhere in the model?
  3. is this capability planned for a future update?
@ajcvickers
Copy link
Contributor

@ktownsend-personal The answer to all your questions is basically the same--for the most common uses of JSON, owned types are not a good fit. The team recognizes this, which is why we are implementing complex types, which have value-object semantics--and we will then support mapping to JSON from complex types.

@ktownsend-personal
Copy link
Author

@ajcvickers that is excellent news. I am looking forward to that feature. Thanks for the link so I could catch up on the discussions.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2024
@roji roji added the closed-no-further-action The issue is closed and no further action is planned. label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants