Click here to Skip to main content
15,886,137 members
Articles / Operating Systems / Windows
Article

From Tables to Business Objects

Rate me:
Please Sign up or sign in to vote.
2.75/5 (17 votes)
24 Nov 20032 min read 138.2K   1.4K   35   18
Describes how to generate ObjectSpaces business objects from SQL Server tables using CodeSmith.

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


Written By
Web Developer
United States United States
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.

Comments and Discussions

 
QuestionView version? Pin
rspiewak17-Jun-04 7:27
rspiewak17-Jun-04 7:27 
AnswerRe: View version? Pin
David Foderick22-Jun-04 5:28
David Foderick22-Jun-04 5:28 
GeneralObjectSpaces Samples for VS2005 CTP Pin
David Foderick9-May-04 10:00
David Foderick9-May-04 10:00 
GeneralRe: OK intro to CodeSmith, but Pin
Oskar Austegard1-Mar-04 3:48
Oskar Austegard1-Mar-04 3:48 
Generaltoo weak for practical use Pin
javadeveloper5-Dec-03 9:35
javadeveloper5-Dec-03 9:35 
GeneralRe: too weak for practical use Pin
David Foderick5-Dec-03 12:29
David Foderick5-Dec-03 12:29 
GeneralLooks good, but ... Pin
Martin Robins2-Dec-03 11:20
professionalMartin Robins2-Dec-03 11:20 
GeneralRe: Looks good, but ... Pin
David Foderick2-Dec-03 13:03
David Foderick2-Dec-03 13:03 
GeneralRe: Looks good, but ... Pin
T. K. Sarma21-Apr-04 5:11
sussT. K. Sarma21-Apr-04 5:11 
GeneralRe: Looks good, but ... Pin
David Foderick21-Apr-04 5:34
David Foderick21-Apr-04 5:34 
GeneralOK intro to CodeSmith, but Pin
Oskar Austegard2-Dec-03 3:46
Oskar Austegard2-Dec-03 3:46 
GeneralRe: OK intro to CodeSmith, but Pin
David Foderick2-Dec-03 8:10
David Foderick2-Dec-03 8:10 
GeneralRe: OK intro to CodeSmith, but Pin
Oskar Austegard2-Dec-03 9:14
Oskar Austegard2-Dec-03 9:14 
GeneralRe: OK intro to CodeSmith, but Pin
David Foderick29-Feb-04 18:06
David Foderick29-Feb-04 18:06 
GeneralSqlService Pin
carlcbcinc27-Nov-03 4:23
carlcbcinc27-Nov-03 4:23 
Hi,

Very nice ....

Question, Following the generation of the BusinessObject.cst can you tell me what the following line is actually doing ? I presume that service must already exist ? Could it be generated from that tool ? and can it be replace by any ADO.NET command object related to that table ?

SqlService sql = new SqlService();


Portion of the generated code
----------------------------------------------------------------------
#region Expense
///
/// This object represents the properties and methods of a Expense.
///

public class Expense
{
protected Guid _id;
protected string _clientID = String.Empty;
protected decimal _total;
protected decimal _tPS;
protected decimal _tVQ;
protected decimal _gtotal;
protected DateTime _dateFAC;
protected DateTime _datePAY;
protected string _description = String.Empty;
protected decimal _fl_type;
protected decimal _fl_status;
protected decimal _fl_category;
protected decimal _fl_source;
protected bool _defaut;
protected string _cheque = String.Empty;
protected decimal _fl_taxable;
protected decimal _notaxe;

public Expense()
{
}

public Expense(Guid id)
{
SqlService sql = new SqlService();
sql.AddParameter("@ID", SqlDbType.UniqueIdentifier, id);
SqlDataReader reader = sql.ExecuteSqlReader("SELECT * FROM Expense WHERE ID = '" + id.ToString() + "'");
GeneralRe: SqlService Pin
David Foderick27-Nov-03 7:11
David Foderick27-Nov-03 7:11 
GeneralRe: SqlService Pin
carlcbcinc27-Nov-03 9:14
carlcbcinc27-Nov-03 9:14 
GeneralRe: SqlService Pin
Eric J. Smith27-Nov-03 18:17
Eric J. Smith27-Nov-03 18:17 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.