简体   繁体   中英

Update of large entity through service

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM