Copy / Duplicate child BO

Copy / Duplicate child BO

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


ksavoie posted on Thursday, May 19, 2011

I've been ask by my client to add a "duplicate" function to my windows form application in VB.NET 2005, using CSLA 2.

I'm looked into the copy of the object.  I tried to use the following code, but it's not doing what I need. 

Public Shared Function NewCost(ByVal oldCost As Customer) as Cost
   Return CType(DataPortal.Create(New CopyCriteria(oldCost)), Cost)
End Function

<Serializable()>
Private Class CopyCriteria
   Public OldCost as Cost
   Public Sub New(ByVal oldCostas Cost)
        Me.OldCost = oldCost
   End Sub
End Class

I have a one Contract, with a list of Cost.  I want to be able to duplicate the Contract Information and the first Cost, so CostID = 0.

How would I duplicate/copy only the information of the CostID = 0?  I tried to find a solution online, but without success.

Can anybody help me with this one?  Examples of code would be great.

 

ajj3085 replied on Thursday, May 19, 2011

Are you trying to duplicate the data first, then return an instance, or duplicate an instance the user is currently looking at?

If its the former, you probably want to go with a command object that simply copies the rows in the database, and returns the new objects id.

If you're after the latter, create a duplicate instance method.  The method should be fairly straight forward, the first thing it will do is Clone itself, then you'll need to set all the primary key values in your objects to null or empty or whatever.  Ensure each item in the graph calls MarkNew, then you may want to run rules on all instances again.

At that point you can present the "new" object to the user, allowing them to edit and then save the new object.

ksavoie replied on Friday, May 20, 2011

thanks Andy,

I'll try your second option.

Copyright (c) Marimer LLC