The following guide will walk you through the basics on how-to extend the CodeSmith Generator PLINQO Templates to fit your custom development needs. In this example we will create a new Webpage for each entity that we generate. This page will contain a LinqDataSource control and an ASP.NET GridView control that will display data from a corresponding entity that we generated. I always recommend that you use the PLINQO API when writing templates that interact with code generated by PLINQO as this ensures that the code generated has the proper names and associations and or types.
Find relevant templates to use as a starting point
The first step I always use is to determine if an existing template has a close fit to your end goal so you save time writing the template. In this example I choose to pick the Enity.Editable.cst template that is located in the CSharp or VisualBasic\Internal Folder. This template is a very basic template that shows how to query off of the underlying PLINQO Template API. After I had determined that this was the template file I wanted to use, I created a new folder next to the internal folder called CustomTemplates. This name really doesn't matter but I choose this name to get started. Next, I copied the ASP.NET sample templates out of the the ASP.NET samples folder and renamed it to Entity.GridView respectively.
Once the files were renamed, I copied the import statements and properties that were defined in the Entity.Editable.cst template and copied them to my Entity.GridView templates. Since the paths were relative and used the same hierarchy. I was able to get away with not updating the file paths. I removed the extra properties and declarations that I knew I would not be needing. I was left with the following in the three Entity.GridView templates:
<%@ Assembly Name="Dbml" Path="..\..\Common" %> <%@ Import Namespace="LinqToSqlShared.DbmlObjectModel" %> <%@ Property Category="1.Mapping" Name="Database" Type="LinqToSqlShared.DbmlObjectModel.Database" Optional="False" Description="Database instance. Must be set by parent template" %> <%@ Property Category="1.Mapping" Name="Type" Type="LinqToSqlShared.DbmlObjectModel.Type" Optional="False" Description="The Type instance for this entity. Must be set by parent template" %> <%@ Map Name="CSharpAlias" Src="System-CSharpAlias.csmap" Reverse="False" Description="Convert system data types to c# alias" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryId" DataSourceID="LinqDataSource1"> <Columns> <asp:BoundField DataField="CategoryId" HeaderText="CategoryId" SortExpression="CategoryId" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Descn" HeaderText="Descn" SortExpression="Descn" />Columns>
asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="PetShop.Web" EnableDelete="True" EnableInsert="True" EnableUpdate="True" EntityTypeName="" TableName="Category"> asp:LinqDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryId" DataSourceID="LinqDataSource1"> <Columns> <% foreach(Column column in Type.Columns) {%> <asp:BoundField DataField="<%= column.Member %>" HeaderText="<%= column.Member %>" <% if(column.IsReadOnly.HasValue && column.IsReadOnly.Value) { %> %>ReadOnly="True" <%}%> SortExpression="<%= column.Member %>" /> <% } %> Columns>
<% string keyNames = string.Empty; foreach (var column in Type.PrimaryKeyColumns.AsIndexedEnumerable()) { keyNames += string.Format("{0}{1}", column.Value.Member, !column.IsLast ? "," : string.Empty); } %><asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="<%= keyNames %>" DataSourceID="LinqDataSource1">
<% var association = Type.GetForeignKeyAssociation(column); if (association == null) { %> <asp:BoundField DataField="<%= column.Member %>" HeaderText="<%= column.Member %>" <% if(column.IsReadOnly.HasValue && column.IsReadOnly.Value) { %> %>ReadOnly="True" <%}%> SortExpression="<%= column.Member %>" /> <%} else {%> <% } }%>
<%@ Register Name="EntityGridViewSourceViewTemplate" Template="CustomTemplates\Entity.GridView.aspx.cst" %> <%@ Register Name="EntityGridViewCodeBehindTemplate" Template="CustomTemplates\Entity.GridView.aspx.cs.cst" %><%@ Register Name="EntityGridViewDesignerTemplate" Template="CustomTemplates\Entity.GridView.aspx.designer.cs.cst" %>