Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON based architecture, how best to synchronise front end and back end

Tags:

json

c#

Looking at using JSON for a new .net REST based system and it looks great, but there is one thing thats alluding my understanding. What is the strategy for keeping the JSON on either side of the application in sync?

What I mean by that is if you do a GET for : www.mysite.com/item/12345. Then the .net side of the application goes away to the db, retrieves the Item with id 12345 and resolves it to your object model Item that is then serialised to JSON and returned.

If you do a POST to : www.mysite.com/item and pass -

{
    "Id": "12346",
    "ItemName": "New Item",
    "ItemCost": 45
}

Then the .net side of the application picks it up, deserialises it to an Item object and then hands it off to be added to the db.

How do you get both sides, your JS object model and your .net object model serialisation to sync up? Does this just need to be maintained by hand or is there a clever way of providing a template for the JSON based on the serialisation of the .net model?

I'm kinda just looking for best practices and whats the done thing and don't see how the client side knows what JSON to pass to the server side.

like image 753
David Avatar asked Feb 28 '12 12:02

David


1 Answers

Personally I've found it easier to 'drive' these changes for the .NET environment. Not wishing to teach you how to suck eggs but Javascript is a very loosely bound language means changes / functionality / properties can be added on the hoof whereas in .NET it is far easier to test and stablise your POCOs in a more rigid manner.

One method I have recently toyed with is generating empty POCOs from my service when creating objects, manipulating as appropriate before then pushing them back to the service for persistence, etc. It still doesn't resolve that wild-west like feeling of working in Javascript, but at least the DataContracts can match up on a superficial level.

like image 120
SeanCocteau Avatar answered Nov 15 '22 01:11

SeanCocteau