Clone an object and mark childrens as New

Clone an object and mark childrens as New

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


Antonio Sandoval posted on Thursday, January 29, 2009

I have a class Root named Invoice, and a child list named InvoiceDetailList
BussinesBase<Invoice>
    BussinesListBase<InvoiceDetailList, InvoiceDetail>

class Invoice{
public InvoiceDetailList GetInvoiceDetailList {
get {
 return _InvoiceDetailList;
}
}
}

The user can cancel an Invoice, and also can duplicate and cancel at the same time:
The clone method must cancel the Old Invoice, so I wrote a custom Clone method:

public Invoice DuplicateInvoice(){
        CanExecuteMethod("CancelInvoice", true); //Same command for CancelInvoice()
        
         //Run Cancel Invoice Command
CancelInvoiceCommand result;
Invoice newInvoice=null;
result = DataPortal.Execute<CancelInvoiceCommand>(new CancelInvoiceCommand(_InvCode));
if(result.Cancelled){
newInvoice = base.Clone();
newInvoice.MarkNew();
//How to mark new all the childrens?
}
return newInvoice;
}

Now, the problem is that the InvoiceDetail does not mark as New, I was thinking on write an Internal method to do that, but maybe there is a better way.

Thank you in advance

rsbaker0 replied on Thursday, January 29, 2009

Another way to do this that avoids side-effects like this is to actually create a new object, and then use the Csla.Data.DataMapper class to copy the corresponding fields from the source object to the new target.

I've done it both ways. Cloning might be faster, but then you have an object that may have unwanted baggage from the original like autonumber keys, etc. You can pass properties to be ignored to Csla.Data.DataMapper when copying from one object to another. Note that DataMapper only appears to copy browsable properties, if that is a consideration.

JoeFallon1 replied on Thursday, January 29, 2009

I agree. I do it this way too. Create a new object and set its properties from the source object.

Joe

Antonio Sandoval replied on Thursday, January 29, 2009

Perfect, it works, thank you.

            Invoice cloned = Invoice.NewInvoice();
            Csla.Data.DataMapper.Map(this, cloned, new string[] { "AccCode", "InvoiceDetailsList" });
            foreach (InvoiceDetail detail in this.InvoiceDetailsList)
            {
                InvoiceDetail clonedDetail = InvoiceDetail.NewInvoiceDetail();
                Csla.Data.DataMapper.Map(detail,clonedDetail ,new string[] {"AccCode"});
                cloned.InvoiceDetailsList.Add(clonedDetail );
            }

Copyright (c) Marimer LLC