in
Forums
Blogs
Files
Devexpress.Com
ClientCenter
Support Center
DevExpress Channel

Gary's Blog

XPO Cookbook #1 – Quickly Serialise an Object

Problem

You want to serialise an object as quickly as possible and you have no preference as to which RDBMS is used.

Solution

using System;
using DevExpress.Xpo;

class Program {
    static void Main(string[] args) {
        //GS - Create a unit of work to work within
        using (UnitOfWork uow = new UnitOfWork())
        {
            //GS - Create and serialize a person
            Person person = new Person(uow) { 
                FistName = "Gary", 
                LastName = "Short", 
                DateOfBirth = DateTime.Parse("24/01/1970") 
            };

            uow.CommitChanges();
        }
    }
}

//GS - Define a Person Class
public class Person : XPObject {
    public Person(Session session)
        : base(session) { }

    private string _FistName;
    public string FistName {
        get {
            return _FistName;
        }
        set {
            SetPropertyValue("FistName", ref _FistName, value);
        }
    }
    private string _LastName;
    public string LastName {
        get {
            return _LastName;
        }
        set {
            SetPropertyValue("LastName", ref _LastName, value);
        }
    }
    private DateTime _DateOfBirth;
    public DateTime DateOfBirth {
        get {
            return _DateOfBirth;
        }
        set {
            SetPropertyValue("DateOfBirth", ref _DateOfBirth, value);
        }
    }
}

Discussion

The first thing to notice is that I have more than one class defined in my source file. Such things are normally frowned upon in favour of a “one class, one file” mantra, and rightly so. However, I am not writing an application for production here, I’m writing code for a blog post and it makes it easier to discuss if all the code is displayed; it also makes it easier for you to cut, paste and run the example if you so wish. I will be defining all my classes in the one source file for all of the recipes in this cookbook, but I will only be explaining the rationale once. If you wish, you may take bets on how far into the cookbook I get before someone comments along the lines of “shouldn’t you have one file per class?”. When they do, the reader who guessed closest to the recipe number wins; when it happens you can also point the commenter here for an explanation. :-)

Anyway, on to the explanation of the recipe. Firstly let’s chat about the business object we have created for serialisation. The first thing to notice is that our class inherits from XPObject, one of three classes that your business objects can inherit from when using XPO, more on the other two in future recipes. XPObject gives us support for unique keys and optimistic locking out of the box and is the class inherited from if you use the CodeRush template to create your business object. The CodeRush template is the recommended way to create such business classes and is the route I always take. The mnemonic for this template is “xc”.

The next thing to notice about our business object is the setter for the properties uses the SetPropertyValue method, this allows XPO to handle change management for you and is one less thing for you to worry about. The CodeRush templates for adding these properites (“xps” for string properties and “xpd8” for the DateTime property) adds the call to SetPropertyValue for you and is, again, the recommended route.

To make use of our business object we create a UnitOfWork. The UnitOfWork inherits from Session and the benefit it gives us developers is that we no longer have to keep track of all the changes to our persistent classes ourselves. When we used the Session object each instance of our business objects had to be sent the Save() message in order to be persisted. When using UnitOfWork this is no longer the case, the UnitOfWork tracks all the changes for you and they are persisted by making one call to CommitChanges() once you have made all the required changes to all of your objects and you are ready to serialise your object graph.

The last thing to mention is that the default persistence mechanism is to serialise the object graph to an Access database. We shall examine how to change this default behaviour in a future recipe.

Technorati tags: ,
Digg This
Published Aug 25 2008, 12:46 PM by Gary Short (Developer Express)
Filed under: ,
Technorati tags: XPO, XPO Cookbook

Comments

 

Paul Fuller said:

Well there is a challenge.  I guess that it will be in Recipe #1.

Hey Gary, “shouldn’t you have one file per class?”.

What do I win ?

August 25, 2008 8:33 AM
 

John Watson said:

I just "discovered" this weekend that the overloaded version of SetPropertyValue you are using is deprecated. I too had been using it everywhere per lots of examples. This post (community.devexpress.com/.../some-setpropertyvalue-getpropertyvalue-overloads-will-be-deprecated.aspx) states "In future releases of XPO, we will deprecate certain overloads of the SetPropertyValue and GetPropertyValues in XPBaseObject."

August 25, 2008 8:47 AM
 

John Watson said:

p.s. It's okay if you want to say "bloody hell" I barely finished posting the sample when somebody points out a problem! :) It happens to me all the time.

August 25, 2008 8:53 AM
 

Gary Short (Developer Express) said:

@Paul, doh, I walked right into that one. You don't win anything as no one likes a smart alec <grin>.

@John Don't worry, only the overloads that did *not* contain a parameter pertaining to the property name are depricated, as per Oliver's post which you linked to. These deprications were done a while ago so if you are using overloads and not getting a "deprication warning" you are safe. Also, if you are using the CR templates then you are also safe.

August 25, 2008 9:11 AM
 

Trevor Westerdahl said:

Ummm... I am confused....

You mention "serialize" an object - I get excited... then I realize everywhere you say serialize, you mean "save" or "persist".

I hope you correct that because serializing XPO objects has yet to be on the menu (as far as I know).

August 26, 2008 5:30 AM
 

Gary Short (Developer Express) said:

@Trevor, You are quite right, sorry about that. I forgot there for a moment that, only in the .Net world, is there a distinction between serialize and persist. I, of course, mean persist.

August 26, 2008 5:55 AM
 

Ryan Britton said:

I also had to stay my beating heart as I raced to this page to see how to "Quickly Serialize an Object" (Which has been an ongoing debate in various forms for quite a while in the forums and has several (now quite old) posts from Sturm in the blog...

Oh well...

I don't suppose you're consider that for your next recipe? Now there is a cookbook that I'd like in my kitchen :)

September 2, 2008 4:48 PM

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED