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

Gary's Blog

XPO Cookbook #5 – Creating Persistent Objects for an Existing Database

Problem

You have an existing database for which you have to create an application and so wish to create a set of persistent objects to reflect the table(s) you have to work with.

Solution

Use the PersistantClasses Wizard to generate the classes for you, then use as normal.

using System;
using DevExpress.Xpo;
using AdventureWorks;
using DevExpress.Data.Filtering;

class Program {
    static void Main(string[] args) {
        //GS - Create and save a contact
        using (UnitOfWork uow = new UnitOfWork()) {
            new Person_Contact(uow) {
                FirstName = "Gary",
                LastName = "Short"
            }.Save();
            uow.CommitChanges();
        }

        //GS - Read the contact back to prove it is persisted
        using (UnitOfWork uow = new UnitOfWork()) {
            Person_Contact contact = 
                uow.FindObject<Person_Contact>(
                    new BinaryOperator("FirstName", "Gary"));

            if (contact != null) {
                Console.WriteLine(String.Format(
                    "Contact first name {0} has been written to the database",
                        contact.FirstName));
            };
        }

        //GS - Reset database
        using (UnitOfWork uow = new UnitOfWork()) {
            Person_Contact contact = 
                uow.FindObject<Person_Contact>(
                    new BinaryOperator("FirstName", "Gary"));

            if (contact != null) {
                uow.Delete(contact);
                uow.CommitChanges();
            }
        }
    }    
}

Discussion

The PersistentClasses Wizard will allow you to select the table(s) and field(s) you wish to select from your existing database; it will then generate persistent class(es), for the selected items, in a single class, thus not polluting your project if you have generated dozens of classes. In the example above I generated the Person.Contact table from the AdventureWorks sample database.

From here, you can either use the classes as normal (as per the example above) or you can subclass one or more of these generated classes and place your business logic and any non persistent properties in these subclasses.

Technorati tags:
Digg This
Published Aug 28 2008, 04:59 PM by Gary Short (Developer Express)
Filed under: ,
Technorati tags: XPO, XPO Cookbook

Comments

 

Sha73 said:

Gary,

I think there's no need for the UOW in the first lines of code ? The 'Save' will the default session and not the uow, right ?

Or I missed something here ?

August 28, 2008 12:30 PM
 

Alain said:

i think that forgot pass uw in constructor method as parameter. (not enought salt in the recipe) :)

August 28, 2008 12:46 PM
 

Gary Short (Developer Express) said:

We consider it best practice to use UnitOfWork. Though, strictly speaking, the .Save() is not required.Smile

August 28, 2008 12:52 PM
 

Gary Short (Developer Express) said:

@Alain, yes for best practice, the ctor should have the uow passed in, and I've changed the example to show that, but the example works without it. [;-)]

August 28, 2008 1:19 PM
 

Linton said:

First, thanks very much for you articles.

Second, one of the (constant) beefs users have with DevExpress examples is that don't always conform to Best Practices. So new users take-off and start coding based on examples and get to a point (1 million lines later) where they have a problem and DevEx says: "Oh, you should be doing it THIS way". I can point to several posts where this has happend and folks have complained.

Having said that, it is NEVER okay to provide a sample that does not conform to currently accepted best practices. As punishment, you must now go back to all examples and fix them before you lead too many astray :-)

August 28, 2008 1:58 PM
 

Gary Short (Developer Express) said:

Don't worry Linton, my example does conform to currently accepted best practice, I just forgot to pass the uow into the constructor, it was more of a typo really, which I've corrected in the sample.

August 28, 2008 5:43 PM
 

Mike_Grace said:

I can't currently use this as Advantage Database Server is not available in the wizard. I have created a suggestion for it.

August 29, 2008 5:17 AM
 

Gary Short (Developer Express) said:

@Mike_Grace, although the Advantage Database Server is supported by XPO for "greenfield" projects, it is not supported in the wizard and so, I'm afraid, it cannot be used for "brownfield" projects.

August 29, 2008 10:31 AM
 

Paul Gable said:

Are there any workarounds for created XPO objects for tables whose primary keys are composed of multplie columns?

November 4, 2008 8:56 PM

Leave a Comment

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