XPO - Using the class generation wizard

XPO Team Blog
24 July 2007

Newest versions of XPO (since the 7.2 release) have a wizard that allows you to to create class structures from existing databases. This feature has been requested many times in the past, and we made first steps a while ago by including a tutorial project that supports MS SQL Server (if you're curious, the tutorial is still included - it's called "GeneratingPersistentClasses"). The new wizard takes things to the next level by supporting a variety of additional database systems, so we recommend you start using it instead of the tutorial application. If you give us feedback on the functionality, we will also consider extending it in the future.

Using the feature is not difficult, but possibly not immediately obvious. In an existing project (currently with the exception of ASP.NET projects - see the end of this post), add a new item to the project and select Persistent Classes X.X from the list of item types (the X.X stands for the precise version of XPO you have installed). You can change the name of the new file to something more useful, but in practice I don't find that very important because I'm going to change the file name anyway once the classes have been generated.

 

 

Before anything is created, the template brings up a dialog that allows you to select the database provider you want to use, the database itself and any connection parameters, and finally the tables and columns you want to generate classes and properties for.

 

 

After this step, the class creation process is executed and the result is a new file in your project with all the new classes inside. The wizard currently leaves two important things to do:

  1. Renaming the classes. In traditional databases, tables regularly have weird names that I don't want in my OO class structure. Even if the names aren't weird, they may not be totally suitable. In the screenshot above you can see tables called Categories and Customers (plural). I would rename the resulting classes to Category and Customer (singular), because that makes more sense in an OO context.
    Hint: if renaming sounds like a lot of work, have a look at Refactor! It has a Rename refactoring that makes this extremely easy.
  2. Pull each of the new classes into its own file. Having a single class per file is a convention I recommend very much. Another thing Refactor! can help you with, btw. Why is it not done automatically? No idea, to be honest... but I think the current implementation isn't so bad, because it leaves the decision to the developer and it makes search and replace type operations on the freshly generated source code much easier (for those who don't use a tool like Refactor! <g>).


Finally, a word about ASP.NET projects. In the current release, the template Persistent Classes X.X (as well as the standard template Persistent Object X.X) is not available if the project type is ASP.NET. The reasons for this are somewhat complicated, you can find an explanation here. We will fix this problem for the next minor update, meanwhile you can import persistent class declarations into a different project and move them over to the ASP.NET one - but do look out for the guidelines given in this post.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
Tags
No Comments

Please login or register to post comments.