AdFactum ObjectMapper .NET Blog

Official blog of the AdFactum ObjectMapper .NET

New Release – ObjectMapper .NET 0.99.417.0

Posted by Gerhard Stephan on May 17th, 2006

Storing object trees that contains self referencing objects turned out as a bigger problem as expected.

We found out that it’s not always sufficient to generate one insert per object. Sometimes it’s necessary to update the object reference separately after doing the insert. That is the case if I have a tree structure like this:

///<summary>
/// Company Object
///</summary>
public class Company : ValueObject
{
      private string name;
      private Company parent;
      private ArrayList childs = new ArrayList();
 
      public string Name
      {
            get { return name; }
            set { name = value; }
      }
 
      public Company Parent
      {
            get { return parent; }
            set { parent = value; }
      }
 
      [BindCollectionTo(typeof(Company))]
      public ArrayList Childs
      {
            get { return childs; }
            set { childs = value; }
      }
}
 
 

Company c1 = new Company();
c1.Name = "Parent";
 
Company c2 = new Company();
c2.Name = "Child";
 
c2.Parent = c1;
c1.Childs.Add(c2);
 
AccessPersister persister = new AccessPersister("test.mdb");
ObjectMapper mapper = new ObjectMapper(new DefaultFactory(), persister);
 
mapper.Save(c1);
 
mapper.Dispose();

The property named "Parent" of the child object can only be set, if the parent object has already been inserted into the database. That’s why the ObjectMapper .NET code had to be changed in order to do the insert of the child and parent object. After that insertion, the ObjectMapper .NET updates the parent property using a separate update SQL statement. In older releases the code produced an exception or did not store the tree correctly in database.

To cut a long story short the code mentioned above does work in the current release.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>