Friday, April 9, 2010

Using ASP.Net User Controls to Create Code Generator

I m a big fan of code generators. It can help us minimize the errors and reduces the development time. Although we can use T4(text template transformation toolkit) in .Net to create code generators, But I sometimes feel like to create my own tools because I enjoy the full control over my home made tool, and I know what is going on in my code. Do not get me wrong, I don't want to reinvent the wheel, it is just for learning purpose.
I want to use the power of User Controls in ASP.Net.
What I want to achieve is generating Entity class for every table in our database.

First, I create a user control for my entity class template called EntityTemplate.ascx

and Create the required properties in the code behind:
public string MyClassName {get; set;}
public DataColumnCollection MyColumns {get; set;}
public string MyNamespace { get; set;}

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EntityTemplate.ascx.cs" Inherits="Templates_EntityTemplate" %>
using System;
using System.Collections.Generic;
using Common.Core;

namespace <%=MyNamespace %>.BOL.Entities
public class <%= MyClassName %>
#region Members
<% for (int i = 0; i < MyColumns.Count; i++)
{ %>
public <%= MyColumns[i].DataType.Name%> <%= MyColumns[i].ColumnName%> {get;set;}

<%} %>
#endregion Members

Second, we create a web from to grab the list of all tables from our Database and for every table we grab the table schema to get the list of columns for that table. then for each table, we dynamically load that template user control and we set the properties, then we Use RenderControl method to get the output of that rendering user control and save it on the hard disk.

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlCilent");
DbConnection cn= factory.CreateConnection();

cn.ConnectionString = "Your Connection String";
DbDataAdapter da = factory.CreateDataAdapter();
DbCommand cmd = factory.CreateCommand();
cmd.Connection = cn;
da.SelectCommand = cmd;
cmd.CommandText = "SELECT name FROM sysobjects where type = 'U'";
cmd.CommandType = CommandType.Text;
DataTable dtTables = new DataTable();

System.Text.StringBuilder output = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(output);
HtmlTextWriter htw = new HtmlTextWriter(sw);

for(int i = 0; i< dtTables.Rows.Count; i++)
cmd.CommandText = "SELECT * FROM " + dtTables.Rows[i]["name"];
DataTable dt = new DataTable();
da.FillSchema(dt, SchemaType.Source);
Control ctrl = Page.LoadControl("Templates/EntityTemplate.ascx");
EntityTemplate EnCtrl = (EntityTemplate) ctrl;
EnCtrl.MyTableName = dtTables.Rows[i]["name"];
EnCtrl.MyClassName = dtTables.Rows[i]["name"];
EnCtrl.MyNamespace = "";
EnCtrl.MyColumns = dt.Columns;

System.IO.File.WriteAllText("BOL\\Entities\\" + dtTables.Rows[i]["name"] + ".cs", output.ToString());
output.Remove(0, output.Length);

hope you enjoy, Cheers!


Cyber Coder said...

I guess you should start living in 2010 instead of still living in 2005. If you need Code Generators for these examples, you are doing something hopelessly wrong.

Perhaps it's time you keep your stupid ideas to yourself instead of blogging it!

Saeed said...

hey mate, if u read the post, you will see that I am not saying this is the best way to generate code, it is only for learning purpose and showing the power of user controls, but anyway, you are the first person to comment on my blog. Thanks for the comment,I try to start living in 2010 but I don't promise,

Post a Comment