by Sameera
23. May 2007 21:42
In our last episode, I was trying to come up with a CodeSmith template that could generate my Data Tiers. CodeSmith does come with a huge bundle of pre-written templates. But, I wanted to go ahead and write my own firstly because I wanted to explore CodeSmith's capabilities. Secondly, I have my own style of writing entity classes and needless to say, the existing templates didn't fit my mould.
Before I get started, there's one thing I should note: If you're reading a template written by someone else, trust me, you will find it extremely difficult to follow. Other than for the obvious reasons, this is partly due to the fact that its extremely difficult to get the formatting just right on both the generated code as well as the template code. The template writer would of course try to get the generated code as readable as possible, and break the formatting of the template while doing so.
Getting back to the task at hand, you should know that I'm one of those people who just hate working with databases. To me, something about them feel very low tech. So this is in part, an attempt to escape the torture of creating tables, store procs and writing data plumbing code and the like.
Here are my end-goals:
1. To come up with an easy to follow XML configuration format that would drive the code generation. The config file would be the only input an end user would need to code.
2. Generate the SQL required for creating tables, indexes and relationships.
3. Generate stored procedures that perform basic CRUD and retrieval operations
4. Generate the C# entity classes along with the C# methods for data retrieval and manipulation.
The template I have so far created can be found here (11.65 kb). You may also want to check out the XML that would drive this template. I'll get in to more details about each of these components in my future posts.
by Sameera
15. May 2007 21:15
Now, here's my Entity definition. This definition would vary from project to project and would be the only thing I'd have to change.
<nTier projectPrefix="prj">
<entity name="Tag" primaryKeys="id">
<properties>
<property name="id" sqlType="bigint" notnull="true" />
<property name="text" sqlType="nvarchar(50)" notnull="true" />
</properties>
<indices>
<index columns="text" />
</indices>
</entity>
<entity name="Article" primaryKeys="id">
<properties>
<property name="id" sqlType="bigint" notnull="true" />
<property name="title" sqlType="nvarchar(255)" notnull="true" />
<property name="content" sqlType="xml" />
</properties>
<indices>
<index columns="title" />
</indices>
</entity>
<entity name="ArticleTag" primaryKeys="articleId, tagId">
<properties>
<property name="articleId" sqlType="bigint" notnull="true" />
<property name="tagId" sqlType="bigint" notnull="true" />
</properties>
</entity>
</nTier>
Now, here's how it's all structured:
- Each entity tag will correspond to a database table and most of the time an C# class. There're exceptions such as the link-tables. I'll explain that when I get to one.
- The projectPrefix attribute specifies the prefix that would be added to each table and stored procedure generated. For example, the entity Article would generate a table prj_Article
- Each property tag inside properties tag will correspond to a table column and a public property in the generated class.
- The primaryKeys attribute can be used to provide a comma seperated list of primary key columns
- The indices tag is used to specify nonclustered indexes for the table. The columns attribute accepts a comma seperated list of columns