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

nTier .Net Code Generator

By , 16 Jul 2009
Rate this:
Please Sign up or sign in to vote.

Download DotNETCodeGeneratorSourceWithoutNETXP.zip - 569.8 KB Download

Download DotNETCodeGeneratorWithNETXP.zip - 1.26 MB

Introduction

Every one has shortage of time , every one wants short cuts. Why don't we have (being developers) short cuts to developing database applications. This is what Dot NET Code Generator tries to give you. It generates full object oriented source code and even all the forms for you using 3 layer architecture .

Background

When I started development, changing customer requirements caused me a lot of trouble.
Whenever a major change occurred I�ve to do a lot of coding to accommodate that change .After being worried for some time I decided to automate the coding process, so that I Don�t have to write repetitive coding tasks again and again.

Using the code

This Application is not developed using specific design in mind, it is developed using old style structured programming, but the code it generates is complete Object oriented which takes benefits of inheritance, polymorphism and encapsulation. All code is generated using 3 layer architecture. I�ve used meta data provided by SQL Server�s Master Database to Generate all the code . if you open master database�s sotred procedure section you�ll see a lot of stored procedures prefixed by sp_ these procedures provide you much information sufficient for our code generator .
I�ve used following stored procedures.

  • sp_databases
  • sp_tables
  • sp_Columns
  • sp_primary_keys_Rowset
  • sp_fkeys

sp_databases returns all databases attached with current instance of Sql server all these stored procedures are self explanatory , I�ll use simple �Sample � database (to explain the code) which only has two tables one tblMaster and other tblDetail .

Database is intentionally kept simple for simplicity. Once we have our database we can use sp_tables and pass our database name as argument this will return all the tables with in our database in our case tblMaster and tblDetail respectively we�ll store these tables temporarily , so that we can loop through all the tables and generate the code . Following Code snippet shows you what actually happens .

for(int i=0 ; i < lstSelectedTables.Length ; i++)
{
    string tblName = lstSelectedTables[i][2].ToString();
        if(cbolanguage.Text == CSharp")
           GenerateCSharpCode(tblName);
    else
          GenerateVBCode(tblName);
    if((chkgui.Checked ==false) && (chkdata.Checked ==false)&& (chkinterface.Checked 
  ==false)&& (chkprocedures.Checked ==false))
  {
  MessageBox.Show(Please select at least one code option");
  break;
  }
  }
public void GenerateDataClasses(string tblName,string tbldetail)
  {
  FileStream file ;
  file = new FileStream(tempPath + \\cls_d" + tblName+ .cs",System.IO.FileMode.Create);
  StreamWriter sw = new StreamWriter(file);
  sw.WriteLine(using System;\n");
  sw.WriteLine(namespace " + txtprojectname.Text + .DataClasses" 
  );
  sw.WriteLine({");
  sw.WriteLine(\t/// <summary>");
  sw.WriteLine(\t/// Summary description for cls_d" + tblName+ .");
  sw.WriteLine(\t/// </summary>\n");
  sw.WriteLine(\tpublic class cls_d" + tblName);
  sw.WriteLine(\t{");
  GenerateAttributes(tblName,sw,tbldetail);
  sw.WriteLine(\t}");//End Class Here
  sw.WriteLine(}");//End Namespace Here
  sw.Flush();
  file.Close();
  txtprogress.AppendText(cls_d" + tblName+ .cs Created \n")   ;
 }
  public void GenerateAttributes(string TableName , StreamWriter sw,string tbldetail)
  {
  try
  {
  string type;
  DataTable dtColumns ;
  dbobj = new dboperation(constr);
  dbobj.objcmd.CommandText = exec sp_Columns " + TableName ;
  dtColumns = dbobj.filldata().Tables[0];
  for(int i = 0 ; i < dtColumns.Rows.Count ; i ++ )
  {
  type = GetType(dtColumns.Rows[i][5].ToString());
  sw.WriteLine(\t\tprivate " + type +  m" + dtColumns.Rows[i][3].ToString()   + ;");
  } 
    if(tbldetail != ")
  sw.WriteLine(\t\tprivate string mDetail;");
  sw.WriteLine(\n\t\t#  region Constructor");
  GenerateConstructor(dtColumns,sw,tbldetail);
  sw.WriteLine(\t\t#end  region\n");
  sw.WriteLine(\t\t#  region Properties");
  GenerateProperties(dtColumns,sw,tbldetail);
  sw.WriteLine(\t\t#end  region\n"); }
    catch(Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
 }
  public void GenerateConstructor(DataTable dtColumns,StreamWriter sw,string tbldetail)
  {
  string tblname = dtColumns.Rows[0][2].ToString();
  sw.WriteLine("\n\t\tpublic cls_d" + tblname + ()");
  sw.WriteLine("\t\t{");
  sw.WriteLine("\t\t\t //");  sw.WriteLine("\t\t\t // TODO: Add constructor logic here");
  sw.WriteLine("\t\t\t //");
  for(int i = 0 ; i < dtColumns.Rows.Count ; i++ )
  {
    if(GetInitValue(dtColumns.Rows[i][5].ToString()) != ")
  sw.WriteLine("\t\t\t" + dtColumns.Rows[i][3].ToString() +  = 
  " + GetInitValue(dtColumns.Rows[i][5].ToString()) + ;" );
    else
  sw.WriteLine("\t\t\t" + dtColumns.Rows[i][3].ToString() +  = 
  \"\" ;" );
 }
    if(tbldetail != ")
  sw.WriteLine("\t\t\tmDetail = \"\" ;" );
  sw.WriteLine("\t\t}");
 }
 public void GenerateProperties(DataTable dtColumns,StreamWriter sw,string 
  tbldetail)
  {
  string type;
  for(int i = 0 ; i < dtColumns.Rows.Count ; i ++ )
  {
  type = GetType(dtColumns.Rows[i][5].ToString());
  sw.WriteLine("\t\tpublic " + type +  " + dtColumns.Rows[i][3].ToString()   );
  sw.WriteLine("\t\t{");
  sw.WriteLine("\t\t\tget");
  sw.WriteLine("\t\t\t{");
  sw.WriteLine("\t\t\t\t  return m" + dtColumns.Rows[i][3].ToString()   + ;");
  sw.WriteLine("\t\t\t}");
  sw.WriteLine("\t\t\tset");
  sw.WriteLine("\t\t\t{");
  sw.WriteLine("\t\t\t\tm" + dtColumns.Rows[i][3].ToString() +    = value ;");
  sw.WriteLine("\t\t\t}");
  sw.WriteLine("\t\t}");  
  } 
    if(tbldetail != ")
  {
  sw.WriteLine("\t\tpublic string Detail" );
  sw.WriteLine("\t\t{");\
  sw.WriteLine("\t\t\tget");
  sw.WriteLine("\t\t\t{");
  sw.WriteLine("\t\t\t\t  return mDetail;");
  sw.WriteLine("\t\t\t}");
  sw.WriteLine("\t\t\tset");
  sw.WriteLine("\t\t\t{");
  sw.WriteLine("\t\t\t\tmDetail = value ;");
  sw.WriteLine("\t\t\t}");
  sw.WriteLine("\t\t}");
  }  }

Generated Code

Here is the sample code generated by our code generator.

using System;

namespace Sample.DataClasses

  {
  /// < class=summary class=>
  /// Summary description for cls_dtblDetail.
  /// < class=/ class=summary class=>
public class cls_dtblDetail
{
  private int mDetailID;
  private int mMasterID;
  private string mDetailFieldOne;
  private DateTime mDetailFieldTwo;
  private string mDetailFieldThree;
 #  region Constructor
 public cls_dtblDetail()
{
   // TODO: Add constructor logic here
  DetailID = 0;
  MasterID = 0;
  DetailFieldOne = " ;
  DetailFieldTwo = System.DateTime.Now.Date;
  DetailFieldThree = " ;
}
  #end  region
 #  region Properties
  public int DetailID
 {
  get
  {
    return mDetailID;
  }
  set
  {
  mDetailID = value ;
  }
  }
  public int MasterID
  {
  get
  {
    return mMasterID;
  }
  set
  {
  mMasterID = value ;
  }
  }
  public string DetailFieldOne
  {
  get
  {
    return mDetailFieldOne;
  }
  set  {
  mDetailFieldOne = value ;
  }
  }
  public DateTime DetailFieldTwo
  {
  get
  {
    return mDetailFieldTwo;
  }
  set
  {
  mDetailFieldTwo = value ;
  }
  }
  public string DetailFieldThree
  {
  get
  {
    return mDetailFieldThree;
  }
  set
  {
  mDetailFieldThree = value ;
  }
  }
  #end  region
 }
  }
Interface class is as follow.
using System;
  using System.Data;
  using System.Data.SqlClient;
  using Sample.DataClasses;
  using Sample.Utility;
  namespace Sample.Interface
  {
  /// < class=summary class=>
  /// Summary description for cls_itblDetail.
  /// < class=/ class=summary class=>
  public class cls_itblDetail
  {
 private dboperation dbobj ;
  private cls_dtblDetail mData;
  public string m_Error;
 #  region Constructor
  public cls_itblDetail()
  {
  //
  // TODO: Add constructor logic here
  //
  mData = new cls_dtblDetail();
  }
  #end  region
 #  region Methods
  public bool AddtblDetail(cls_dtblDetail pData)
  {
  try
  {
  mData = pData;
  dbobj = new dboperation();
  dbobj.objcmd.CommandType = CommandType.StoredProcedure;
  dbobj.objcmd.CommandText = addtblDetail" ;
  dbobj.objcmd.Parameters.Add(@MasterID" , mData.MasterID);
  dbobj.objcmd.Parameters.Add(@DetailFieldOne" , mData.DetailFieldOne);
  dbobj.objcmd.Parameters.Add(@DetailFieldTwo" , mData.DetailFieldTwo);
  dbobj.objcmd.Parameters.Add(@DetailFieldThree" , mData.DetailFieldThree);
  dbobj.executeQuery();
  }    catch(Exception ex)
  {
  m_Error = ex.Message;
    return false;
  }
    return true;
  }
  public bool UpdatetblDetail(cls_dtblDetail pData )
  {
  try
  {
  mData = pData;
  dbobj = new dboperation();
  dbobj.objcmd.CommandType = CommandType.StoredProcedure;
  dbobj.objcmd.CommandText = updatetblDetail" ;
  dbobj.objcmd.Parameters.Add(@DetailID" , mData.DetailID);
  dbobj.objcmd.Parameters.Add(@MasterID" , mData.MasterID);
  dbobj.objcmd.Parameters.Add(@DetailFieldOne" , mData.DetailFieldOne);
  dbobj.objcmd.Parameters.Add(@DetailFieldTwo" , mData.DetailFieldTwo);
  dbobj.objcmd.Parameters.Add(@DetailFieldThree" , mData.DetailFieldThree);
  dbobj.executeQuery();
  }
    catch(Exception ex)
  {
  m_Error = ex.Message;
    return false;
  }
    return true;
  }
  public bool DeletetblDetail(cls_dtblDetail pData)
  {
  try
  {
  mData = pData;
  dbobj = new dboperation();
  dbobj.objcmd.CommandType = CommandType.StoredProcedure;
  dbobj.objcmd.CommandText = deletetblDetail" ;
  dbobj.objcmd.Parameters.Add(@DetailID" , mData.DetailID);
  dbobj.executeQuery();
  }
    catch(Exception ex)
  {
  m_Error = ex.Message;
    return false;
  }
    return true;
  }
  public DataSet GettblDetail()
  {
  DataSet ds = new DataSet();
  try
  {
  dbobj = new dboperation();
  dbobj.objcmd.CommandType = CommandType.StoredProcedure;
  dbobj.objcmd.CommandText = gettblDetail" ;
  ds = dbobj.filldata();
  }
    catch(Exception ex)
  {
  m_Error = ex.Message;
    return null;
  }
    return ds;
  }
  public DataSet GettblDetailThroughQuery(string qry)
  {
  DataSet ds = new DataSet();
  try
  {
  dbobj = new dboperation();
  dbobj.objcmd.CommandType = CommandType.Text;
  dbobj.objcmd.CommandText = qry ;
  ds = dbobj.filldata();
  }
    catch(Exception ex)
  {
  m_Error = ex.Message;
    return null;
  }
    return ds;
  }
  #endregion
 }
  }

License

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

About the Author

Asif Ahmed Khan
Software Developer (Senior)
Pakistan Pakistan
i'm Asif Ahmed Khan i've Bcs(Hons)degree in computer science and has been using .NET especially CSharp and VB.NET since 2005 . I like to develop tools to reduce time evolved in development , so one can have more time to understand the problem rather than implementation.

Comments and Discussions

 
General[My vote of 2] ORM PinmemberDonsw3-Oct-09 18:53 
Questionconecting strings with + instead of string.format? PinmemberSpringwald23-Jul-09 1:38 
QuestionIs it possible to use Microsoft Enterprise Library (DAAB)? PinmemberHemant.Kamalakar20-Jul-09 19:42 
GeneralMy vote of 1 PinmemberKinStephen20-Jul-09 8:21 
Generalcan't use it at all in any place PinmemberTeBeCo17-Jul-09 5:02 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 16 Jul 2009
Article Copyright 2009 by Asif Ahmed Khan
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid