calculate object delta

时光怂恿深爱的人放手 提交于 2019-11-29 11:47:11

At a previous job, we had large 3-D models that we wanted to share between clients. To save actual model changes would have been impossible given model size and bandwidth restrictions.

Instead of sending the whole changed model, we opted to serialize operations on the data. For example, an operation might be {CUT plane: (pt1, pt2, pt3)}, or {DRILLHOLE (point, radius, depth)}. This worked great for our app, though it might not be appropriate for your model.

Also, Matthew's suggestion of calculating hashes or timestamps is a good one. Also, maybe you could keep a hashtable of unique keys so the server knows which ones were deleted and which were added.

You may be able to save some bandwidth by giving your objects the ability to calculate a hash of themselves based on their property values. Compare the server object hash to the client object hash, if they are different, update the appropriate one.

I am planning my first N-tier architecture and came across this question just as I was about to post a similar one. All the sample code I’ve seen so far passes the entire object graph over the wire every time, without regard for what changes have actually been made. I've been considering to instead take the approach described above. Although, it appears to be the road less traveled.

I like the idea of passing back just the deltas for each modified property. For collection properties, this would be what’s been both added and removed. For a single-valued property, it could be just the new value. I could implement this in a general way such that the accumulation of these deltas would be transparent for all domain objects. And the deltas would be sent across the wire in a general form, without using DTO classes specific to my domain model.

This made me wonder if I couldn't take the idea further by also using the same general-purpose delta data structure for communication in the direction of server to client. After all, you could have a delta that basically populates an empty object. Then I could altogether eliminate the hard-coded DTO classes. I Googled "generic DTOs" (without quotes) and found this and this. Looks like others have already applied the idea in practice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!