Blogs

Gary's Blog

XPO Cookbook #3 – Persist an Existing Hierarchy Using Attributes

     

Problem

You wish to use XPO as an ORM tool. You have an existing hierarchy of business objects and you do not wish to implement any extra interfaces.

Solution

Assume you have the following hierarchy of business objects where Employee inherits from Person:-

//GS - Create an existing hierarchy
public class Person { private string _Name; public string Name { get { return _Name; } set { _Name = value; } } private DateTime _DateOfBirth; public DateTime DateOfBirth { get { return _DateOfBirth; } set { _DateOfBirth = value; } } public Person() { } } public class Employee : Person { private string _EmployeeNumber; public string EmployeeNumber { get { return _EmployeeNumber; } set { _EmployeeNumber = value; } } public Employee() { } }

After adding support for XPO the hierarchy will look like this:-

[Persistent]
public class Person {

    private int _Id;
    [Key(true)]
    public int Id {
        get { return _Id; }
        set {
            _Id = value;
        }
    }
    
    private string _Name;
    public string Name {
        get { return _Name; }
        set {
            _Name = value;
        }
    }

    private DateTime _DateOfBirth;
    public DateTime DateOfBirth {
        get { return _DateOfBirth; }
        set {
            _DateOfBirth = value;
        }
    }
    
    public Person() {
        
    }
}

[Persistent]
public class Employee : Person {

    private string _EmployeeNumber;
    public string EmployeeNumber {
        get { return _EmployeeNumber; }
        set {
            _EmployeeNumber = value;
        }
    }
    
    public Employee() {
        
    }
}

Creating, modifying and saving objects from this hierarchy are now done in the same way as if the objects inherited from one of the XPO classes in the first place. The full example now looks like this:-

using System;
using DevExpress.Xpo;


class Program {
    static void Main(string[] args) {
        XpoDefault.Session.Save(new Employee() { 
            Name="Gary",
            DateOfBirth = DateTime.Parse("24/01/1970"),
            EmployeeNumber = "RA 1234"
        });
    }
}

[Persistent]
public class Person {

    private int _Id;
    [Key(true)]
    public int Id {
        get { return _Id; }
        set {
            _Id = value;
        }
    }
    
    private string _Name;
    public string Name {
        get { return _Name; }
        set {
            _Name = value;
        }
    }

    private DateTime _DateOfBirth;
    public DateTime DateOfBirth {
        get { return _DateOfBirth; }
        set {
            _DateOfBirth = value;
        }
    }
    
    public Person() {
        
    }
}

[Persistent]
public class Employee : Person {

    private string _EmployeeNumber;
    public string EmployeeNumber {
        get { return _EmployeeNumber; }
        set {
            _EmployeeNumber = value;
        }
    }
    
    public Employee() {
        
    }
}

Discussion

To enable the initial object hierarchy to be persisted by XPO the following steps were carried out:-

  1. The Persistent attribute was added to both the Person and the Employee Classes.
  2. A new property (Id) was added to Person (this is inherited by Employee).
  3. The Key attribute was added to the Id property and a boolean parameter of true was passed to it to indicate that the key should be generated automatically.
Technorati tags:
Digg This
Published Aug 26 2008, 04:53 PM by Gary Short (DevExpress)
Filed under: ,
Technorati tags: XPO, XPO Cookbook
Bookmark and Share

Comments

 

Robert Fuchs said:

The Id is generated automatically - then why did you define a setter?

August 26, 2008 3:17 PM
 

Gary Short (Developer Express) said:

Though not strictly required in this recipe, as the object is only being persisted to the database, the setter is required when reading the object back from the database.

August 26, 2008 5:04 PM
 

Alain said:

Why not use SetPropertyValue methods to ensure the OnChanged event call ??

August 27, 2008 2:29 AM
 

Gary Short (Developer Express) said:

@Richard, further to my last, I've done some testing on this and if you don't have a setter on the Id field when you try to save the object then a "Nonpersistent key" exception is thrown.

@Alain, for no other reason than this recipe was focused on using the attributes to make an exsisting class hierarchy persistable.

August 27, 2008 5:05 AM
 

Mark Krasnohorsky said:

Hi Gary,

[Key(true), Persistent("Id")]

private int _Id;

   [PersistentAlias("_Id")]

   public int Id {

       get { return _Id; }

  }

Should do the trick.

MK

August 27, 2008 9:01 AM
 

Gary Short (DevExpress) said:

@Alain, oh and the fact that that method is defined in DevExpress.Xpo.PersistentBase

August 27, 2008 11:28 AM
 

Barrie McGuire said:

Hang on..   if you can do this, then what's the point in descending from XBObject/XPBaseObject at all? unless i'm missing something here? :)

February 13, 2009 7:00 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.