Generate your way to the solution via CodeSmith.
I really don’t want to get into this philosophical argument, whether code generation is good or not, but just think of your Code Generator as a silent programmer which REALLY loves to do your dirty repetitive work. Please read the following with an open mind to the subject, you’ve got nothing to lose.
My time at the army was done and I was back in the free market.
2 days after I was employed for SQLink company, I talked with Amir Engel about the idea of code generation. He told me that he managed to write some templates with CodeSmith and he showed them to me. It looked great but I was hoping to use my CG for that purpose, still, I wanted to protect my “baby”. But this desire was gone when I read about CodeSmith’s SchemaExplorer. My god, I was shocked about how easy it is to write a template which will connect to my database with zero (0 !) effort. In addition, it has its own Studio which allows me to write,test and debug my templates with ease.
This is where the “Base” principle kicks in and help us to protect our custom changes. My correct “DAL” object class structure will be:
// File: UsersDALBase.cs
public class UsersDALBase
{
// our “generated” code here – all the CRUD operations for example.
}
// in another file!
// File: UsersDAL.cs
public class UsersDAL : UsersDALBase
{
// My custom behivors here.
}
Every time I’ll regenerate the code, I’ll overwrite the “Base” file and no harm done – my custom changes are safe !
In my “upper” layers, I always use UsersDAL(or [table]DAL for that manner) and not UsersDALBase.
Now that we’ve got the background, it’s time to introduce to you my amigo –
CodeSmith – harness its power for your own need
- SchemaExplorer – Stop doing all the reverse engineering for Oracle\SqlServer (via “system tables”), it’s already written for you. The only thing you need is to specify is the connection string and… thats it ! You want to see how easy it is? OK OK, relax:
<%@ CodeTemplate Language=“C#” TargetLanguage=“C#”
Src=“../OeCodeTemplate.cs” Inherits=“OrenEllenbogen.Templates.OeCodeTemplate”
Debug=“False” Description=“Generate an entity class for a given table.” %>
<%@ Property Name=“SourceTable” Type=“SchemaExplorer.TableSchema”
Category=“Connection” Description=“Table Object should be based on.” %>
<%@ Assembly Name=“SchemaExplorer” %>
<%@ Import Namespace=“SchemaExplorer” %>
<%
// Collection of all columns in the table.
ColumnSchemaCollection Columns = new ColumnSchemaCollection(SourceTable.Columns);
%>
<%
// Variables by table columns.
for (int i=0; i < Columns.Count; i++) { %>
/// <summary>
/// <%=Columns[i].Name%><%=(Columns[i].Description.Length>0) ? ” : ” + Columns[i].Description : “”%>
/// </summary>
protected <%= GetCSType(Columns[i]) %> <%=VariableStyle(Columns[i].Name)%> = <%= GetCSDefaultByType(Columns[i]) %>;
<%
} //end for
%>
- you can use “code behind” file (*.cs) which holds the common methods for all your templates files (mine is OeCodeTemplate.cs)
- I use the methods GetCSType, VariableStyle and GetCSDefaultByType methods which are in my “code behind” file, but this are simple methods which you can copy\write by yourself.
how to get all the indexes\columns\primary keys\(etc) from a given table.
- Templates GUI – You’ve seen that I’m using SourceTable property on the previous section, the great thing about it is when I’ll open this template with CodeSmith explorer I’ll be able to pick the table directly from my DataSource. So easy and comfortable.
- Huge community – you can find a lot of examples in CodeSmith forums. Don’t try to write something which was already written before you; You can always find a template and get some of its code for your specific need.
- Its easy – believe me, my 4 years old cousin can write a template with CodeSmith, its that easy !
- Its(was) free ! – version 2.6 is free for use, but the new one (3.0) costs (nothing you can’t afford though).
- Using Lut’z reflector with CodeSmith 2.6 – The main problem with this version (and former) is the lack of IntelliSense. This is a problem due to the simple fact that you can’t remember every method\member in SchemaExplorer object for example which makes it harder for you to develop. The simple solution is to use the reflector over the SchemaExplorer.dll, which “sits” in CodeSmith directory, and explore your way to the required member\method.
- Debugging – Debugging with CodeSmith 2.6 (and former) isn’t so comfortable, but it doable – that’s enough for me. In order to do it, all you need to do is to put Debug=”True” in the page header directive and call Debugger.Break(); before the lines you want to debug.
NOTE: in the new version (3.0), Eric .J. Smith, the creator of this great tool, spoiled us with brand new features like built-in IntelliSense and easier debugging options and much more.
Summarize:
Code generation, in my humble opinion, is MONEY SAVER, simple as that, you can cut down the development time by half !
And finally, I’m not afraid to change the database because I know what I need to change and now – it’s even EASY to do.