Search code examples
wcfservicedto

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.


Solution

  • 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.