Click here to Skip to main content
15,888,521 members
Articles / Desktop Programming / Windows Forms
Article

Code Generator using CodeSmith Api

Rate me:
Please Sign up or sign in to vote.
2.87/5 (19 votes)
8 Jan 20074 min read 128.3K   5.5K   61   31
This tool generate the c# code using the code smith templates and API

Sample Image - Code_Generator.jpg

Introduction

Before I start to explain what my code generator is and how it works, I want to let you people what thing have hit me to write this article and my code generator.

Well as all programmer knows that writing code for DAL and BL is time consuming and we write same lines of code for each project excepts some custom changing, so I was wondering by we take such burden every time there should some other fast, easiest , efficient way, so I started to explorer it and started working on that, as I progress I find CodeSmith (<a href="http: www.codesmithtools.com="" "="">www.codesmithtools.com) as a useful tool to achieve that.

CodeSmith enables software developers to efficiently:

  • Reduce repetitive coding
  • Generate your code in less time with fewer bugs
  • Produce consistent code that adheres to your standards
  • Create your own custom templates for any language

So I started to learn the codesmith, and luckly due to its easy structure I grasp its concept , working quickly.

Now I should explain the working of my software. First of I started to work on writing templates for myself. I stated building my own frame work, As I am beginner so started to understand the .netTiers Application Framework (<a href="http: www.nettiers.com="" "="">http://www.nettiers.com). After under standing this framework I started to build my framework and get some help from the .netTiers Application Framework.

My framework consist of the following classes

  • Entity Class
  • Entity Base Class
  • EntityController Class
  • EntityController Base Cclass
  • DataProvider Class
  • DataProvider Base Class
  • GenericList Class
  • CBO Class

Entity Class:

This class is inherited from the base class Entity Base Class, the entity class contain no function but you can override the virtual function in the base class, and if you want some custom function then you can write your own function in the entity class no in the base class.

Entity Base Class;

This class have the properties , methods to that should be required for the database table. Its an abstract class. This class have the following functions other then the constructor

  • Equals
  • GetHashCode

EntityController Class:

This class is inherited from the base class EntityController Base Class, this class can also override the virtual functions from the base class.

EntityController Base Class:

This class have the basic methods as follows

  • Add(with four overloaded methods
  • Update(with four overloaded methods)
  • Delete(with four overloaded methods)
  • GetPagedData(for getting data in the form of page)
  • DeleteAll
  • Get
  • GetAll
  • GetCustomView(to get the custom data base table columns
  • GetByPk
  • CountAll
  • GetByFk

This class call the function of the DataProvider class to interact with the database.

DataProvider Class:

This class is inherited from the base class DataProvider Base Class, this class can also override the virtual functions from the base class.

DataProvider Base Class:

This class actually interact with the database. For this purpose I uses the Microsoft Enterprise Library 2006. which provide efficient data access libraries. You can download the enterprise library from the Microsoft site (<a href="http: www.microsoft.com="" "="">www.microsoft.com).

GenericList Class:

In my framework for getting data in bundle I uses generic list instead of the datasets, for this I write the generic class.

CBO Class:

For converting the object to the required class or entity object I use this class.

CodeGenerator:

Image 2

I wrote CodeSmith templates for above mentions class, other then these to class I wrote an other template which was use to generate the store procedure which will be use by the methods in the DataProvider Base Class.

Now come to point why I write the application for compiling the CodeSmith templates that I have written, the problem was that to generate the code for the some database we have to compile each template for each table. So it take some time and also some hectic, so I develop the application to perform this work, I generate the code using all these templates by just info about the database. I develop this tool which uses CodeSmith API to compile the templates. Please install the CodeSmith 3.2 on your system to generate the code

Image 3

There was another class for the CodeSmith templates which was code behind class SqlScript.cs; I adopted this class from the .netTiers Application Framework (<a href="http: www.nettiers.com="" "="">http://www.nettiers.com). And have some custom function to be used in my framework.

Please let me know about your suggestions and comments thanks, and I am working on to version capability like dot net 2003 and dot net 2005. And also to compile the other templates also not specific related to my frame work

Name: Muhammad Shafqat Masood

Software Engineer (Nextbridge)

Contact: Napster_ghb@yahoo.com

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
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionfeedback Pin
ashok rathore18-Mar-14 0:58
ashok rathore18-Mar-14 0:58 
GeneralMy vote of 3 Pin
phoohtoo3-Mar-14 20:03
phoohtoo3-Mar-14 20:03 
GeneralMy vote of 5 Pin
Tehseen Qureshi23-Aug-12 3:54
Tehseen Qureshi23-Aug-12 3:54 
Generalmy vote of 5 Pin
88Rocker11-Nov-10 20:16
88Rocker11-Nov-10 20:16 
QuestionI have not license? Pin
taoquandecor1-Apr-10 19:13
taoquandecor1-Apr-10 19:13 
QuestionDoes it work with MySQL? Pin
KoiralaKiran7-Apr-09 6:24
KoiralaKiran7-Apr-09 6:24 
GeneralNot able work with newer Codesmith Pin
nikhil_ksa1-Aug-08 10:07
nikhil_ksa1-Aug-08 10:07 
GeneralRe: Not able work with newer Codesmith Pin
shafqatm3-Sep-08 19:49
shafqatm3-Sep-08 19:49 
General3q Pin
XuShiHao25-Mar-08 22:12
XuShiHao25-Mar-08 22:12 
Generalcould u please give me a code to add webreference dynamically to c# dll Pin
christefer15-Oct-07 19:11
christefer15-Oct-07 19:11 
GeneralCould you give a sample business logic class code Pin
sarzzaidi9-Oct-07 1:16
sarzzaidi9-Oct-07 1:16 
Generalits unable to load database name Pin
MCSDvikasmisra27-Jun-07 10:16
MCSDvikasmisra27-Jun-07 10:16 
GeneralRe: its unable to load database name Pin
napster_ghb17-Dec-07 3:24
napster_ghb17-Dec-07 3:24 
GeneralAdvantages of SmartCode Pin
Danilo Mendez26-May-07 14:40
Danilo Mendez26-May-07 14:40 
GeneralAnother Option!! Pin
Iqbal M Khan15-Feb-07 20:45
Iqbal M Khan15-Feb-07 20:45 
Tierdeveloper is an object to relational mapping (O/R mapping) code generator that lets you develop extremely high performance .NET applications in record time.

Read More:

http://www.alachisoft.com/rp.php?source=/codeproj.newsa/&dest=/tdev/overview.html[^http://www.alachisoft.com/rp.php?source=/codeproj.newsa/&dest=/download.html[^
QuestionWhat's the benefit? Pin
Chris Carter9-Jan-07 16:19
Chris Carter9-Jan-07 16:19 
AnswerRe: What's the benefit? Pin
napster_ghb9-Jan-07 20:29
napster_ghb9-Jan-07 20:29 
GeneralRe: What's the benefit? Pin
Eric J. Smith17-Sep-07 7:44
Eric J. Smith17-Sep-07 7:44 
QuestionWhere is the source code for your codeGenerator? Pin
devnet2478-Jan-07 23:10
devnet2478-Jan-07 23:10 
AnswerRe: Where is the source code for your codeGenerator? Pin
napster_ghb9-Jan-07 20:31
napster_ghb9-Jan-07 20:31 
GeneralRe: Where is the source code for your codeGenerator? Pin
Member 15407931-May-08 2:50
Member 15407931-May-08 2:50 
GeneralRe: Where is the source code for your codeGenerator? Pin
mcheschm24-Sep-08 8:28
mcheschm24-Sep-08 8:28 
GeneralRe: Where is the source code for your codeGenerator? Pin
shafqatm12-Nov-08 19:49
shafqatm12-Nov-08 19:49 
Generalcode generator Pin
Tanvir Ahmed8-Jan-07 19:57
Tanvir Ahmed8-Jan-07 19:57 
GeneralCode Generator Requirements Pin
napster_ghb8-Jan-07 19:45
napster_ghb8-Jan-07 19:45 

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.