Clone Issue with Collection

Clone Issue with Collection

Old forum URL: forums.lhotka.net/forums/t/3860.aspx


ravimaran posted on Wednesday, November 07, 2007

Hi,
I have an object Client and it contatains a collection List. I.E. 
Client object has a variable clientOrders as a collection list and the child object Order
[Serializable()]
class Client : BusinessBase<Client>
{
    _clientOrders = ClientOrders.EmptyOrderList()
   // At run time it creates an orderList with orders.
}

[Serializable()]
class ClientOrders : BusinessListBase<ClientOrders, Orders>
{}

and
[Serializable()]
class Orders: BusinessBase<Orders>
{}

Now when I do Client.Clone() I am able to see the clientOrders but all the each order object's IsDirty or IsNew property set to False.  Therefore when I save the cloned object the clientOrders is not saved as well.  I wasn't able to override Clone() method in clientOrders so I can make all the order object as MarkAsChild() and IsNew() to true. 

I am really confused on how to accomplish this.  Please help me to make this happen.  Thank you.

RockfordLhotka replied on Wednesday, November 07, 2007

Clone() returns an identical clone. It must do this in fact, or the data portal will fail.

It sounds like you want a Copy() method that creates a new set of objects based on the original (but not the same).

In the past I've recommended people override the cloning behavior to make this happen, but now that would be a bad idea, because the 3.5 data portal will clone your objects by default in some cases, and it expects a real clone (100% copy).

So what you should do is create a Copy() method that creates a clone and then manipulates the clone as required. This may mean implementing extra methods on your objects to allow the Copy() method to reset your child states:

Public Function Copy() As Client
  Dim tmp As Client = Me.Clone()
  tmp.FixUpAfterCopy()
End Function

Private Sub FixUpAfterCopy()
  MarkNew()
  mChildList.FixUpAfterCopy()
End Sub

---- then in ChildList ----

Friend Sub FixUpAfterCopy()
  For Each item As Orders In DeletedList
    item.FixUpAfterCopy()
  Next
  For Each item As Orders In Me
    item.FixUpAfterCopy()
  Next
End Sub

---- then in Orders ----

Friend Sub FixUpAfterCopy()
  MarkNew()
End Sub

 

Copyright (c) Marimer LLC