SchemaExplorer now contains the LINQ to easier querying of metadata!
A new feature in CodeSmith 5.1 is the ability to use LINQ with SchemaExplorer. LINQ support makes it easier to filter, organize and sort the metadata from your datasource.
Here is a quick sample that shows the difference LINQ makes in developing a CodeSmith template:
Before
<% for( int i = 0; i < SourceDatabase.Tables.Count; i++ ) { if( SourceDatabase.Tables[i].HasPrimaryKey && SourceDatabase.Tables[i].PrimaryKey.MemberColumns.Count == 1) { %> <%=SourceDatabase.Tables[i].Name %> <% for( int j = 0; j < SourceDatabase.Tables[i].Columns.Count; j++ ) { if( SourceDatabase.Tables[i].Columns[j].DataType == System.Data.DbType.Int32 ) { %> <%=SourceDatabase.Tables[i].Columns[j].Name %> <%}%> <%}%> <%}%> <% } %>
After
<% foreach (TableSchema table in databaseSchema.Tables.Where(t => t.HasPrimaryKey && t.PrimaryKey.MemberColumns.Count == 1)) %> <%=table.Name %> <% foreach (ColumnSchema column in table.Columns.Where(c => c.DataType == System.Data.DbType.Int32)) { %> <%=column.Name %> <%}%> <% } %>
As you can see, LINQ will make your templates easier to read and a lot easier to maintain. This sample is attached so you can try it yourself. Also, to demonstrate LINQ with SchemaExplorer, we have updated the StoreProcedures template that ships with CodeSmith 5.1 to take advantage of the LINQ support. The StoredProcedures template is located in the Database\StoredProcedures samples folder.
With LINQ support in SchemaExplorer, it is much easier and cleaner to get the data you want when generating code using CodeSmith.