XPO - Using the class generation wizard

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.

Tags
7 comment(s)
Nate

I really liked this new feature. It helped me get up and running on a new XPO project right away.

The only thing I can think of that would be particularly useful would be finding the FK relationships and creating the Associations. But that's already been accepted as a suggestion so that's good.

I'm glad you guys are blogging again! Twice in one day! It's like Christmas!!!

24 July, 2007
Oliver Sturm (DevExpress)

I never stopped blogging - it's just the frequency that varies :-)

24 July, 2007
Luc DEBRUN

This feature is absolutely wonderful if you have an already existing database and want to port your app to XPO. Did this recently with an old app and made it into an XAF app in a few hours. I obviously support the suggestion that the wizard needs to create the associations too. Furthermore, may be the wizard could be extended whereby for each of the fields selected, one could have the option to select the XPO (and XAF?) attributes that should go with it. May not be possible for such wizard to have all attributes options available but the main ones should be there (the built-in types of Rule attributes from XAF would be great for example).  

24 July, 2007
George Walker

It would be nice to have a Check/Uncheck All ability because when I'm presented with a non-sorted list of all 200+ tables in my database, it's kind of a pain...

Otherwise, it's a pretty good feature.

26 July, 2007
Alberto Cortes

Wonderful new feature!!!.

I think it could be better with:

1) Check/Uncheck All options.

2) Option to use [DisplayName("Name to Show ")]

3) Control to change the order (move the last to firts and so on)

4) To make relationships automatically.

Anyway its great!

6 August, 2007
Joao Luis

I like this feature a lot, but .....

Did you try Class Wizard with a 1000 tables, and select only 100 ????? I have  :-(

With table names ordered and unselected by default, i believe that it will be much beter!

Nice work anyway.

2 November, 2007
Jim Clay

I have used the wizard and it does spead up things.

I agree, the Check/Uncheck All would be very beneficial.  Also the ability to automatically make associations would be great.

The other thing not mentioned by others is the ability to map views as well, rather than just tables.

19 March, 2008

Please login or register to post comments.