ZaneZ wrote:
Copy Entities
I know that I’ve asked this question before and I was advised to use the Serialize method in order to clone an entitiy. However using the Serialize method on a very large entity isn’t efficient. So I created a method for each entity Manager to copy an entity (this method copies only the necessary related entity collections by traversing each collection and cloning the fields of each entity that's needed). This also isn’t efficient because each manager would need the same type of method (inefficient code). I was wondering if there was another way, possibly using the WriteXML method, of cloning an entity and all its related data.
There are two things:
1) cloning a single entity
2) cloning an entity and all its related entities.
in case of 1), you can do (I'm cloning myCustomer here for adapter: )
CustomerEntity clone = new CustomerEntity();
clone.Fields = ((EntityFields2)myCustomer.Fields).Clone();
this now gives you a perfect clone of the entity's fields.
WriteXml is slower than binary serialization to a memorystream, so in case of 2) I'd opt for the memorystream trick, which is a generic piece of code.
However, reading your sidenote, I think there is an easier method ->
Side note. This is a major problem in our current application. It’s a C# windows .NET app and the problem occurs when we create a form that takes in a pre-loaded entity to manipulate. Basically we don’t want the same entity to be updated in the form because if the form is cancelled we don’t want the entity to be updated. So we copy the entity and pass it into the form, so when OK is hit, the temporary entity within the form is copied back to the original. Going back and forth from the database just to open a form and update an entity isn’t an option. Any Suggestions?
Entities have field versioning build in . So if you want to roll back an entity's fields to a previous set of values, use this:
myCustomer.SaveFields("BeforeForm");
// here you open your form, pass in myCustomer
MyForm editForm = new MyForm();
editForm.ToEdit = myCustomer;
DialogResult result = editForm.ShowDialog(null);
if(result == DialogResult.Cancel)
{
// cancel clicked, roll back
myCustomer.RollbackFields("BeforeForm");
}
else
{
// save entity
}
This is per entity, so it doesn't version related entities. Research on this has shown that it can be done, but versioning of graphs is pretty complex, though not impossible Graph versioning is planned.