Showing posts with label CodeSmith. Show all posts
Showing posts with label CodeSmith. Show all posts

Friday, 6 February 2009

Using Reflection in CodeSmith Templates to Generate your UI

We are currently evaluating whether to use The Text Template Transformation Toolkit (aka T4 - used by MS internally for MVC and Entity Frameworks and built into VS 2008 - see http://www.olegsych.com/2007/12/text-template-transformation-toolkit/) or CodeSmith for generation of our UI layer. This will save us time when getting started with a new application and remove a lot of the monkey work that needs to be done in the UI layer. This code generation will also be a selling point for the Oakton Application Framework.

Our planned process is to:

1) Create our Data Transfer Objects with the lists and properties we wish to display in the User Interface (ie not generate the UI directly from the database schema).
2) Quickly generate basic pages using Reflection on our Data Transfer Objects (Dtos)

This is a simplified example of using CodeSmith templates and the System.Reflection namespace to spit out html for your page based on the properties of your Dto classes (or any other classes that you wish to bind to your UI).

<%--
Name: GenerateUIFromDtoViaReflection_Draft0-1
Author: David Klein
Description: Using Reflection to Generate UI from Dto Assembly when has correct Mapping Attributes
Note: Make sure that referenced assemblies are in the GAC or in the CodeSmith /bin directory so they can be
resolved correctly.
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="True"
Description="Template description here." %>
<%@ Property Name="AssemblyPath"
Type="System.String" Default="c:\temp\MyDtoAssembly.dll" Optional="false"
Category="Strings" Description="The path to your dto."
Editor="System.Windows.Forms.Design.FileNameEditor" %>
<%@ Property Name="TypeName"
Type="System.String" Optional="false" Category="Strings"
Description="Fully qualified name for your Dto" %>
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="Oakton.Sample.Common.Dto" %>
<%@ Assembly Name="Oakton.Framework.Common" %>
<%@ Assembly Name="Oakton.Sample.Common" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="Oakton.Sample.Common.Dto" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Oakton.Framework.Common" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Windows.Forms" %>

<%
//Debugger.Break();
MessageBox.Show(TypeName);
Assembly assembly = System.Reflection.Assembly.LoadFile(AssemblyPath);
Type objectType = assembly.GetType(TypeName);

//System.Type objectType = System.Type.GetType(TypeName);
//System.Windows.Forms.MessageBox.Show("Is null {0}", (objectType == null).ToString());

PropertyInfo[] properties = objectType.GetProperties();

foreach(PropertyInfo property in properties)
{

//Add Property of Dto to UI
string fieldName = property.Name;
%>
<div class="row">
<label>
<asp:Literal ID="<% =fieldName %>Literal" runat="server" Text='<%%$ Resources:CommonResources, CustomerDetail_<% =fieldName %> %%>' />
</label>
<span class="input">
<asp:TextBox runat="server" ID="<%=fieldName%>TextBox" Text='<%%# Bind("<% =fieldName %>")%%>'
SkinID="short" />
</span>
</div>
<%


}
%>



If you are interested in some of the Patterns (e.g. Dtos and how they should be populated by Transfer Object Assemblers), mentioned above, see (Gasp! It's not from Microsoft!) the Java J2EE patterns site at http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObjectAssembler.html.
The same principles apply regardless of your selected language.



Tuesday, 5 February 2008

Rehash: Dave's simple SQL Workbench/Query Analyzer Code Generator

This is a perenially handy tool for generating database-oriented code using the most basic of tools: SQL 2005 Workbench. It makes use of system tables or the information_schema and is a very basic (but very useful) tool for code generation - especially when you have hundreds of tables.

Here is one I just made today to avoid having to create a CodeSmith template (which we don't have licences for) or doing the monkey work of manually typing in these attributes. Just set the output mode in isqlw/SQL Workbench to text and generate away! (minus the chimps..)


SELECT
'[EntityPropertyMapping("' + column_name + '")]' + CHAR(10) +
'public ' + CASE data_type
WHEN 'nvarchar' THEN 'string'
WHEN 'int' THEN 'int'
WHEN 'datetime' THEN 'DateTime'
END
+ ' ' + column_name + ' { get; set; }' + CHAR(10) + CHAR(10)
FROM information_schema.columns
WHERE table_name = 'Asset'

Output is:


[EntityPropertyMapping("AssetName")]
public string AssetName { get; set; }

[EntityPropertyMapping("Description")]
public string Description { get; set; }


[EntityPropertyMapping("Comments")]
public string Comments { get; set; }

Monday, 15 October 2007

Debugging CodeSmith Templates in Windows Vista

I had some problems with Windows Vista trying to debug the PLINQO (Professional Linq) CodeSmith templates. To get around the problems of crashing and restarting ( As described by Paul Welter,
http://community.codesmithtools.com/blogs/pwelter/archive/2007/06/09/tips-amp-tricks-debugging.aspx)

"There are some problems with using the Just-In-Time debugger in Windows Vista. First you need to make sure you have all the latest service packs installed. Next, the debugger in Vista will cause CodeSmith to hang when you finish debugging. You can work around this issue by updating the Just-In-Time debugger setting DbgJITDebugLaunchSetting. The setting is found in the registry at [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework]. Change the value of DbgJITDebugLaunchSetting to 2. This will cause the debugger dialog to be displayed immediately when your code hits a breakpoint. This will also allow control to return to CodeSmith when you continue the execution of the template from the debugger."

Note that the original value on my Vista machine was 10 Hex (16).