-
Notifications
You must be signed in to change notification settings - Fork 53
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
[Guidance] How to properly deserialize entity graphs coming from the server? #64
Comments
Hi @omatrot, welcome! Let's move these issues over to EntityFrameworkCore.Scaffolding.Handlebars, which is where the code gen takes place. To respond to your items:
This is the default behavior of ASP.NET Core, so there should not be a problem. Have you tried it yet? 2/ What about object references looping and duplicates? Good question. In .NET there is a setting which handles cycles in the graph. On the client, you best bet may the the Dcerialize npm package, which is compatible with Json.Net on the server. |
Hi Tony, I'm @omatrot on Twiter :) Thanks for the amazing work. |
Dcerialize works only with annoted properties.
I assume that entities constructors must be called for the whole tracking to work properly ? |
Can you update the hbs templates to include the annotations?
Yes, you need the ctor to return |
I'm afraid I'll need to do that, will start by manually modifying a few entities though.
I may be wrong, but standard JSON deserialization will not call constructors, so Dcerialize should be used as it will call them. |
The ctor does not need to be called during serialization. Rather it's called in the client (SPA) app to set the |
I'm talking about deserialization on the client indeed. |
You should be fine then. Have you tried and run into problems? |
Right now I'm using standard JSON deserialization on the client (Javascript). I guess I need to use Dcerialize for the constructors to be called and change tracking to work properly |
Ah yes, I believe Dcerialize will help then. Otherwise you would need to call the Proxify method on deserialized entities. |
Calling the proxify method on deserialized entities fails with |
Can you paste the code here? |
|
Does the entity have a constructor? |
Yes:
|
Does web pack remove the constructor? What happens if you add console.log? |
Perhaps you could reproduce the issue with a small GitHub repo? |
The constructor is called, But I think this is my fault because I have es5 instead of es2015 in .tsconfig target compiler option. Didn't you had this problem with your sample ? |
You can’t use the es5 compilation option because proxies are an es2015 feature that cannot be transpiled to es5. My sample does not perform deserialization. But I can modify it to do so. Doesn’t dcerialize solve circular references? |
Yes it does, but here I'm talking about circular references from the angular point of view:
EDIT: My angular module is importing several entities, and I think that this is the source of the problem:
|
Two ways to deal with the Angular warning.
|
I've pulled all the entities in one single ts file and it solved the warning during compilation. This is going to sound crazy, but everyting is fine until I simply add code using Dcerialize (in my case DeserializeArray). With this code that is not even called yet, the angular module complains that one of my entity cannot be used before being instantiated. If I'm using ES5 target, the angular module is OK. EDIT: |
Sounds like your were able to get past the circular dependencies issue by specifying a single .ts output file. This is the same issue as the Angular bug you mentioned. The other issue seems like it is specific to Dcerialize, so maybe open an issue there? It seems to have to do with deserializing entities with constructors. |
Unfortunately, issues are not enabled in the Dcerialize repo :( |
Maybe check these out? https://stackoverflow.com/a/22245105 |
I may be wrong but all are creating simple JSON objects with only properties. |
Ok I've been able to reproduce the problem on your trackable entities sample simply by adding Typescript decorators on entities. I've ben using serializr to successfully deserialize entities. I just need to handle JSON.NET refs by myself and deal with arrays that are not supported right now. I'll report here as soon as I have a working example with my entities. |
serialzr looks promising. Regarding cycles, you might want to consider creating separate entities for serialization that don’t have cyclical references, using something like AutoMapper (see https://medium.com/ps-its-huuti/how-to-get-started-with-automapper-and-asp-net-core-2-ecac60ef523f). It sounds like Json.Net’s ref format isn’t interoperable. |
serializr has its own ref format that is not suitable to my needs. |
Closing this for now, since best practice is to avoid sending entities over the wire that have cycles and use auto-mapper to handle the data transfer. |
Just to be clear, this as nothing to do with circular dependencies. It's more about types being used before being declared. |
Oh yes, was thinking of another issue. 😄 You mentioned JSON refs, but those would go away without JSON.NET preserving references to handle cycles. I imagine using POJO's (Plain Old JavaScript Objects) as DTO's (Data Transfer Objects) would omit a ctor and solve your serialization issue? |
Why not try it with your tackable entities javascript sample augmented with a Web API sending the initial data and receiving the modified version ? |
Good idea to update the sample with an API. Let me see if I can find some time to do that. |
Hi @tonysneed. |
Haven’t had time for this. @lelong37 Does the URF sample use trackable entities? |
Hi @tonysneed. I'll try that in my project and get back to you with results. |
That sounds like great news! If all goes well, why don’t you submit a pull request? |
This works because decorator metata are mandatory for dependency injection in angular <= 7.5. |
I see two potential problems:
1/ Entities on the server are pascal cased on the server while camel cased on the client.
2/ What about object references looping and duplicates?
I suppose the other way around there is something to do too.
The text was updated successfully, but these errors were encountered: