How many times you have repeated the same code over and over and over again? Sometimes we all really get tired of writing the same old code. In this article I will introduce to you Code Generation, which means code will write code for you. We will be using XSLT to create a simple template to generate entity classes based on the XML file.

Introduction:

How many times you have repeated the same code over and over and over again? Sometimes we all really get tired of writing the same old code. In this article I will introduce to you Code Generation, which means code will write code for you. We will be using XSLT to create a simple template to generate entity classes based on the XML file.

What is Code Generation?

The idea behind code generation is that we don't have to type the same repetitive code again and again. Just like Microsoft Word has several templates which let's you make the document quickly. These templates include resume template, calendar template etc. We are going to develop a template which will makes is easy for us to write entity classes.

XML FILE:

The XML file contains all the variable names, method names and properties names along with additional information. Here is small code from the XML file. You can download the complete XML file (The file is also included in the zip folder at the bottom). 

<field>

<scope>public</scope>

<name>_userID</name>

<datatype>int</datatype>

<initialvalue>0</initialvalue>

</field>

<property>

<scope>public</scope>

<fieldname>_userID</fieldname>

<name>UserID</name>

<datatype>int</datatype>

</property>

<method>

<scope>public</scope>

<name>GetAllUsers</name>

<return>DataSet</return>

<parameters>

<parameter>

<datatype>int</datatype>

<name>userID</name>

</parameter>

<parameter>

<datatype>string</datatype>

<name>firstName</name>

</parameter>

</parameters>

</method>

 

XSLT File:

XSLT file will transform the XML file into a particular format. Here is part of the XSLT file. You can download the complete XSLT file (XSLT file is also included in the zip folder). You can define any template or structure you want in the XSLT file.

public class <xsl:value-of select="@classname"/>

{

// Constructor Defination

public <xsl:value-of select="@classname"/>() { }

// Variables Defination

<xsl:for-each select="fields/field">

private <xsl:value-of select="datatype"/><xsl:text></xsl:text> <xsl:value-of select="name"/>=<xsl:value-of select="initialvalue"/>;

</xsl:for-each>

 

.NET Libraries Used:

There is a little bit of C# code involved which is used to transform the combine the XML and XSLT and produce meaningful results.

if(!Page.IsPostBack)

{

System.Xml.Xsl.XslTransform transform = new XslTransform();

transform.Load(Server.MapPath("Learning.xslt"));

transform.Transform(Server.MapPath("DBUser.xml"),Server.MapPath("User.cs"));

}

Basically, we just load the .xslt file and transform it using the Transform method of the XslTransform class. You will need to add the namespace. Now I did not created User.cs file implicitly and so you will need to add the User.cs file and leave it BLANK.

using System.Xml.Xsl;

So, Doc what is generated:

Now, when you run the page you will be amazed to see that the whole class is generated without even writing a single line of code.

This is the code that is automatically generated.

 

// This code is generated automatically by using the AzamSharpEntityClassTemplate

// Name: Mohammad Azam

// Email: [email protected]

// Website: www.gridviewguy.com

 

using System;

using System.Data;

using System.Collections;

 

namespace Security

{

public class DBUser

{

// Constructor Defination

public DBUser() { }

// Variables Defination

private int _userID=0;

private string _firstName=null;

private string _lastName=null;

// Properties Defination

public int UserID

{

get { return _userID; }

set { _userID = value; }

}

public string FirstName

{

get { return _firstName; }

set { _firstName = value; }

}

public string LastName

{

get { return _lastName; }

set { _lastName = value; }

}

// Methods Defination

public DataSet GetAllUsers ( int userID,string firstName,)

{

return null;

}

public DataSet GetTopUsers ( string firstName,string lastName,)

{

return null;

}

private ArrayList MyNewMethod ( int userID,string firstName,)

{

return null;

}

}

}

 

This is the exact code generated. As you have noticed that the methods parameters have a comma at the end. I am kind of working on this to remove this (I am kind of new to XSLT.... don't tell anyone :) ).  

I hope you liked the article, happy coding!