Best practices for not passing object references on service calls
I have an application where a client communicates with a server side through REST. This is written in .Net, but I guess the question should be independent of this.
Now - I have services such as GetAllCustomers
and GetCustomerById
. A Customer has references to a potentially big list of Order, so I don't want to pass the Customers references from the GetAllCustomers
service. I basically want to return the Customers with their simple data, but no references. Then I will do another service call GetCustomerById
to fetch the complete data when a Customer is selected in the client.
The question now is - is there a recommended way of handling this? Using开发者_如何学运维 Lazy Loading I can simply pass the object before the references are loaded - and then make sure this isn't used on the client side. But is this ugly? (Also - I got problems communicating lazy loaded objects with REST, but it worked with SOAP - but this is a different question..) I guess I could fetch all data from the database and then delete the references before I return it, but this sure sounds hacky. Also I still get unnecessary load on my database..
So; is there a good solution to this?
I'm assuming you are transmitting your data with JSON/XML.
Try to lazy-load the objects (server side) and when serializing ignore the Client Order list.
EDIT: You can omit a member from serialization with the attribute [System.Xml.Serialization.XmlIgnoreAttribute]
精彩评论