C# Data Object Code Builder

by Andrew Barber 31. January 2009 14:29

In my day-to-day programming work, one of the most frequent - and least exciting - things I do is create data object classes. These classes use the data access layer code to interact with the persistent storage mechanism, and then present a nice, neat object-oriented package to the presentation layer for databinding and any other operations. Business rule logic can easily be created within these to enforce whatever must be enforced, again taking some of the weight off the presentation layer (whether Windows Forms or a Web Application).

However, it is quite, well, boring to create all that code. Fields in a data store must have matching representations created in private member variables and public property accessors. Functions for adding, updating and deleting records must be created, with the requisite creation of Parameter objects for each field at issue. Constructors to load new records, existing records, and collections of records must be created, again linking the data in the data store with the data in the object. It's a lot of 'plumbing' to do, and it creates a temptation to avoid using good, N-tier practices, and just access the data directly within the presentation layer code. You would never do that, would you? I used to consider it all the time, until I developed the original version of the DataClassBuilder.

About the Program

DataClassBuilder is a .NET 2.0 WinForms program which accepts database schema information and builds a C# class with the basics to access that data in an object-oriented manner, effectively enabling separation between data access and presentation. This tool is meant for those who frequently must create middle-tier data objects in C# to encapsulate and separate presentation from data access and business rules.

The tool includes T-SQL code necessary both to generate the schema information it requires, and it also dynamically generates scripts to create SQL Server Stored Procedures (Sprocs) which it uses in the code for adding, updating and deleting records. It implements all the basic 'plumbing' code needed for a data object for databinding, and is especially suited to be used as an instance object in a Table Data Gateway (TDG) pattern. One-To-Many relationships can be represented by creating objects for the foreign key items as well, and linking them by adding appropriate Collection-based members to the root object. Constructors by ID, IDataReader and default empty are generated for instantiation of single record objects, collections based on the result of a query and new objects, respectively.

This code is released to the public domain, and is provided 'As-Is', with absolutely no representation as to its suitability for any purpose whatsoever.

How It's Used

To use the program, either build the included project or navigate to the /bin/debug/ directory and execute it from there. The program is completely non-destructive, and makes no network connections of any kind. It merely produces plain text for a C# class which represents and accesses the database table the schema of which you have given it.

The 'Namespace' and 'Class Name' boxes can contain any values you wish, and will simply be output verbatim in the generated code. Table Name must be the correct name of the database table. Click on 'Read Me' (or open the README.txt file) and navigate to the bottom to find T-SQL code which will output the information required in the Tab-Separated Field Info box. All fields must be included for the resulting code to work properly. Also, the generator expects that the first field listed is the primary key field. However, you can manually adjust the code easily to account for any other PK mechanism you may have.

What it Does

Once you have entered all the information, click 'Go' and a ready-to-customize C# class is generated with the following features:

  • Private member variable for all fields; camel-cased and underscore-prefixed.
  • Public property accessors for all fields, with get; and set; implemented, with names matching the database fields. Alter as you see fit, including removing set; accessors for database-generated values.
  • Functions for inserting, updating and deleting records, using parameterized T-SQL stored procedures which are also generated by the code.
  • Static, dynamically generated member variables for all *Char-based fields to assure that each time your application runs, it has the correct field size information. This is used for setting parameters, and can also be used to enforce maximum lengths of text boxes and other controls in Web/WinForms. A T-SQL Sproc is included to retrieve this information as well.
  • All database field names and sizes are represented by member constants, and those constants are used in the creation of all SqlParameter objects. This helps when customizing the class; letting Intellisense and the compiler warn you about misspellings of field names, avoiding aggravating run-time errors.
  • Non-Destructive; All it does is create C#/T-SQL code. Try it out, and see if it could be of use to help create the boilerplate code for your data objects.
  • Includes and uses a Data access class which wraps many common data scenarios, and prevents the need to code with DbConnection and DbCommand objects. IDataReader functions use the CommandBehavior.CloseConnection value to assure that when you are done with a Data Reader, the underlying connection is also closed. Note: You may need to alter the namespace of the included Data class to match your generated code.

Download DataClassBuilder

You may get DataClassBuilder v1.0.3318 (January 31, 2009) by clicking here.

Why Eels?

No one can really be certain. But those slimey underwater critters obviously have something going for them!

Links/Profile

Andrew Barber's Profiles:
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent the views of employees, contractors or clients of Inkwell Creative Group, LLC in any way.

© Copyright 2008, 2009 Andrew Barber