Copying Entities in an OO Fashion

When people think about OR mappers, they usually think about code
generation and automating the CRUD operations in an object oriented
fashion.  At my previous job, we rolled our own OR mapping
framework because we (1) didn’t know what OR mapping was (2) didn’t
realize that many other products exist that do the same thing but
without developing it yourself :-).

One feature that we incorporated into our framework was the notion
of copying an entity in the database.  At a simple level this
would take an entity, Customer, and copy its values into a new row and
returning the newly created entity. 

Customer c2 = c.Copy();

Now, this functionality by itself is pretty useful, and pretty easy
to implement. Basically all you are doing is reinserting the
current entity in the datastore, loading up the new entity and
returning it. This saves you a few lines of code by having the
.Copy method.  Where the benefits really start to show themselves
is when you want to copy an entity that has relationships with other
entities and you need those other entities copied as well

Consider a scenario where you have a calendar application that
has activities.  Each activity in turn has potentially many
contacts associated with it.  You can override the .Copy method in
your ActivityEntity object to look something like this:

[AirCode]
public ActivityEntity Copy()
{
         ActivityEntity copiedEntity = base.Copy();
 
         foreach(ActivityContactEntity item in this.Contacts)
        {
            
 ActivityContactEntity copiedContact = item.Copy();
              copiedContact.Activity = copiedEntity;
              copiedContact.Save();
        }
        return copiedEntity;
}

Now when you copy an activity, all the contacts get copied as
well.  All that has to happen after copying a contact is to
associate the contact with the new activity rather than the original
one. This example is pretty simple with only one set of related
entities.  In practive I’ve used this functionality to copy
entities that had about ten related entities, some as a collection
like in this example, and some 1 – 1 relationships.  Having this
easy ability to copy entities can really add a lot of rich features to
your applications, like copy and paste, undo and redo, stuff like that
becomes much easier when you can copy an entire object regardless
of its data complexity.  This is a feature that I’m going to be
working on adding to Codus for a future release, among other things, and I’m excited to be working on it, its a rather interesting and fun exercise.

3 Comments so far »

  1. Dennis van der Stelt said,

    Wrote on November 1, 2005 @ 3:10 am

    I just learned this myself a few days ago, it’s called a copy constructor. It’s apparently something from C++, and in C# it doesn’t exist.

    Read more about it here:
    http://msdn2.microsoft.com/en-us/library/ms173116.aspx

    Summary:
    While creating a new object, in its constructor, you pass in an object of the same type. The constructor will then copy the passed object into itself, making an exact copy.

    No idea if C++ supports this by default, unfortunatly C# doesn’t. But perhaps with some reflection, you can create this yourself.

  2. breichelt said,

    Wrote on November 1, 2005 @ 9:24 am

    Thanks for the comment Dennis, that would be pretty convenient :) To support an object copy (as opposed to a database copy), we are going to use a binary serializer. We’ll serialize the object, and the immediately deserialize it back to an object and then return it. This way its a brand new object (not another reference) and its an exact copy.

  3. Anonymous said,

    Wrote on November 3, 2005 @ 2:02 pm

    ADO.NET Provider Model Fundamentals [Via: dhayden ]
    Ajax’s responseXML Error [Via: ]

    Anticipated…

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Enter my name (ben) in this box, so I know you're a human.

Comment: