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:
XPO Cookbook