Click here to Skip to main content
Click here to Skip to main content

From Tables to Business Objects

By , 24 Nov 2003
 

Introduction

Sometimes you need to automate the repetitive task of generating business objects from table schema. This article uses a free tool called CodeSmith to generate domain entity classes (a.k.a. business objects) for ObjectSpaces from your pre-existing database tables.

Background

ObjectSpaces is a persistence framework that will be available with .NET 2.0. A preview version was distributed at the PDC conference in October 2003. You can also obtain ObjectSpaces if you have a MSDN subscription.

A persistence layer, like ObjectSpaces, can simplify your life by dynamically (at runtime) generating the SQL statements that move data between memory and relational tables. That means that you do not write data access code for your business objects. Instead, you create a mapping file that maps the business classes and their properties to relational data tables and columns.

However, at the time I am writing this article, some necessary tools do not yet exist, hence the motivation for this article. One of the time consuming tasks when working with ObjectSpaces is generating the public properties that wrap the private fields. Besides being good object-oriented programming practice, using a public property to wrap a private field is a requirement for data binding to an ObjectSet. Therefore, as a policy, I always create my domain classes in this manner.

Using CodeSmith to generate ObjectSpaces entities

First, download the free CodeSmith code generation tool from the CodeSmith site. Run the msi to install it. CodeSmith installs into your Programs menu.

Next, if you are using Microsoft ObjectSpaces, you will want to download the templates that go along with this article. I have templates for generating both C# and VB.NET business classes for ObjectSpaces. Extract the two CodeSmith template files into the correct directory where CodeSmith lives. On my machine it goes into c:\Program Files\CodeSmith\Samples\.

Using CodeSmith is simplicity itself. Run the CodeSmith Explorer from your Programs menu.

CodeSmith Explorer

You can edit the template right from the Explorer by right-clicking on the .cst file and selecting edit. Execute the template/script by double-clicking it. Click the ellipse (...) next to Source Table to bring up the Table Picker. If it gives you an error, then click the ellipse after Data Source to edit the connection string to point to your database. Once you have a table selected, click the Generate button and Whala!, the source code for your business object appears in the Template Output window. Copy and paste this code into your Visual Studio source file. That's it!

Generated Business Object

Points of Interest

Eventually, the ObjectSpaces team will create integrated Visual Studio Add-ins. In the meantime, tools like CodeSmith will make your life easier.

History

Initial version - 11/21/2003

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

David Foderick
Web Developer
United States United States
Member
David Foderick is the owner of Net Knowledge, Inc in Southern California, focusing on Object-oriented Analysis and Design, Databases, and layered applications. He is a frequent presenter at local user groups and trainer with certifications in .NET (MCSD), SQL Server(MCDBA), UML and MSF. David received his BSEE from the University of Minnesota.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionView version?memberrspiewak17 Jun '04 - 7:27 
Thanks! This works fine for plain old VS.NET 2003 as well. Would it be easy to create a version which operates on Views instead of Tables?
 
Rick Spiewak
AnswerRe: View version?memberDavid Foderick22 Jun '04 - 5:28 
I don't see why not. I would think it would be an easy matter to get view schema from the db as well as table schema. Go to the CodeSmith support forum and see http://www.ericjsmith.net/codesmith/forum/.
 
BTW, with only slight modification to the script, the same idea can be used to create domain classes for other O/R Mapping tools like NHibernate, Wilson O/R Mapper etc.
 
Dave Foderick
Net Knowledge
 
~Dave Foderick
Net Knowledge
GeneralObjectSpaces Samples for VS2005 CTPmemberDavid Foderick9 May '04 - 10:00 
If you have Visual Studio 2005 (March Community Tech Preview) and you want to get started with ObjectSpaces you can download the ObjectSpaces Samples here: http://www.objectspaces.biz
 
~Dave Foderick
Net Knowledge
GeneralRe: OK intro to CodeSmith, butmemberOskar Austegard1 Mar '04 - 3:48 
Sure, come Whidbey, that'll work. Meanwhile you'll have to address it otherwise.Hmmm | :|
Generaltoo weak for practical usememberjavadeveloper5 Dec '03 - 9:35 
hi, great work but only for hello world examples. In my applications i have many relations and different datatypes. How can you create relations? How can you set DB types correctly : Oracle has no bool, db2 has other ones for this etc etc.
It is the same game like DataSets - in all small applications and in all MS examples you use DataSets and in real libe projects you have very rare DataSets (microsoft said: "prevent DataSets and better use structs as dataholders) - thats the truth Smile | :) But 90% of .NET developers dont work on big projects thay work on small office sites or apps.
 
That is a "hello world" game: Use it only with small applications and use it only with microsoft sql server etc. etc.
 
In practice 70 % of bigger projects use Oracle and db2 (IBM, BMW, DaimlerChrysler,...) and you have to watch performance if you work remotely. You need TOs and you need many outjer joins - but maybe you can save a bit time Smile | :)
 
But it is fine for a example how this could work and you can learn something.
In the future we will go more to declarative than object oriented programming.
 
OR is the past Smile | :) )
 

GeneralRe: too weak for practical usememberDavid Foderick5 Dec '03 - 12:29 

javadeveloper wrote:
How can you create relations?
 
You change it manually to Ilist or whatever your implementation of a collection is. If my utility did that (plus a lot more), it wouldn't be free! I would be charging hundreds of dollars for it!
 
javadeveloper wrote:
In the future we will go more to declarative than object oriented programming.
OR is the past )

 
Object-relational mapping is declarative programming. Your example for boolean exactly illustrates the benefit of ORM. Boolean is a type in the language and it is mapped to a sql bit type. If you implemented it for Oracle you would adjust the mapper, the generated code would not change.

 
~Dave Foderick
Net Knowledge
GeneralLooks good, but ...memberMartin Robins2 Dec '03 - 11:20 
You state within the article that as an MSDN subscriber, I should be able to get a preview copy of ObjectSpaces. Where can I get this? Does it work with VS2003?
 
I have looked at the available download list on MSDN (I have universal) and I can find no reference. A search of MSDN only brings up references within a couple of webcasts.
 
Thanks.
GeneralRe: Looks good, but ...memberDavid Foderick2 Dec '03 - 13:03 
As far as I know, if you did not attend PDC then you have to request Whidbey from MSDN Customer service. ObjectSpaces 1.0 only works on Whidbey.
 
Check out this link:
 
http://msdn.microsoft.com/events/pdc/after/default.aspx#longhorn
 

~Dave Foderick
Net Knowledge
GeneralRe: Looks good, but ...sussT. K. Sarma21 Apr '04 - 5:11 
Hi,
 
Do you have any idea when ObjectSpaces/Whidbey is expected to be released? If yes, please let me know.
 
Thanks,
 
T. K. Sarma
GeneralRe: Looks good, but ...memberDavid Foderick21 Apr '04 - 5:34 
The release date for Whidbey (Visual Studio 2005) isn't until the beginning of 2005. Until then, you have several choices:
1. Get your hands on the CTP (Whidbey Community Technology Preview). These were given out in DevDays or you can download through MSDN. They can be very unstable!
2. Wait for the first public beta of Whidbey which should appear sometime around July.
3. Buy Paul Wilson O/R Mapper for $50 which is an ObjectSpaces clone and works in .NET 1.1
 
Be aware that the release date of ObjectSpaces will come AFTER the release of Whidbey. Apparently it will be a separate download. That means the FINAL release of ObjectSpaces is probably 10 to 12 months away. Plan accordingly.
 
~Dave Foderick
Net Knowledge

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 25 Nov 2003
Article Copyright 2003 by David Foderick
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid