Update of large entity through service

笑着哭i 提交于 2019-12-07 22:49:54

问题


We're building an n-tier system based on WCF (but not Entity Framework) and we've run into a discussion of the best way to implement updates of large entities. We've created DTOs and map data from our domain model into theese when data is sent to the client. The client then makes some changes and sends them back using the same DTO, in our current implementation. Some of our entities might have 80-100 properties, but perhaps the client only makes changes to one or a few of them. It seems inefficient to populate the whole DTO and send it back and then try to figure out on the server side wich properties were actually modified. Is there a better way to implement this or should we go with the "brute force" method? In the future we need to support non .Net clients so we don't want to tie our solution to something .net-specific.


回答1:


We solved this problem by including a string array of changed property names on every DTO. The update code in the service only validates and writes properties whose names are in this array.

To make coding easier, we put this string array in an abstract base class and inherited all "changeable" DTOs from that. For example:

<DataContract>
Public MustInherit Class ChangeTrackableObject
    <DataMember> Public Property Changes As HashSet(Of String)
End Class

There are no <KnownType> attributes because DTO polymorphism is not required in this case. The DataContractSerializer simply uses the inheritance relationship to pull in the base class property, nothing more.

This system seems to work equally well from .NET and Java client code. Java clients see this as a string array. We have a library of extension methods for .NET clients to make it easier to populate the data.




回答2:


One thing you can do is allow the client to send delta updates. All your objects could have a unique guid that the server mananges.

You could have a WCF method like so to send the delta

  void UpdateProperty( Guid objguid ,
            string propertyname , string propertyvalue );

On the server side you can locate the object using the guid and update the corresponding property.




回答3:


If you're schema is that large, you could try using XML as a web method parameter along with an XSD that has optional elements. This wouldn't be tied to a particular platform. You could just send in the elements that have changed along with the unique key identifier.



来源:https://stackoverflow.com/questions/8714463/update-of-large-entity-through-service

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