Geeks With Blogs
Title Of Blog
An application I've been working utilizes the jQuery DataTables plugin and server side processing for pagination.

My initial set up had my controller call a view model which in turn called my business layer to search for some entities. The view model method returned an object (DataTableModel) which encapsulates all of the properties needed for the DataTable plugin including a list of objects/entities. So far so good.

Next step has the controller serializing the DataTableModel to JSON so the DataTable plugin could do it's thing with it. Unfortunately this is where things fell down. Since the search results contained within the DataTableModel are a list of Entity Framework generated POCO objects with all of it's navigation properties the JSON serializer would try to serialized the entire object. Not surprising I suppose, however, I kind of expected it would figure out that if the navigation properties were null it would ignore them and not try to actually load them....but alas no.

Sooooo, my next adventure was figuring out how to attack this problem. The best practice it seems would be to have a view model for anything and everything you'd want to return to the UI whether directly to a view orvia Ajax. I don't necessarily have a problem with this, but at the same time I do not think it unrealistic to want to return POCO objects to the UI either.

Initially I decided to try and return a list of ExpandObjects for the JSON serializer to work with. This would serialize without error, however, the result is not quite what I'd hoped for. The result was the serializer creating key/value pairs for each property in the object vs. actually having an object property with respective value.

In the end I decided to have my Linq query return a dynamic with all of the information needed for display in my table. I then updated my DataTableModel to have a List<dynamic> vs. List<EntityFrameworkPOCO>. This allows the JSON serializer to work without fail.
Posted on Tuesday, March 12, 2013 9:14 AM | Back to top


Comments on this post: Entity Framework POCO and JSON serialization

# re: Entity Framework POCO and JSON serialization
Requesting Gravatar...
My objects are Entity Framework objects, which are overloaded to perform additional business functionality (eg. authentication, etc.) and I do not want to decorate these classes with platform-specific attributes, etc. as I want to present a platform-agnostic API. Thanks for more updates Free Online Commodity Trading Tips
Left by Surya Mansad on Jun 29, 2015 6:24 AM

# re: Entity Framework POCO and JSON serialization
Requesting Gravatar...
I just read it aloud to my husband, who laughed. But strange as it may seem, maybe some editors
web...Technicalstock

http://www.technicalstock.in/
Left by technicalstock on Apr 06, 2016 6:19 AM

# re: Entity Framework POCO and JSON serialization
Requesting Gravatar...
People tend to complicate their own lives, as if living weren't already complicated enough. http://indiavisitonline.in/complete-himachal-tour-package
Left by Aboin apally on Dec 05, 2016 4:01 AM

# re: Entity Framework POCO and JSON serialization
Requesting Gravatar...
Hello everyone,
I am an engineering student in computer sciences. I am in process of passing my graduation project which is related to Poco, c++ library.
What about the data serialiwation on JSON format in c++ using POCO?
Are there some tools to acheive this aim?
I need a help to get a solution to do this.

Thank you in advance,
Best regards,
Left by Abdessattar on Feb 24, 2017 8:29 AM

Your comment:
 (will show your gravatar)


Copyright © Jake Rutherford | Powered by: GeeksWithBlogs.net