📄 nhibernate[1].class.csgen
字号:
##|TYPE Template
##|UNIQUEID ebf85179-4b8b-4e9c-967c-a8cd54c6b802
##|TITLE NHibernate Class Generator
##|NAMESPACE NHibernate
##|OUTPUT_LANGUAGE C#
##|GUI_ENGINE .Net Script
##|GUI_LANGUAGE C#
##|GUI_BEGIN
<%#REFERENCE System.Windows.Forms.dll %>
<%#NAMESPACE System, System.Text, System.Collections, Zeus, Zeus.UserInterface, Zeus.DotNetScript %>
public class GeneratedGui : DotNetScriptGui
{
public GeneratedGui(ZeusGuiContext context) : base(context) {}
public override void Setup()
{
if ( !input.Contains("chooseTables") || !input.Contains("txtPath") )
{
ui.Title = "NHibernate Data Class";
ui.Width = 300;
ui.Height = 470;
// Grab default output path
string sOutputPath = "";
if (input.Contains("defaultOutputPath"))
{
sOutputPath = input["defaultOutputPath"].ToString();
}
// Setup Folder selection input control.
GuiLabel label1 = ui.AddLabel("label1", "Select the output path:", "Select the output path in the field below.");
GuiTextBox outputPath = ui.AddTextBox("outputPath", sOutputPath, "Select the Output Path.");
GuiFilePicker selectPath = ui.AddFilePicker("selectPath", "Select Path", "Select the Output Path.", "txtPath", true);
ui.AddLabel ("label2", "Namespace: ", "Provide your objects namespace.");
GuiTextBox classNamespace = ui.AddTextBox("classNamespace", "Business.Data", "Provide your objects namespace.");
ui.AddLabel ("label3", "Member variable prefix: ", "Provide your Prefix.");
GuiTextBox memberPrefix = ui.AddTextBox("memberPrefix", "m_", "");
// Setup Database selection combobox.
GuiLabel label4 = ui.AddLabel("label4", "Select a database:", "Select a database in the dropdown below.");
GuiComboBox chooseDatabase = ui.AddComboBox("chooseDatabase", "Select a database.");
// Setup Tables selection multi-select listbox.
GuiLabel label5 = ui.AddLabel("label5", "Select tables:", "Select tables from the listbox below.");
GuiListBox chooseTables = ui.AddListBox("chooseTables", "Select tables.");
chooseTables.Height = 120;
// Attach the onchange event to the cmbDatabases control.
setupDatabaseDropdown(chooseDatabase);
chooseDatabase.AttachEvent("onchange", "chooseDatabase_onchange");
ui.ShowGui = true;
}
else
{
ui.ShowGui = false;
}
}
public void setupDatabaseDropdown(GuiComboBox Databases)
{
try
{
if (MyMeta.IsConnected)
{
Databases.BindData(MyMeta.Databases);
if (MyMeta.DefaultDatabase != null)
{
Databases.SelectedValue = MyMeta.DefaultDatabase.Alias;
bindTables(Databases.SelectedValue);
}
}
}
catch (Exception ex)
{
}
}
public void bindTables(string sDatabase)
{
int count = 0;
GuiListBox lstTables = ui["chooseTables"] as GuiListBox;
try
{
IDatabase db = MyMeta.Databases[sDatabase];
lstTables.BindData(db.Tables);
}
catch (Exception ex)
{
}
}
public void chooseDatabase_onchange(GuiComboBox control)
{
int count = 0;
GuiComboBox cmbDatabases = ui["chooseDatabase"] as GuiComboBox;
bindTables(cmbDatabases.SelectedText);
}
}
##|GUI_END
##|BODY_MODE Markup
##|BODY_ENGINE .Net Script
##|BODY_LANGUAGE C#
##|BODY_TAG_START <%
##|BODY_TAG_END %>
##|BODY_BEGIN
<%
public class GeneratedTemplate : DotNetScriptTemplate
{
private string _outputBuffer;
private ArrayList _selectedTables;
private string _dbName;
private string _tableName;
private string _exportPath;
private string _fileName;
private string _nameSpace;
private string _prefix;
public GeneratedTemplate(ZeusContext context) : base(context) {}
public override void Render()
{
_dbName = input["chooseDatabase"].ToString();
_selectedTables = input["chooseTables"] as ArrayList;
_exportPath = input["outputPath"].ToString();
_nameSpace = input["classNamespace"].ToString();
_prefix = input["memberPrefix"].ToString();
foreach(string _newTable in _selectedTables)
{
ITable _workingTable = MyMeta.Databases[_dbName].Tables[_newTable];
_tableName = _workingTable.Alias.Replace(" ", "");
_fileName = "NHibernate." + _tableName + ".cs";
printLicense();%>
using System;
using System.Collections;
namespace <%= _nameSpace %>
{
#region Class <%= _tableName %>
/// <summary>
/// <%= _workingTable.Description %>
/// </summary>
[Serializable]
public sealed class <%= _tableName %>
{
<% buildPrivateMembers(_workingTable); %>
<% buildDefaultConstructor(_workingTable); %>
<% buildPublicAccessors(_workingTable); %>
<% buildInternalAccessors(_workingTable); %>
<% buildPublicFunctions(_workingTable); %>
<% buildHBMDefination(_workingTable); %>
}
#endregion // Class <%= _tableName %>
}
<%
_outputBuffer += output.text;
output.save(System.IO.Path.Combine(_exportPath, _fileName), false);
output.clear();
}
output.text = _outputBuffer;
}
private void buildDefaultConstructor(ITable Table)
{
%>#region Default ( Empty ) Class Constuctor
public <%= _tableName %>()
{
<%= _prefix %>isChanged = false;
<%= _prefix %>isDeleted = false;<%
foreach(IColumn field in Table.Columns)
{
string fieldName = _prefix + field.Alias.Replace(" ", "").ToLower();
switch(field.LanguageType)
{
default:%>
<%= fieldName %> = null; <%
break;
case "string":%>
<%= fieldName %> = String.Empty; <%
break;
case "DateTime":%>
<%= fieldName %> = DateTime.MinValue; <%
break;
case "bool":%>
<%= fieldName %> = false; <%
break;
case "decimal":
case "short":
case "int":
case "long":%>
<%= fieldName %> = 0; <%
break;
}
}%>
}
#endregion // End of Default ( Empty ) Class Constuctor<%
}
private void buildPrivateMembers(ITable Table)
{
if(Table.Columns.Count > 0)
{
%>#region Private Members
private bool <%= _prefix %>isChanged;
private bool <%= _prefix %>isDeleted;<%
foreach(IColumn field in Table.Columns)
{%>
private <%= field.LanguageType %> <%= _prefix %><%= field.Alias.Replace(" ", "").ToLower() %>;<%
}
%>
#endregion // End of Private Members<%
}
}
private void buildInternalAccessors(ITable Table)
{
if(Table.Columns.Count > 0)
{
%>#region Internal Accessors for NHibernate<%
foreach(IColumn field in Table.Columns)
{
string fieldAccessor = _prefix + field.Name.Replace(" ", "");
string fieldName = _prefix + field.Alias.Replace(" ", "").ToLower();
%>
#region <%= fieldAccessor %>
/// <summary>
/// <%= field.Description %>
/// </summary>
internal <%= field.LanguageType %> <%= fieldAccessor %>
{
get { return <%= fieldName %>; }
set { <%= fieldName %> = value; }
}
#endregion <%
}
%>
#endregion // Internal Accessors for NHibernate <%
}
}
private void buildPublicAccessors(ITable Table)
{
if(Table.Columns.Count > 0)
{
%>#region Public Accessors <%
foreach(IColumn field in Table.Columns)
{
string fieldAccessor = field.Alias.Replace(" ", "");
string fieldName = _prefix + field.Alias.Replace(" ", "").ToLower();
%>
#region <%= fieldAccessor %>
/// <summary>
/// <%= field.Description %>
/// </summary>
public <%= field.LanguageType %> <%= fieldAccessor %>
{
get { return <%= fieldName %>; }<%
if(!((field.IsInPrimaryKey && field.IsAutoKey) || field.IsComputed))
{
switch(field.LanguageType)
{
default:%>
set { <%= _prefix %>isChanged |= (<%= fieldName %> != value); <%= fieldName %> = value; }<%
break;
case "byte": %>
set
{
if( value.Length > <%= field.CharacterMaxLength.ToString() %>)
throw new ArgumentOutOfRangeException("Invalid value for <%= fieldAccessor %>", value, value.ToString());
<%= _prefix %>isChanged |= (<%= fieldName %> != value); <%= fieldName %> = value;
}
<%
break;
case "string": %>
set
{
if( value.Length > <%= field.CharacterMaxLength.ToString() %>)
throw new ArgumentOutOfRangeException("Invalid value for <%= fieldAccessor %>", value, value.ToString());
<%= _prefix %>isChanged |= (<%= fieldName %> != value); <%= fieldName %> = value;
}<%
break;
}
}%>
}
#endregion // End of <%= fieldAccessor %>
<%
}
%>
#region IsChanged
/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsChanged
{
get { return <%= _prefix %>isChanged; }
}
#endregion // End of HasChanged
#region IsDeleted
/// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsDeleted
{
get { return <%= _prefix %>isDeleted; }
}
#endregion // End of IsDeleted
#endregion // End of Public Accessors <%
}
}
private void buildPublicFunctions(ITable Table)
{%>
#region Public Functions
#region MarkAsDeleted
public void MarkAsDeleted()
{
<%= _prefix %>isDeleted = true;
<%= _prefix %>isChanged = true;
}
#endregion
#endregion<%
}
private void buildHBMDefination(ITable Table)
{
string ifNull = String.Empty;
if(Table.Columns.Count > 0)
{
%>#region NHibernate HBM Defination
/*
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="<%= _nameSpace %>.<%= _tableName %>, <%= _nameSpace %>" table="<%= _tableName %>"> <%
foreach(IColumn field in Table.Columns)
{
if(field.IsInPrimaryKey && field.IsAutoKey)
{%>
<id name="<%= _prefix + field.Name.Replace(" ", "") %>" column="<%= field.Name %>" type="<%= convertNHibernate(field.LanguageType) %>" unsaved-value="0">
<generator class="assigned" />
</id><%
}
else
{
if(!field.IsNullable)
{
ifNull = " not-null=\"true\"";
}
else
{
ifNull = String.Empty;
}
%>
<property name="<%= _prefix + field.Name.Replace(" ", "") %>" column="<%= field.Name %>" type="<%= convertNHibernate(field.LanguageType) %>"<%= ifNull %> /><%
}
}
%>
</class>
</hibernate-mapping>
*/
#endregion // NHibernate HBM Defination<%
}
}
private string convertNHibernate(string Type)
{
string retVal = Type;
switch(Type)
{
case "string":
retVal = "String";
break;
case "long":
retVal = "Int64";
break;
case "short":
retVal = "Int16";
break;
case "int":
retVal = "Int32";
break;
case "decimal":
retVal = "Decimal";
break;
}
return retVal;
}
private void printLicense()
{%>/*
Insert License speil here!!!!
*/<%
}
}
%>
##|BODY_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -