Click here to Skip to main content
12,882,639 members (32,248 online)
Click here to Skip to main content
Add your own
alternative version


290 bookmarked
Posted 26 Jul 2008

Tier Generator 1.0

, 26 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A powerful tool for rapid application development.



Tier Generator is a powerful tool for generating business and data layers in C#. It is a code generation tool that helps users to rapidly generate and deploy business and data layers for their applications. The idea behind this is to provide a utility (tool) to the developer that has the capabilities of quickly generating consistent and tested source code that will help to get projects started sooner and finished faster.

Tier Generator connects to a Microsoft SQL Server database server and generates business and data layers in C#. It also generates Stored Procedures for DML operations.

Business layer

Tier Generator generates code in two layers (business and data). It generates some classes for the each table in the database in the business layer. E.g., our database contains the table Employee. Tier Generator will generate the following files:

  • Employee
  • EmployeeKeys
  • EmployeeFactory

The Employee (business object) class contains the declaration of all instance fields along with properties. It also overrides the AddValidationRules method to associate the validation rules to the properties of the business object. It also contains an enum of all the fields.

public class Employee: BusinessObjectBase
   #region InnerClass

   public enum EmployeeFields
     EmployeeID, Name, Password, Email, TeamID,DepartmentID, IsAdmin    

   #region Data Members

   int _employeeID;
   string _name;
   string _password;
   string _email;
   int _teamID;
   int _departmentID;
   bool _isAdmin;


   #region Properties

   public int  EmployeeID
     get { return _employeeID; }
        if (_employeeID != value)
           _employeeID = value;

   public string  Name
     get { return _name; }
       if (_name != value)
         _name = value;

   #region Validation

   internal override void AddValidationRules()
     ValidationRules.AddRules(new Validation.ValidateRuleNotNull("EmployeeID", 
     ValidationRules.AddRules(new Validation.ValidateRuleNotNull("Name", 
     ValidationRules.AddRules(new Validation.ValidateRuleStringMaxLength("Name", 
     ValidationRules.AddRules(new Validation.ValidateRuleStringMaxLength("Password", 
     ValidationRules.AddRules(new Validation.ValidateRuleStringMaxLength("Email", 
     ValidationRules.AddRules(new Validation.ValidateRuleNotNull("TeamID", 
     ValidationRules.AddRules(new Validation.ValidateRuleNotNull("DepartmentID", 
     ValidationRules.AddRules(new Validation.ValidateRuleNotNull("IsAdmin", 


The EmpolyeesKeys (business object keys) class contains the list of primary keys of the table.

public class EmployeeKeys
    #region Data Members

    int _employeeID;


    #region Constructor

    public EmployeeKeys(int employeeID)
      _employeeID = employeeID; 


    #region Properties

    public int  EmployeeID
      get { return _employeeID; }



The EmployeeFactory (business factory) class contains the methods for the Insert, Delete, Update, and Select operations. It provides the following methods for the DML operations:

  • public bool Insert(Employee businessObject)
  • public bool Update(Employee businessObject)
  • public Employee GetByPrimaryKey(EmployeeKeys keys)
  • public List<Employee> GetAll()
  • public List<Employee> GetAllBy(Employee.EmployeeFields fieldName, object value)
  • public bool Delete(EmployeeKeys keys)
  • public bool Delete(Employee.EmployeeFields fieldName, object value)

The factory class performs the DML operations with the help of the data layer.

public class EmployeeFactory
    #region data Members

    EmployeeSql _dataObject = null;


    #region Constructor

    public EmployeeFactory()
      _dataObject = new EmployeeSql();


    #region Public Methods

    public bool Insert(Employee businessObject)
       if (!businessObject.IsValid)
          throw new InvalidBusinessObjectException(

        return _dataObject.Insert(businessObject);


    public bool Update(Employee businessObject)
      if (!businessObject.IsValid)
        throw new InvalidBusinessObjectException(
      return _dataObject.Update(businessObject);

    public Employee GetByPrimaryKey(EmployeeKeys keys)
       return _dataObject.SelectByPrimaryKey(keys); 

    public List<Employee> GetAll()
       return _dataObject.SelectAll(); 

    public List<Employee> GetAllBy(Employee.EmployeeFields fieldName, 
                                         object value)
      return _dataObject.SelectByField(fieldName.ToString(), value);  

    public bool Delete(EmployeeKeys keys)
       return _dataObject.Delete(keys); 

    public bool Delete(Employee.EmployeeFields fieldName, object value)
       return _dataObject.DeleteByField(fieldName.ToString(), value); 



Data Layer

The data access file generated by the Tier Generator contains the methods for DML operations. It uses Stored Procedures for DML operations. The factory class methods call the data layer methods for insertion and deletion.

class EmployeeSql : DataLayerBase 
   #region Public Methods

   /// <span class="code-SummaryComment"><summary></span>
   /// insert new row in the table
   /// <span class="code-SummaryComment"></summary></span>
   /// <span class="code-SummaryComment"><param name="businessObject">business object</param></span>
   /// <span class="code-SummaryComment"><returns>true of successfully insert</returns></span>
   public bool Insert(Employee businessObject)
     SqlCommand    sqlCommand = new SqlCommand();
       sqlCommand.CommandText = "dbo.[sp_Employee_Insert]";
       sqlCommand.CommandType = CommandType.StoredProcedure;

     // Use base class' connection object
     sqlCommand.Connection = MainConnection;

       sqlCommand.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4, 
                                                  false, 0, 0, "", 
       sqlCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar, 
                                                  50, ParameterDirection.Input, 
                                                  false, 0, 0, "", 
       sqlCommand.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 
                                                  50, ParameterDirection.Input, 
                                                  false, 0, 0, "", 
       sqlCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar, 
                                                  100, ParameterDirection.Input, 
                                                  false, 0, 0, "", 
       sqlCommand.Parameters.Add(new SqlParameter("@TeamID", SqlDbType.Int, 
                                                  4, ParameterDirection.Input, 
                                                  false, 0, 0, "", 
       sqlCommand.Parameters.Add(new SqlParameter("@DepartmentID", SqlDbType.Int, 
                                                  4, ParameterDirection.Input, 
                                                  false, 0, 0, "", 
       sqlCommand.Parameters.Add(new SqlParameter("@IsAdmin", SqlDbType.Bit, 
                                                  1, ParameterDirection.Input, 
                                                  false, 0, 0, "", 
       businessObject.EmployeeID = 

       return true;
     catch(Exception ex)
       throw new Exception("Employee::Insert::Error occured.", ex);


How to use

The code generated by the Tier Generator is easy to use. Open the generated project in Visual Studio 2005 and compile it. Run the Stored Procedures script in the the database which is generated by the Tier Generator. You can find the SQL script file in the generated folder.

Add a new Windows/web project in the existing project and add the DLL of the generated code to it. Add app.config for Windows applications and web.config for web applications. Get the connection string from the generated app.config file. You will get this file in the generated folder.

   <add key="Main.ConnectionString"
           value="Data Source=localhost;Initial Catalog=School;
                  User Id=sa;Password=sa" />

Here is the code sample for inserting a new record:

public void AddNewRecord()
    Employee emp = new Employee();
    emp.EmployeeID = 1;
    emp.FirstName = "Shakeel";
    emp.LastName = "Iqbal";
    EmployeeFactory empFact = new EmployeeFactory();

The code sample for selecting all the records:

public void SelectAll()
     EmployeeFactory empFact = new EmployeeFactory();
     List<Employee> list = empFact.GetAll();   
    dataGrid1.DataSource = list;

Future enhancements

I have some future enhancements planned for the Tier Generator, and I have plans to launch the next version of the Tier generator. In this version, I will improve my business and data layers, and I will also provide the following features:

  • Generate Windows application.
  • Generate Web application.
  • Generate Web Services.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shakeel Iqbal
Software Developer (Senior) TEO
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

You may also be interested in...

Comments and Discussions

GeneralRe: Abandoned? Pin
Shakeel Iqbal2-Jan-10 5:48
memberShakeel Iqbal2-Jan-10 5:48 
GeneralRe: Abandoned? Pin
ipadilla3-Jan-10 1:10
memberipadilla3-Jan-10 1:10 
GeneralHave you abandoned this project Pin
VenerableBede16-Dec-09 3:38
memberVenerableBede16-Dec-09 3:38 
GeneralRe: Have you abandoned this project Pin
Shakeel Iqbal2-Jan-10 5:50
memberShakeel Iqbal2-Jan-10 5:50 
GeneralRe: Have you abandoned this project Pin
gravbox8-Jan-10 16:37
membergravbox8-Jan-10 16:37 
GeneralAdapted for SQL Compact Pin
Atlas20024-Dec-09 4:34
memberAtlas20024-Dec-09 4:34 
GeneralImpressive Pin
VenerableBede25-Nov-09 2:28
memberVenerableBede25-Nov-09 2:28 
GeneralPlease Include BugList too Pin
Member 159112717-Aug-09 1:14
memberMember 159112717-Aug-09 1:14 
GeneralGreat Solution but few bugs and enhancements required Pin
Member 159112717-Aug-09 1:08
memberMember 159112717-Aug-09 1:08 
GeneralSQL 2005 Pin
mtone15-Jul-09 9:21
membermtone15-Jul-09 9:21 
GeneralRe: SQL 2005 Pin
Shakeel Iqbal16-Jul-09 18:50
memberShakeel Iqbal16-Jul-09 18:50 
Generalclosing Datareader Pin
flyingbaobao14-May-09 17:07
memberflyingbaobao14-May-09 17:07 
GeneralWell done! Pin
Member 27491296-Apr-09 7:52
memberMember 27491296-Apr-09 7:52 
GeneralMulti Table Pin
Dawit Kiros1-Apr-09 20:02
memberDawit Kiros1-Apr-09 20:02 
GeneralRe: Multi Table Pin
Shakeel Iqbal2-Apr-09 20:13
memberShakeel Iqbal2-Apr-09 20:13 
GeneralRe: Multi Table Pin
Dawit Kiros4-Apr-09 8:31
memberDawit Kiros4-Apr-09 8:31 
Question[Message Deleted] Pin
Dawit Kiros17-Mar-09 8:46
memberDawit Kiros17-Mar-09 8:46 
AnswerRe: Badley need the Enhancement.......... Pin
Shakeel Iqbal17-Mar-09 8:59
memberShakeel Iqbal17-Mar-09 8:59 
QuestionHow can I use Transactions with this tier? Pin
Member 21090139-Mar-09 14:48
memberMember 21090139-Mar-09 14:48 
AnswerRe: How can I use Transactions with this tier? Pin
Shakeel Iqbal10-Mar-09 5:49
memberShakeel Iqbal10-Mar-09 5:49 
GeneralRe: How can I use Transactions with this tier? Pin
Member 210901310-Mar-09 18:32
memberMember 210901310-Mar-09 18:32 
GeneralExcellent work Pin
ipadilla21-Jan-09 21:38
memberipadilla21-Jan-09 21:38 
GeneralNice Work Pin
ArpitDubey14-Nov-08 6:12
memberArpitDubey14-Nov-08 6:12 
Generalmulti database Pin
zhaojicheng2-Nov-08 17:14
memberzhaojicheng2-Nov-08 17:14 
GeneralRe: multi database Pin
Shakeel Iqbal2-Nov-08 21:15
memberShakeel Iqbal2-Nov-08 21:15 
GeneralRe: multi database Pin
zhaojicheng3-Nov-08 3:01
memberzhaojicheng3-Nov-08 3:01 
GeneralMYSQL Databases Pin
Gokhan Mamaci30-Oct-08 8:59
memberGokhan Mamaci30-Oct-08 8:59 
GeneralRe: MYSQL Databases Pin
Shakeel Iqbal31-Oct-08 8:18
memberShakeel Iqbal31-Oct-08 8:18 
GeneralMessage Removed Pin
Katekortez25-Oct-08 9:29
memberKatekortez25-Oct-08 9:29 
GeneralPartial classes and a few other enhancements Pin
Karijn22-Oct-08 22:57
memberKarijn22-Oct-08 22:57 
QuestionIs there anyone to address my problems? Pin
bilal haider9-Oct-08 23:49
memberbilal haider9-Oct-08 23:49 
QuestionNice work and 2 questions? Pin
bilal haider8-Oct-08 2:49
memberbilal haider8-Oct-08 2:49 
AnswerRe: Nice work and 2 questions? Pin
Shakeel Iqbal10-Oct-08 4:39
memberShakeel Iqbal10-Oct-08 4:39 
GeneralShould ignore timestamp field Pin
mac Moraleja23-Sep-08 22:27
membermac Moraleja23-Sep-08 22:27 
GeneralRe: Should ignore timestamp field Pin
Shakeel Iqbal24-Sep-08 0:13
memberShakeel Iqbal24-Sep-08 0:13 
GeneralDatabase Case Sensitivity Issue Pin
Dave Herron8-Sep-08 8:01
memberDave Herron8-Sep-08 8:01 
GeneralExcellent! Pin
loploplop8-Sep-08 4:18
memberloploplop8-Sep-08 4:18 
GeneralNice work ;-) Pin
Asif Qadri3-Sep-08 17:24
memberAsif Qadri3-Sep-08 17:24 
GeneralNice! Pin
Spiff Dog2-Sep-08 21:48
memberSpiff Dog2-Sep-08 21:48 
Generalyou got my 5 Pin
black_zero27-Aug-08 19:29
memberblack_zero27-Aug-08 19:29 
GeneralRe: you got my 5 Pin
Shakeel Iqbal28-Aug-08 0:53
memberShakeel Iqbal28-Aug-08 0:53 
GeneralVery nice tool , But Pin
black_zero27-Aug-08 19:28
memberblack_zero27-Aug-08 19:28 
GeneralGreat Work n Congrats on winning prize Pin
Aftab Zaheer Satti26-Aug-08 19:58
memberAftab Zaheer Satti26-Aug-08 19:58 
QuestionSchema bug [modified] Pin
mm-2k31-Jul-08 23:11
membermm-2k31-Jul-08 23:11 
QuestionVery nice, but I have a request Pin
tvlep30-Jul-08 10:52
membertvlep30-Jul-08 10:52 
AnswerRe: Very nice, but I have a request Pin
Shakeel Iqbal30-Jul-08 20:46
memberShakeel Iqbal30-Jul-08 20:46 
GeneralRe: Very nice, but I have a request Pin
tvlep31-Jul-08 6:07
membertvlep31-Jul-08 6:07 
GeneralRe: Very nice, but I have a request Pin
Shakeel Iqbal31-Jul-08 7:35
memberShakeel Iqbal31-Jul-08 7:35 
GeneralFabulous Work Pin
Afaak30-Jul-08 6:46
memberAfaak30-Jul-08 6:46 
GeneralRe: Fabulous Work Pin
Shakeel Iqbal30-Jul-08 6:48
memberShakeel Iqbal30-Jul-08 6:48 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 26 Jul 2008
Article Copyright 2008 by Shakeel Iqbal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid