codeSmith实现ExtJS中Grid代码生成

新的项目中。要使用ExtJs,为了简单,写了这么一个简单的CodeSmith模板来根据数据库中的表生成Grid。

代码如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> <%--
Name:
<%= GetClassName(SourceTable) %>
Author:sunfishlu
Description:
--%>
<% @CodeTemplateLanguage = " C# " TargetLanguage = " JScript " Description = " Generatesaverysimplebusinessobject. " ResponseEncoding = " UTF-8 " %>
<% @PropertyName = " NameSpace " Type = " String " Category = " Text " Default = " Sunfishlu. " Description = " Thenamespacetouseforthisclass " %>
<% @PropertyName = " SourceTable " Type = " SchemaExplorer.TableSchema " Category = " Context " Description = " Tablethattheobjectisbasedon. " %>
<% @AssemblyName = " SchemaExplorer " %>
<% @AssemblyName = " System.Data " %>
<% @ImportNamespace = " SchemaExplorer " %>
<% @ImportNamespace = " System.Data " %>

/* *
*@authorsunfish
*/

var storeMain = new Ext.data.Store({
proxy:
new Ext.data.HttpProxy({
url:
' <%=GetUrlName(SourceTable)%> '
}),
reader:
new Ext.data.JsonReader({
root:
' data ' ,
id:
' <%=SourceTable.PrimaryKey.MemberColumns[0].Name%> ' ,
totalProperty:
' totalCount ' ,
fields:
[
<% for ( int i = 0 ;i < SourceTable.Columns.Count; ++ i){ %> <%= GetMemberVariableName(SourceTable.Columns[i]) %><% if (i < SourceTable.Columns.Count - 1 ){ %> , <% } %><% } %>
]
}),
remoteSort:
true
});

Ext.onReady(
function (){
Ext.BLANK_IMAGE_URL
= ' http://images.cnblogs.com/s.gif ' ;
Ext.QuickTips.init();
var grid = new Ext.grid.GridPanel({
store:storeMain,
columns:
[
new Ext.grid.RowNumberer(),

<% for ( int i = 0 ;i < SourceTable.Columns.Count;i ++ ){ %>
{header:
" <%=GetHeaderName(SourceTable.Columns[i])%> " ,align: ' center ' ,sortable: true ,dataIndex: <%= GetMemberVariableName(SourceTable.Columns[i]) %> } <% if (i < SourceTable.Columns.Count - 1 ){ %> , <% } %>
<% } %>
],
renderTo:
' grid ' ,
viewConfig:{forceFit:
true },
trackMouseOver:
true ,
loadMask:
true ,
autoShow:
true ,
autoScroll:
true ,
loadMask:{msg:
" 数据加载中,请稍等 " },
bbar:
new Ext.PagingToolbar({
pageSize:
20 ,
store:storeMain,
displayInfo:
true ,
displayMsg:
' 当前显示{0}-{1}条记录/共{2}条记录 ' ,
emptyMsg:
" 没有数据 "
})
});
storeMain.load({params:{start:
0 ,limit: 20 ,sort: ' <%=SourceTable.PrimaryKey.MemberColumns[0].Name%> ' ,dir: ' desc ' }});
});
< scriptrunat = " template " >
// Myfunctionshere.
publicstringGetClassName(TableSchematable)
{
if (table == null )
{
return null ;
}
return table.Name + " Grid " ;
}
publicstringGetUrlName(TableSchematable)
{
if (table == null )
{
return null ;
}
return table.Name + " Grid " + " .aspx " ;
}

#regionscript
publicstringGetMemberVariableDeclarationStatement(ColumnSchemacolumn)
{
return GetMemberVariableDeclarationStatement( " private " ,column);
}

publicstringGetMemberVariableDeclarationStatement(stringprotectionLevel,ColumnSchemacolumn)
{
stringstatement
= protectionLevel + " " ;
statement
+= GetCSharpVariableType(column) + " " + GetMemberVariableName(column);

stringdefaultValue
= GetMemberVariableDefaultValue(column);
if (defaultValue != "" )
{
statement
+= " = " + defaultValue;
}

statement
+= " ; " ;

return statement;
}

publicstringGetConstructorParameters()
{
stringret
= "" ;
foreach(ColumnSchemacolumn
in SourceTable.Columns)
{
ret
+= GetCSharpVariableType(column) + " " + GetCamelCaseName(GetPropertyName(column)) + " ,/n/t/t/t " ;
}
return ret.Substring( 0 ,ret.Length - 5 );
}

publicstringGetAssignValue()
{
stringret
= "" ;
foreach(ColumnSchemacolumn
in SourceTable.Columns)
{
ret
+= GetMemberVariableName(column) + ( new String( ' ' ,GetPropertyNameMaxLength() - GetPropertyNameLength(column))) + " = " + GetCamelCaseName(GetPropertyName(column)) + " ;/n/t/t/t " ;
}
return ret;
}

publicstringGetReaderAssignmentStatement(ColumnSchemacolumn,
int index)
{
stringstatement
= " if(!reader.IsDBNull( " + index.ToString() + " )) " ;
statement
+= GetMemberVariableName(column) + " = " ;

if (column.Name.EndsWith( " TypeCode " ))statement += " ( " + column.Name + " ) " ;

statement
+= " reader. " + GetReaderMethod(column) + " ( " + index.ToString() + " ); " ;

return statement;
}

publicstringGetCamelCaseName(stringvalue)
{
// returnvalue.Substring(0,1).ToLower()+value.Substring(1);
return value;
}

publicstringGetMemberVariableName(ColumnSchemacolumn)
{
// stringpropertyName=GetPropertyName(column);
stringpropertyName = column.Name;
stringmemberVariableName
= " ' " + GetCamelCaseName(propertyName) + " ' " ;

return memberVariableName;
}
publicstringGetHeaderName(ColumnSchemacolumn)
{
// stringpropertyName=GetPropertyName(column);
stringpropertyName = column.Name;
stringmemberVariableName
= GetCamelCaseName(propertyName);

return memberVariableName;
}

publicstringGetPropertyName(ColumnSchemacolumn)
{
stringpropertyName
= column.Name;

if (propertyName == column.Table.Name + " Name " ) return " Name " ;
if (propertyName == column.Table.Name + " Description " ) return " Description " ;

if (propertyName.EndsWith( " TypeCode " ))propertyName = propertyName.Substring( 0 ,propertyName.Length - 4 );

return propertyName;
}

public
int GetPropertyNameLength(ColumnSchemacolumn)
{
return (GetPropertyName(column)).Length;
}
public
int GetPropertyNameMaxLength()
{
int ret = 0 ;
foreach(ColumnSchemacolumn
in SourceTable.Columns)
{
ret
= ret < GetPropertyNameLength(column) ? GetPropertyNameLength(column):ret;
}
return ret;
}

publicstringGetMemberVariableDefaultValue(ColumnSchemacolumn)
{
if (column.IsPrimaryKeyMember)
{
return "" ;
}
switch (column.DataType)
{
case DbType.Guid:
{
return " Guid.Empty " ;
}
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
case DbType.String:
case DbType.StringFixedLength:
{
return " String.Empty " ;
}
default :
{
return "" ;
}
}
}

publicstringGetCSharpVariableType(ColumnSchemacolumn)
{
if (column.Name.EndsWith( " TypeCode " )) return column.Name;

switch (column.DataType)
{
case DbType.AnsiString: return " string " ;
case DbType.AnsiStringFixedLength: return " string " ;
case DbType.Binary: return " byte[] " ;
case DbType.Boolean: return " bool " ;
case DbType.Byte: return " byte " ;
case DbType.Currency: return " decimal " ;
case DbType.Date: return " DateTime " ;
case DbType.DateTime: return " DateTime " ;
case DbType.Decimal: return " decimal " ;
case DbType.Double: return " double " ;
case DbType.Guid: return " Guid " ;
case DbType.Int16: return " short " ;
case DbType.Int32: return " int " ;
case DbType.Int64: return " long " ;
case DbType.Object: return " object " ;
case DbType.SByte: return " sbyte " ;
case DbType.Single: return " float " ;
case DbType.String: return " string " ;
case DbType.StringFixedLength: return " string " ;
case DbType.Time: return " TimeSpan " ;
case DbType.UInt16: return " ushort " ;
case DbType.UInt32: return " uint " ;
case DbType.UInt64: return " ulong " ;
case DbType.VarNumeric: return " decimal " ;
default :
{
return " __UNKNOWN__ " + column.NativeType;
}
}
}

publicstringGetReaderMethod(ColumnSchemacolumn)
{
switch (column.DataType)
{
case DbType.Byte:
{
return " GetByte " ;
}
case DbType.Int16:
{
return " GetInt16 " ;
}
case DbType.Int32:
{
return " GetInt32 " ;
}
case DbType.Int64:
{
return " GetInt64 " ;
}
case DbType.AnsiStringFixedLength:
case DbType.AnsiString:
case DbType.String:
case DbType.StringFixedLength:
{
return " GetString " ;
}
case DbType.Boolean:
{
return " GetBoolean " ;
}
case DbType.Guid:
{
return " GetGuid " ;
}
case DbType.Currency:
case DbType.Decimal:
{
return " GetDecimal " ;
}
case DbType.DateTime:
case DbType.Date:
{
return " GetDateTime " ;
}
case DbType.Binary:
{
return " GetBytes " ;
}
default :
{
return " __SQL__ " + column.DataType;
}
}
}
publicstringGetSqlDbType(ColumnSchemacolumn)
{
switch (column.NativeType)
{
case " bigint " : return " BigInt " ;
case " binary " : return " Binary " ;
case " bit " : return " Bit " ;
case " char " : return " Char " ;
case " datetime " : return " DateTime " ;
case " decimal " : return " Decimal " ;
case " float " : return " Float " ;
case " image " : return " Image " ;
case " int " : return " Int " ;
case " money " : return " Money " ;
case " nchar " : return " NChar " ;
case " ntext " : return " NText " ;
case " numeric " : return " Decimal " ;
case " nvarchar " : return " NVarChar " ;
case " real " : return " Real " ;
case " smalldatetime " : return " SmallDateTime " ;
case " smallint " : return " SmallInt " ;
case " smallmoney " : return " SmallMoney " ;
case " sql_variant " : return " Variant " ;
case " sysname " : return " NChar " ;
case " text " : return " Text " ;
case " timestamp " : return " Timestamp " ;
case " tinyint " : return " TinyInt " ;
case " uniqueidentifier " : return " UniqueIdentifier " ;
case " varbinary " : return " VarBinary " ;
case " varchar " : return " VarChar " ;
default : return " __UNKNOWN__ " + column.NativeType;
}
}

publicstringGetPrimaryKeyType(TableSchematable)
{
if (table.PrimaryKey != null )
{
if (table.PrimaryKey.MemberColumns.Count == 1 )
{
return GetCSharpVariableType(table.PrimaryKey.MemberColumns[ 0 ]);
}
else
{
throw new ApplicationException( " Thistemplatewillnotworkonprimarykeyswithmorethanonemembercolumn. " );
}
}
else
{
throw new ApplicationException( " Thistemplatewillonlyworkontableswithaprimarykey. " );
}
}

publicoverridestringGetFileName()
{
return this .GetClassName( this .SourceTable) + " .cs " ;
}

publicstringGetCamelCaseNameTrue(stringvalue)
{
return value.Substring( 0 , 1 ).ToLower() + value.Substring( 1 );
}
#endregionscript
< / script>

你可能感兴趣的:(ExtJs)