📄 nhibernate.mapping-1.1.csgen
字号:
##|TYPE Template
##|UNIQUEID 2c38e44a-7d9d-4d88-a5d8-493b6336d572
##|TITLE NHibernate Object Mapping 1.1
##|NAMESPACE NHibernate
##|OUTPUT_LANGUAGE C#
##|COMMENTS_BEGIN
This is an update of k-dub's NHibernate Object Mapping template. Generate C# classes or hbm mapping files or both. Can use tables or views. Can specify "read-only". Can generate overridden Equals and GetHashCode methods. Added support for one-to-many mappings.
##|COMMENTS_END
##|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 = 640;
// 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", "Business.Data", "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 chkDefaultEmptyConstructor = ui.AddCheckBox( "chkDefaultEmptyConstructor", "Create a default (empty) constructor. Required.", true, "Create a default constructor with empty fields." );
chkDefaultEmptyConstructor.Top = chkMapping.Top + 20;
GuiCheckBox chkRequiredFieldsConstructor = ui.AddCheckBox( "chkRequiredFieldsConstructor", "Create a constructor with all required (non-null) fields as parameters.", true, "Create a constructor with all required (non-null) fields as parameters" );
chkRequiredFieldsConstructor.Top = chkDefaultEmptyConstructor.Top + 20;
GuiCheckBox chkIncludePKInReqFieldsCtorArgs = ui.AddCheckBox( "chkIncludePKInReqFieldsCtorArgs", "Include primary key in required fields constructor.", false, "Include primary key in the parameter list for the required fields constructor." );
chkIncludePKInReqFieldsCtorArgs.Top = chkRequiredFieldsConstructor.Top + 20;
GuiCheckBox chkEqualsHashCode = ui.AddCheckBox( "chkEqualsHashCode", "Create Equals and GetHashCode.", false, "Generate Equals and GetHashCode methods." );
chkEqualsHashCode.Top = chkIncludePKInReqFieldsCtorArgs.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;
private bool _generateDefaultCtor;
private bool _generateRequiredFieldsCtor;
private bool _includePKInReqFieldsCtorArgs;
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"];
_generateDefaultCtor = (bool) input["chkDefaultEmptyConstructor"];
_generateRequiredFieldsCtor = (bool) input["chkRequiredFieldsConstructor"];
_includePKInReqFieldsCtorArgs = (bool) input["chkIncludePKInReqFieldsCtorArgs"];
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 )
{
output.writeln( "/*" );
output.writeln( "insert license info here" );
output.writeln( "*/" );
output.writeln( "using System;" );
output.writeln( "using System.Collections;" );
output.writeln( "" );
output.write( "namespace " );
output.writeln( _nameSpace );
output.writeln( "{" );
output.writeln( "\t/// <summary>" );
output.writeln( "\t///\tGenerated by MyGeneration using the NHibernate Object Mapping template" );
output.writeln( "\t/// </summary>" );
output.writeln( "\t[Serializable]" );
output.write( "\tpublic sealed class " );
output.writeln( _className );
output.writeln( "\t{" );
BuildPrivateMembers( Columns );
if( _generateDefaultCtor )
BuildDefaultConstructor( Columns );
if( _generateRequiredFieldsCtor )
BuildRequiredFieldsCtor( Columns );
BuildPublicAccessors( Columns );
// BuildPublicFunctions( Columns );
if( _generateEqualsHashCode )
BuildEqualsHashCodeOverrides( Columns );
output.writeln( "\t}" );
output.writeln( "}" );
_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 ? ColumnFKToClassName( field) : ColumnToNHibernateType( field ) );
if( fieldType.EndsWith( "[]" ) )
{%>
<%= fieldName %> = new <%= fieldType %>{}; <%
}
else
{
switch( fieldType )
{
case "string":%>
<%= fieldName %> = String.Empty; <%
break;
case "DateTime":%>
<%= fieldName %> = DateTime.MinValue; <%
break;
case "bool":%>
<%= fieldName %> = false; <%
break;
case "decimal":
case "float":
case "short":
case "int":
case "long":%>
<%= fieldName %> = 0; <%
break;
default:%>
<%= fieldName %> = new <%= fieldType %>(); <%
break;
}
}
if( field.IsInForeignKey && field.IsInPrimaryKey )
{
//Here, there are foreign key relationships to this column,
//so there are collections in this class that represent those relationships.
//Initialize them.
foreach( IForeignKey fk in field.ForeignKeys )
{
output.writeln("");
output.write("\t\t\t");
output.write( _prefix );
output.write( ToPascalCase( fk.ForeignTable.Alias.Replace( " ", "" ) ) );
output.write( "List = new ArrayList(); " );
}
}
}
output.writeln( "" );
output.writeln( "\t\t}" );
output.writeln( "\t\t#endregion // End of Default ( Empty ) Class Constuctor" );
}
private void BuildRequiredFieldsCtor( IColumns Columns )
{
output.writeln( "" );
output.writeln( "\t\t#region Required Fields Only Constructor" );
output.writeln( "\t\t/// <summary>" );
output.writeln( "\t\t/// required (not null) fields only constructor" );
output.writeln( "\t\t/// </summary>" );
output.write( "\t\tpublic " );
output.write( _className );
output.writeln( "(" );
bool first = true;
foreach( IColumn col in Columns )
{
if( !col.IsNullable && ( !col.IsInPrimaryKey || _includePKInReqFieldsCtorArgs ) )
{
if( !first )
{
output.writeln( ", " );
}
output.write( "\t\t\t" );
if( col.IsInForeignKey && !col.IsInPrimaryKey )
output.write( ColumnFKToClassName( col ) );
else
output.write( ColumnToNHibernateType( col ) );
output.write( " " + ColumnToArgumentName( col ) );
first = false;
}
}
output.writeln( ")" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -