📄 nhibernate updated by gustavo.zeus
字号:
##|TYPE Template
##|UNIQUEID 8d380649-7daf-4ea0-8e2a-1f9f432b0e04
##|TITLE nHibernate - k-dub - Updated - incomplete
##|NAMESPACE
##|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" ) ||
( !input.Contains( "chkClass" ) && !input.Contains( "chkNaming" ) ) )
{
ui.Title = "NHibernate Object Mapping";
ui.Width = 600;
ui.Height = 600;
// 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." );
label1.Width = 200;
GuiTextBox outputPath = ui.AddTextBox( "outputPath", sOutputPath, "Select the Output Path." );
outputPath.Width = 450;
GuiFilePicker selectPath = ui.AddFilePicker( "selectPath", "Select Path", "Select the Output Path.", "outputPath", true );
selectPath.Top = outputPath.Top;
selectPath.Width = 100;
selectPath.Left = outputPath.Left + outputPath.Width + 20;
GuiLabel label2 = ui.AddLabel( "label2", "Namespace: ", "Provide your objects namespace." );
label2.Width = 280;
GuiTextBox classNamespace = ui.AddTextBox( "classNamespace", "NHibernate", "Provide your objects namespace." );
classNamespace.Width = 280;
GuiLabel label3 = ui.AddLabel( "label3", "Member variable prefix: ", "Provide your Prefix." );
label3.Width = 280;
label3.Top = label2.Top;
label3.Left = label2.Width + 20;
GuiTextBox memberPrefix = ui.AddTextBox( "memberPrefix", "m_", "" );
memberPrefix.Width = 280;
memberPrefix.Top = classNamespace.Top;
memberPrefix.Left = classNamespace.Width + 20;
// Setup Database selection combobox.
GuiLabel label4 = ui.AddLabel( "label4", "Select a database:", "Select a database in the dropdown below." );
label4.Width = 250;
GuiComboBox chooseDatabase = ui.AddComboBox( "chooseDatabase", "Select a database." );
chooseDatabase.Width = 250;
GuiLabel label5 = ui.AddLabel( "label5", "Output type:", "Select one or both." );
label5.Width = 150;
label5.Top = label4.Top;
label5.Left = label4.Width + 20;
GuiCheckBox chkClass = ui.AddCheckBox( "chkClass", "Create class files.", true, "Create a class file for each table or view selected. (*.cs)" );
chkClass.Width = 150;
chkClass.Top = chooseDatabase.Top;
chkClass.Left = chooseDatabase.Width + 20;
GuiCheckBox chkMapping = ui.AddCheckBox( "chkMapping", "Create XML mapping files.", true, "Create an XML file for each table or view selected. (*.hbm.xml)" );
chkMapping.Width = 150;
chkMapping.Top = chkClass.Top + 20;
chkMapping.Left = chkClass.Left;
GuiLabel label6 = ui.AddLabel( "label6", "Read Only:", "Create as read only?" );
label6.Width = 150;
label6.Top = label5.Top;
label6.Left = label5.Left + label5.Width + 20;
GuiCheckBox chkReadOnly = ui.AddCheckBox( "chkReadOnly", "Create as read-only.", false, "Create object and mapping to have read-only access." );
chkReadOnly.Width = 150;
chkReadOnly.Top = chkClass.Top;
chkReadOnly.Left = chkClass.Left + chkClass.Width + 20;
GuiCheckBox chkEqualsHashCode = ui.AddCheckBox( "chkEqualsHashCode", "Create Equals and GetHashCode.", false, "Generate Equals and GetHashCode methods." );
chkEqualsHashCode.Top = chkMapping.Top + 20;
// Setup Tables selection multi-select listbox.
GuiLabel label7 = ui.AddLabel( "label7", "Select tables:", "Select tables from the listbox below." );
label7.Top = chkEqualsHashCode.Top + 20;
GuiListBox chooseTables = ui.AddListBox( "chooseTables", "Select tables." );
chooseTables.Height = 120;
// Setup Views selection multi-select listbox.
GuiLabel label8 = ui.AddLabel( "label8", "Select views:", "Select views from the listbox below." );
GuiListBox chooseViews = ui.AddListBox( "chooseViews", "Select views." );
chooseViews.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 );
bindViews( Databases.SelectedValue );
}
}
}
catch
{
}
}
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
{
}
}
public void bindViews( string sDatabase )
{
int count = 0;
GuiListBox lstViews = ui["chooseViews"] as GuiListBox;
try
{
IDatabase db = MyMeta.Databases[sDatabase];
lstViews.BindData( db.Views );
}
catch
{
}
}
public void chooseDatabase_onchange( GuiComboBox control )
{
int count = 0;
GuiComboBox cmbDatabases = ui["chooseDatabase"] as GuiComboBox;
bindTables( cmbDatabases.SelectedText );
bindViews( cmbDatabases.SelectedText );
}
}
##|GUI_END
##|BODY_MODE Markup
##|BODY_ENGINE .Net Script
##|BODY_LANGUAGE C#
##|BODY_TAG_START <%
##|BODY_TAG_END %>
##|BODY_BEGIN
<%#NAMESPACE System.IO, System.Text, System.Text.RegularExpressions, System.Globalization %><%
public class GeneratedTemplate : DotNetScriptTemplate
{
private ArrayList _selectedTables;
private ArrayList _selectedViews;
private string _dbName;
private string _tableName;
private string _className;
private string _exportPath;
private string _fileName;
private string _nameSpace;
private string _prefix;
private bool _createClassFiles;
private bool _createXmlFiles;
private bool _createReadOnly;
private bool _generateEqualsHashCode;
public GeneratedTemplate( ZeusContext context ) : base( context ) {}
public override void Render()
{
_dbName = input["chooseDatabase"].ToString();
_selectedTables = input["chooseTables"] as ArrayList;
_selectedViews = input["chooseViews"] as ArrayList;
_exportPath = input["outputPath"].ToString();
_nameSpace = input["classNamespace"].ToString();
_prefix = input["memberPrefix"].ToString();
_createClassFiles = (bool)input["chkClass"];
_createXmlFiles = (bool)input["chkMapping"];
_createReadOnly = (bool)input["chkReadOnly"];
_generateEqualsHashCode = (bool)input["chkEqualsHashCode"];
foreach( string _newTable in _selectedTables )
{
ITable _workingTable = MyMeta.Databases[_dbName].Tables[_newTable];
_tableName = _workingTable.Alias.Replace( " ", "" );
_className = ToPascalCase( _tableName );
if( _createClassFiles )
{
GenerateClassFile( _workingTable.Columns );
}
if( _createXmlFiles )
{
GenerateMappingFile( _workingTable.Columns );
}
}
foreach( string _newView in _selectedViews )
{
IView _workingView = MyMeta.Databases[_dbName].Views[_newView];
_tableName = _workingView.Alias.Replace( " ", "" );
_className = ToPascalCase( _tableName );
if( _createClassFiles )
{
GenerateClassFile( _workingView.Columns );
}
if( _createXmlFiles )
{
GenerateMappingFile( _workingView.Columns );
}
}
}
private void GenerateClassFile( IColumns Columns )
{%>/*
insert license info here
*/
using System;
namespace <%= _nameSpace %>
{
/// <summary>
/// Generated by MyGeneration using the NHibernate Object Mapping template
/// </summary>
[Serializable]
public sealed class <%= _className %>
{
<% BuildPrivateMembers( Columns ); %>
<% BuildDefaultConstructor( Columns ); %>
<% BuildPublicAccessors( Columns ); %>
<% BuildPublicFunctions( Columns ); %>
<% if( _generateEqualsHashCode ) BuildEqualsHashCodeOverrides( Columns ); %>
}
}
<%
_fileName = _className + ".cs";
output.save( Path.Combine( _exportPath, _fileName ), false );
output.clear();
}
private void GenerateMappingFile( IColumns Columns )
{
BuildHBMDefinition( Columns );
_fileName = _className + ".hbm.xml";
output.save( Path.Combine( _exportPath, _fileName ), false );
output.clear();
}
private void BuildDefaultConstructor( IColumns Columns )
{
%>#region Default ( Empty ) Class Constuctor
/// <summary>
/// default constructor
/// </summary>
public <%= _className %>()
{<%
foreach( IColumn field in Columns )
{
string fieldName = ColumnToMemberVariable( field );
string fieldType = ( field.IsInForeignKey && !field.IsInPrimaryKey ? ToPascalCase( field.ForeignKeys[0].PrimaryTable.Alias.Replace( " ", "" ) ) : ColumnToNHibernateType( field ) );
if( fieldType.EndsWith( "[]" ) )
{%>
<%= fieldName %> = new <%= fieldType %>{}; <%
}
else
{
switch( fieldType )
{
case "string":%>
<%= fieldName %> = null; //String.Empty; <%
break;
case "DateTime":%>
<%= fieldName %> = DateTime.MinValue; <%
break;
case "bool":%>
<%= fieldName %> = false; <%
break;
case "decimal":
case "float":
case "byte":
case "short":
case "int":
case "long":%>
<%= fieldName %> = 0; <%
break;
default:%>
<%= fieldName %> = null;// new <%= fieldType %>(); <%
break;
}
}
}%>
}
#endregion // End of Default ( Empty ) Class Constuctor<%
}
private void BuildRequiredConstructor( IColumns Columns )
{
if( CountRequiredFields( Columns ) > 0 && CountNullableFields( Columns ) < Columns.Count )
{
%>#region Required Fields Only Constructor
/// <summary>
/// required (not null) fields only constructor
/// </summary>
public <%= _className %>(<%
bool first = true;
foreach( IColumn col in Columns )
{
if( !col.IsNullable )
{
if( !first ) output.write( ", " );
output.write( ColumnToNHibernateType( col ) + " " + ColumnToArgumentName( col ) );
first = false;
}
}
%>)
{<%
foreach( IColumn col in Columns )
{
if( !col.IsNullable )
{
%>
<%= ColumnToMemberVariable( col ) %> = <%= ColumnToArgumentName( col ) %>; <%
}
else
{
switch( ColumnToNHibernateType( col ) )
{
default:%>
<%= ColumnToMemberVariable( col ) %> = null; <%
break;
case "string":%>
<%= ColumnToMemberVariable( col ) %> = null; //String.Empty; <%
break;
case "DateTime":%>
<%= ColumnToMemberVariable( col ) %> = DateTime.MinValue; <%
break;
case "bool":%>
<%= ColumnToMemberVariable( col ) %> = false; <%
break;
case "decimal":
case "float":
case "byte":
case "short":
case "int":
case "long":%>
<%= ColumnToMemberVariable( col ) %> = 0; <%
break;
}
}
}
%>
}
#endregion // End Required Fields Only Constructor<%
}
}
private void BuildFullConstructor( IColumns Columns )
{
%>#region Full Constructor
/// <summary>
/// full constructor
/// </summary>
public <%= _className %>(<%
bool first = true;
foreach( IColumn field in Columns )
{
if( !first ) output.write( ", " );
output.write( ( field.IsInForeignKey && !field.IsInPrimaryKey ? ToPascalCase( field.ForeignKeys[0].PrimaryTable.Alias.Replace( " ", "" ) ) : ColumnToNHibernateType( field ) ) + " " + ColumnToArgumentName( field ) );
first = false;
}%>)
{<%
foreach( IColumn col in Columns )
{
%>
<%= ColumnToMemberVariable( col ) %> = <%= ColumnToArgumentName( col ) %>; <%
}
%>
}
#endregion // End Full Constructor<%
}
private void BuildEqualsHashCodeOverrides( IColumns Columns )
{
%>
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
<%= _className %> castObj = (<%= _className %>)obj; <%
if( CountUniqueFields( Columns ) == 0 )
{%>
return castObj.GetHashCode() == this.GetHashCode()<%
}
else
{%>
return ( castObj != null )<%
foreach( IColumn c in Columns )
{
if( c.IsInPrimaryKey )
{
%> &&
( this.<%= ColumnToMemberVariable( c ) %> == castObj.<%= ColumnToPropertyName( c ) %> )<%
}
}
} %>;
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
<% if( CountUniqueFields( Columns ) == 0 )
{
%>return this.GetType().FullName.GetHashCode();
<%
}
else
{%>
int hash = 57; <%
foreach( IColumn c in Columns )
{
if( c.IsInPrimaryKey )
{
%>
hash = 27 * hash * <%= ColumnToMemberVariable( c ) %>.GetHashCode();<%
}
}
%>
return hash; <%
}%>
}
#endregion
<%
}
private void BuildPrivateMembers( IColumns Columns )
{
if( Columns.Count > 0 )
{
%>#region Private Members
private bool <%= _prefix %>isChanged;
private bool <%= _prefix %>isDeleted;<%
foreach( IColumn field in Columns )
{
if( field.IsInForeignKey && !field.IsInPrimaryKey )
{%>
private <%= ToPascalCase( field.ForeignKeys[0].PrimaryTable.Alias.Replace( " ", "" ) ) %> <%= ColumnToMemberVariable( field ) %>; <%
}
else
{%>
private <%= ColumnToNHibernateType( field ) %> <%= ColumnToMemberVariable( field ) %>; <%
}
}
%>
#endregion<%
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -