Lets have this model:

public class Order {
  public Customer Customer {get;set;}
  public string Name {get;set;}
  ...
  public EntitySet<OrderItem> Items {get; private set;}
}

public class OrderItem {
  public string Code {get;set;}
  public string Name {get;set;}
  public Units Units {get;set;}
}

public class Units: Structure {
  public UnitType Type {get;set}
  public int Value {get;set;}
}

public enum UnitType {
...
}

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.

asked Dec 04 '09 at 06:48

Peter%20%C5%A0ulek's gravatar image

Peter Šulek
492313236


One 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

  • Assign all its field

  • Assign it to its owner's field. AY: See the manual chapter about them AY: There are some aspects making their behavior different from ValueTypes in .NET - generally, that's all.


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.

answered Dec 04 '09 at 17:36

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

Hi, i make little helper extension method(extends class <t> where T: Persistent) to deep clone. Later(7.Dec) i attach source code here.

(Dec 04 '09 at 17:36) Peter Šulek Peter%20%C5%A0ulek's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA