Creating Linq2Sql Proxy Objects

I'm trying to find the most efficient way to send Linq2Sql objects to my jQuery plugins via JSON, preferably without additional code for each class.

EntitySets are the main problem as they not only call recursion, but when recursion is ignored (using JSON.NET ReferenceLoopHandling function) a silly amount of data can be recovered when I really only need 1 or 2 levels. It gets really bad when you talk about users, roles and permissions, when you get the user role, user permissions, role permissions and all users of the role in your JSON before it gets into recursion and stops. Compare that to what I really want is just RoleId.

My initial approach was to send a "simplified" version of the object where I reflect the object and set any EntitySets to null, but of course in the above example Roles are set to null and therefore the RoleId is null. Setting only second level properties to zero works but still too much data as EntitySets that were not killed (first levels) re-populate related tables when the JsonSerializer does its reflection and I still get all these Permission objects that I just don't need.

I definitely don't want to get into the situation of creating a light version of each class and applying the "From" and "To" methods on them, as this is a lot of work and seems wasteful.

Another option is to put the JsonIgnoreAttribute on the appropriate properties, but this will cause a nightmare scenario where the classes need to be re-generated.

My current favorite solution, which I love and hate at the same time, is to turn classes into serialization mode, but since I cannot add attributes to real properties, I would need to create JSON-only properties in the partial class. Again, this seems wasteful, but I think this is the best result.

Any suggestions are greatly appreciated!

0


source to share


1 answer


Have you tried setting Serialization mode in your dbml file?

This is a standard property when generating code, and when you set it to Unidirectional, it will not generate all the extra levels of your table structure. I have used this with silverlight and WCF to send data because data contracts do not allow additional layers to be sent (silverlight is very limited in what you can and cannot do).



Hope this helps!

0


source







All Articles