I spent my sunday learning about Code Generation using XSLT. Lot of times we write repetitive code. This mainly includes creating entity classes. With code generation you can develop a template which writes code for you. In other way Code Generation means code that writes code .
I wrote a very simple template which is used to make entity classes. Its not perfect design but you can get idea how it works.
The xml file acts as the container where you tell the code generator what fields, methods ,properties any anything you want in your entity class.
You can view the xml file (download) . When generating the xml file you should always check that if its well formed and abides by the rules presented in the xml schema. Although in this example I did not checked that.
Next comes the XSLT file (download).
And now finally you need some good old C# code that will use .NET libraries to transform the xml using the XSLT file and produce the .cs file.
C# Code:
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"));
}
That's pretty much it. Now add a file named "User.cs" and don't write anything in that file just leave it blank. Although a good practice will be that you don't add that file and it should be generated automatically but I will cover that later.
Now when you run it this is the code generated automatically:
Code Generated:
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;
}
}
}
Ohh yeah the methods will be created correctly but they will have a comma at the end something like this:
public DataSet GetAllUsers ( int userID,string firstName,)// How to remove it let me know!
{
return null;
}
I really loved this stuff and will definately look more into it.
Okay as I promised I looked into it and finally I solved the problem of the extra commas. I owe all of it to Mr Abraham from asp.net forums.
Corrected Code:
<xsl:for-each select="methods/method">
<!-- The following line should be in a single line and should not span multiple lines -->
<xsl:value-of select="scope"/> <xsl:value-of select="return"/> <xsl:value-of select="name"/> (<xsl:for-each select="parameters/parameter"><xsl:if test="(position() > 0 ) and (position() != last())"> <xsl:value-of select="datatype"/> <xsl:value-of select="name"/>,</xsl:if><xsl:if test="position() = last()"> <xsl:value-of select="datatype"/> <xsl:value-of select="name"/></xsl:if> </xsl:for-each>)
{
return null;
}
</xsl:for-each>
Enjoy!