Clone/Copy?

Clone/Copy?

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


kids_pro posted on Tuesday, September 12, 2006

I want to copy object and save it a seperate row (ofcause make change a litle to the properties before save). However when I use Clone() method I can't archive this purpose. I need to manual create copy method for my object and copy field by field.
Is it the way it works?

ajj3085 replied on Tuesday, September 12, 2006

Kids,

Yes, I created a method called Duplicate, because I had a use case which was to copy most of the information, but not all.  My implementation was to use Clone and then blank out any properties which should be set to empty or the default value.

Here's what mine looks like.  This duplicates a Contact business object.

public Contact Duplicate() {
    Contact result;
    SmartDate date;

    result = Clone();

    result.ContactId = GetNextNewId();
    result.FirstName = "";
    result.LastName = "";
    result.Email = "";
    result.Title = "";
    result.Salutation = "";

    result.CreatedUser = ApplicationContext.User.Identity.Name;
    date = new SmartDate( DateTime.Now );
    date.FormatString = "G";
    result.CreatedDate = date;
    result.ModifiedUser = "";
    result.ModifiedDate = new SmartDate();

    result.PhoneNumbers = PhoneNumbers.NewPhoneNumbers();
    result.Address = result.Address.Duplicate();

    result.MarkNew();

    return result;
}

RockfordLhotka replied on Tuesday, September 12, 2006

kids_pro:
I want to copy object and save it a seperate row (ofcause make change a litle to the properties before save). However when I use Clone() method I can't archive this purpose. I need to manual create copy method for my object and copy field by field.
Is it the way it works?

In 2.0 the Clone() method is Overridable/virtual, so you can swap out my implementation for your own if you choose.

That would allow you to implement the Duplicate() idea, but directly in the Clone() method.

Copyright (c) Marimer LLC