I have a working ChangeTrackingList implementation which does its job just fine, but I'd like to "filter" its contents when sending it from the client back to the server so that it only includes the changes. Getting the changes is easy, as my list exposes a GetChanges method for just that purpose. How can I interrupt the DataContractSerializer and substitute List.GetChanges() where List used to be?
More detail: Consider a parent/child relationship in which I have a single parent with multiple children, each of which has a reference back to the parent, such as Customer/Orders. Serializing the entire child list over to the client app is fine, since I need to show all the orders. I don't want to bring all the orders back over to the server when I save, however, just the changes.
Difficulty: I have already looked at implementing ISerializable, and implementing my own GetObjectData, which wouldn't be very hard if not for the fact that I need to preserve object references as well. If I point a DataContractSerializer at my graph, and enable PreserveObjectReferences (Either by adding a behavior, or explicitly through the constructor), I'll get a very nice graph with no duplications, but it will want to include my entire ChangeTrackingList. If I implement ISerializable, I can write out my ChangeTrackingList manually, and include only the changes, but those child objects won't know their parent references anymore.
Clarification: This is a highly simplified example meant to illustrate the problem. I'm not looking for alternate solutions to this particular problem. My real-world problem does not involve customers, orders, or line items in any way. Alternate solutions to the Customer/Order problem are not the answers I'm looking for.
I am, quite simply, looking for a way to serialize only the "interesting" parts of an object graph. The mechanism for identifying and filtering down to "interesting" is already done, I just need a way to substitute those parts into the object graph during serialization.
Another Example: Let's say we have a "Person" entity with a collection of "Phone" entities below it. Phones are invalid without a parent, and the business rules say a Person is invalid without at least one phone. I cannot simply save the person, and then the phones in two separate calls, because each call would be an error. I have to save them as a graph in one single call. Later, if I update the Person to change their address, which is stored on Person, and add a new phone number as well, I need to send the updated person as well as the changes to the list of phones. I don't want to send the original phone because it hasn't changed.
It's a contrived example again, but one more closely resembles the real life problem. I have parents which are invalid wihout at least one child, and no child is valid without a parent.
Update: It looks like the "substitution" I want to perform can be done by way of a DataContractSurrogate class. I've seen a few examples of this, and they are relatively straightforward, but that's because their examples are as well. They are usually of the "Swap EmployeeSurrogate for Employee" variety, where "Employee" is some non-serializable class. In my case things get weirder because the class I want to swap is a generic type.
So, a simpler version of the problem might be this. Lets say I have a totally non-serializable MyList class. (And before someone suggests it, replacing the MyList class is not a valid solution, either. Remember folks, it's just an example.) I want to set up a DataContractSurrogate so that whenever a MyList appears in my object graph, I want to convert it to a simple array for serialization.
Does THIS sound like a valid direction to anyone? Has anyone ever tried to surrogate a generic type? Am I insane for even considering it?