Lets have this model:
Then i create new instance of Order with new Order{Name="...", ...} and setup all properties it have. Imagine that entity Order and OrderItem has more properties, entitysets, ... this is just for example. Saving this to database, where is created new order record, some orderitem records, etc... In my application(asp.net), user can select this order in grid and execute action "copy", which will copy all order informations including order items and its properties, entitysets, etc... and when user clicks ok in copy/edit dialog, then new record(s) are saved to database with copied informations. My question is how can i do this "deep clone" of some entity object with DO4 without manually creating all objects and manually assigning its values to new object? Maybe some serialization/deserialization utils you use with DisconnectedState, but i want some hint how to do that exactly. I found this 2 post but there isnt any usable response there: viewtopic.php?f=13&t=1122&p=4537&hilit=clone#p4537 viewtopic.php?f=12&t=1919&p=7271&hilit=clone#p7271 This thread was imported from our support forum. The original discussion may contain more detailed answer. |
I replied to this in Skype, shortly I'll copy-paste the clone of our talk ;) Please continue any further discussion here: viewtopic.php?f=56&t=5693&start=0 Quote from Skype talk: AY: quick answer is: AY: there is no way to do this w/o manual graph traversal now. AY: Serialization in DO4 is currently designed for different purpose (serializing persistent objects as references), so this won't help. AY: I'll ask my guys to check how long it must take to extend it in the way you need. PS: i thinking about use property Type.Fields on entity PS: to get values from all fiels within entity AY: Yes, that's what you need. PS: can be this the way? AY: Generally, there is all the info you need to clone. But there is no out of the box solution for this. PS: ok, i try it myself PS: and about cloning class derived form "structure" PS: i have some problems yesterday AY: I.e. Entity.Type is TypeInfo from Xtensive.Storage.Model. Classes there provide all the info you need. Like reflection, but adopted to Storage. PS: is there any special information about Structure PS: and cloning it PS: it is something like struct in c# i think, valuetype PS: ? AY: No, generally you should: AY: - Create a new instance of structure
Thanks ;) Btw, general task must be pretty complex, since you must track the references to avoid cloning the same object referenced from two (or more) other clones twice. Hi, i make little helper extension method(extends class <t> where T: Persistent) to deep clone. Later(7.Dec) i attach source code here. |