开发者

WCF contract for Field level update

I'm developing an application that does some CRUD operations through a WCF service. The read method returns a complete entity, the update is performed through a legacy s开发者_高级运维ystem, and only the changed values should be updated.

What is the best way to design the data contract for this scenario without simply sending a dictionary of key-value pairs?


The only other thing I can think of is to make your component durable - i.e. persist its state to a file or database. That way, on the update you can compare the previous state to the state being passed in. I'm not sure that's a good way to go since it will introduce more overhead than just passing in the key-value pairs.

From the outside it might look more CRUDy or whatever, but from a practical standpoint you may be better off just passing some indication as to which values changed.


In case it helps, not sure exactly what you're looking for though ...

In the update request, only act upon fields that are not null.

In addition wrap any non-nullable types in a nullable structure.

As an example ...

Update( Nullable<int> orderNumber, 
        Nullable<DateTime> orderDate, 
        Nullable<bool> isComplete )
{
    if( orderNumber != null )
        databaseRecord.OrderNumber = orderNumber;

    if( orderDate != null )
       databaseRecord.OrderDate = orderDate;

    if( isComplete != null )
       databaseRecord.IsComplete = isComplete;
}


the best way to do this is with property dictionary, just represent your entities as dictionary of property name and value. save all changes in some list and pass a partial dictionary with all changed properties.

i think this is best design,

if u wanna avoid this design, send entire entity with some list of changed properties. (to save transport u can put null on other properties)

if u don't wanna change the service contract signature u can push the names of modified properties on the header


I had two ideas of how to achieve this;

  1. Have the client send both the original entity, and the changed entity in full, the service would then figure out what properties were changed.

  2. Use a pattern similar to Nullable, lets call it Modified with an IsModified flag and a NewValue property of type T. Each property of the DataContract would be of this type, the service can check the IsModified flag when performing the update.

The legacy sytem we use has an api that accepts String.Empty to identify unmodified fields, a '?' character is used to indicate an update to an empty string. I really don't like this, the user of the api is forced to read the documentation, and if you actually want to store a '?' you can't. I want our webservice api to be more explicit.


You can use DataSet to keep your changes. Call your record as DataSet then assign some values to the record. DataSet.Tables[0].GetChanges() will give you the columns which were changed.


You could leave the data contract alone and update your service contract. Just represent the required fields for the method as properties within the service contract. Any consuming application using the service will have to be updated if the service contact changes, but the consuming application will know what is required to successfully update the data.

There are positives and negatives to this method, but I use it when a method I am writing doesn't require the full data contract.

--Edited for a spelling error--


Looking at your requirements and statements, i've made a few assumptions before starting to write my vision on a possible solution:

  • You are using the same class for retrieving (return value type of "read" operation) and updating an item (input parameter type of "update" operation) in your WCF service.
  • Your current problem of implementation is how to use the original class (not a dictionary) AND still be able to determine 'what has changed compared to the read' when you get the "Update" operation called on your WCF service
  • You are writing both the server and client. Both are written using the MS .Net framework.

If this is true, the problem lies in the Update method missing information. The information required is 'has changed' which could be inferred if a 2nd state is present to compare against or should already be present along side the state to update in the back-end.

Since you only have the 'back-end state' (without flags) when the client posts its data to the WCF service, how should we determine what did change? Obviously, we want to prevent another 'read' roundtrip to get the current server state and start comparing.

Sending the original & changed state from the client to the server is a possible but heavy solution. Next to that, the client isn't interrested in this information, the server is.

Adding this all up makes my guess is that changing the type of the 'Update' operation input parameter is the easiest way to go. Create a decorator class that adds 'dirty bit' behavior to the original entity. Use this new class as input parameter for your "Update" operation. You then will have the availability in the server to check this dirty bit next to the full state send by the client. The major change on the client side is that the object needed for the 'Update' operation is no longer the same as the one provided by the 'Read' method. To eleviate this pain, i would probably create a decorator class which added the required 'dirty bit' handling. This only requires the object instanciation to change, while maintaining the interface signature for the client (very little code changes).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜