⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nhibernate.mapping-1.1.csgen

📁 mygeneration 自动生成影射文件
💻 CSGEN
📖 第 1 页 / 共 3 页
字号:
		output.writeln( "\t\t{" );

		foreach( IColumn col in Columns )
		{
			output.write( "\t\t\t" );
			
			if( !col.IsNullable && ( !col.IsInPrimaryKey || _includePKInReqFieldsCtorArgs ) )
			{
				output.write( ColumnToMemberVariable( col ) );
				output.write( " = " );
				output.write( ColumnToArgumentName( col ) );				
				output.writeln( ";" );	
			}
			else
			{
				string fieldType = ( col.IsInForeignKey && !col.IsInPrimaryKey 
					? ColumnFKToClassName( col ) 
					: ColumnToNHibernateType( col ) );
			
				switch( fieldType )
				{
					default:
						output.write( ColumnToMemberVariable( col ) );
						output.writeln( " = null;" );
						break;						
					case "string":
						output.write( ColumnToMemberVariable( col ) );
						output.writeln( " = String.Empty;" );
						break;
					case "DateTime":
						output.write( ColumnToMemberVariable( col ) ); 
						output.writeln( " = DateTime.MinValue;" );
						break;
					case "bool":
						output.write( ColumnToMemberVariable( col ) );
						output.writeln( " = false;" );
						break;
					case "decimal":
					case "float":
					case "short":
					case "int":
					case "long":
						output.write( ColumnToMemberVariable( col ) );
						output.writeln( " = 0;" );
						break;
				}
			}
		}
		output.writeln( "\t\t}" );
		output.writeln( "\t\t#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 )
			{
				if ( !field.IsInPrimaryKey )
				{
					// A column that's in a fk but not in the pk is an actual foreign key, 
					// a many-to-one relationship.  So the member variable is a class instance.
				%>
		private <%= ToPascalCase( field.ForeignKeys[0].PrimaryTable.Alias.Replace( " ", "" ) ) %> <%= ColumnToMemberVariable( field ) %>; <%
				}
				else
				{
					//A column that's in a fk and in the pk represents a fk relationship
					//from another table, a one-to-many relationship.  (This might be 
					//a bad assumption for a table with a composite primary key.)
					//So we have to add the pk member variable itself...
					%>
		private <%= ColumnToNHibernateType( field ) %> <%= ColumnToMemberVariable( field ) %>; <%
					
					//... and then we have to add collections for the foreign tables. 
					foreach( IForeignKey fk in field.ForeignKeys )
					{
						output.writeln("");
						output.write( "\t\tprivate IList " );
						output.write( _prefix );
						output.write( ToPascalCase( fk.ForeignTable.Alias.Replace( " ", "" ) ) );
						output.write( "List; " );						
					}
				}
			}
			else
			{%>
		private <%= ColumnToNHibernateType( field ) %> <%= ColumnToMemberVariable( field ) %>; <%
			}
		}
%>		
		#endregion
<%
		}
	}
	
	private void BuildInternalAccessors( IColumns Columns )
	{
		if( Columns.Count > 0 )
		{
		%>#region Internal Accessors for NHibernate
		<%
			foreach( IColumn field in Columns )
			{
				string fieldAccessor = ColumnToNHibernateProperty( field );
				string fieldName = ColumnToMemberVariable( field );
			%>
		#region <%= fieldAccessor %>
		/// <summary>
		/// <%= field.Description %>
		/// </summary>
		internal <%= ColumnToNHibernateType( field ) %> <%= fieldAccessor %>
		{
			get { return <%= fieldName %>; }
			set { <%= fieldName %> = value; }
		}
		#endregion
		<%
			}
%>
		#endregion // Internal Accessors for NHibernate <%
		}
	}
	
	private void BuildPublicAccessors( IColumns Columns )
	{
		if( Columns.Count > 0 )
		{
			%>
		#region Public Properties
			<%
			
			foreach( IColumn field in Columns )
			{
				string fieldAccessor = ColumnToPropertyName( field );
				string fieldName = ColumnToMemberVariable( field );
				string fieldType = ( field.IsInForeignKey && !field.IsInPrimaryKey ? ToPascalCase( field.ForeignKeys[0].PrimaryTable.Alias.Replace( " ", "" ) ) : ColumnToNHibernateType( field ) );
			%>
		/// <summary>
		/// <%= field.Description %>
		/// </summary>		
		public <%= fieldType %> <%= fieldAccessor %>
		{
			get { return <%= fieldName %>; }
<%
				if( !_createReadOnly )
				{
					//if(!((field.IsInPrimaryKey && field.IsAutoKey) || field.IsComputed))
					//{
						switch( fieldType )
						{
							default:
								output.writeln( "\t\t\tset" );
								output.writeln( "\t\t\t{" );
								
								if( !field.IsNullable && !IsValueType( fieldType ) )
								{
									output.writeln( "\t\t\t\tif( value == null )" );
									output.writeln( "\t\t\t\t\tthrow new ArgumentOutOfRangeException(\"Null value not allowed for " + fieldAccessor + "\", value, \"null\");" );
									output.writeln( "" );
								}
								output.write( "\t\t\t\t" );
								output.write( _prefix );
								output.write( "isChanged |= ( " );
								output.write( fieldName );
								output.writeln( " != value ); " );
								
								output.write( "\t\t\t\t" );
								output.write( fieldName );
								output.writeln( " = value;" );

								output.writeln( "\t\t\t}" );
								break;
							case "byte": %>
			set	
			{	<%if( !field.IsNullable ) 
				  {%>
				if( value == null )
					throw new ArgumentOutOfRangeException("Null value not allowed for <%= fieldAccessor %>", value, "null");
				<%}%>
				if( <%if( field.IsNullable ) {%> value != null && <%}%> 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( !field.IsNullable )
				{%>
				if( value == null )
					throw new ArgumentOutOfRangeException("Null value not allowed for <%= fieldAccessor %>", value, "null");
				<%}%>
				if( <%if( field.IsNullable ) {%> value != null && <%}%> value.Length > <%= field.CharacterMaxLength.ToString() %>)
					throw new ArgumentOutOfRangeException("Invalid value for <%= fieldAccessor %>", value, value.ToString());
				
				<%= _prefix %>isChanged |= (<%= fieldName %> != value); <%= fieldName %> = value;
			}<%						
								break;
					}
				//}
			}%>
		}
			<%
				if( field.IsInForeignKey && field.IsInPrimaryKey )
				{
					//This means that there are collections implementing 
					//one-to-many relationships based on this column.  So 
					//we need accessors for those collections, too. 
					foreach( IForeignKey fk in field.ForeignKeys )
					{
						output.writeln("");
						output.write( "\t\tpublic IList " );
						output.write( ToPascalCase( fk.ForeignTable.Alias.Replace( " ", "" ) ) );
						output.writeln( "List" );	
						output.writeln( "\t\t{" );
						output.writeln( "\t\t\tget" );
						output.writeln( "\t\t\t{" );
						output.write( "\t\t\t\treturn " );
						output.write( _prefix );
						output.write( ToPascalCase( fk.ForeignTable.Alias.Replace( " ", "" ) ) );
						output.writeln( "List;" );
						output.writeln( "\t\t\t}" );
						output.writeln( "\t\t\tset" );
						output.writeln( "\t\t\t{" );
						output.write( "\t\t\t\t" );
						output.write( _prefix );
						output.write( ToPascalCase( fk.ForeignTable.Alias.Replace( " ", "" ) ) );
						output.writeln( "List = value;" );
						output.writeln( "\t\t\t}" );
						output.writeln( "\t\t}" );
					}
				}
			}
			
			%>
		/// <summary>
		/// Returns whether or not the object has changed it's values.
		/// </summary>
		public bool IsChanged
		{
			get { return <%= _prefix %>isChanged; }
		}
		
		/// <summary>
		/// Returns whether or not the object has changed it's values.
		/// </summary>
		public bool IsDeleted
		{
			get { return <%= _prefix %>isDeleted; }
		}
		
		#endregion 
<%
		}
	}
	
	private void BuildPublicFunctions( IColumns Columns )
	{%>
		#region Public Functions
		
		/// <summary>
		/// mark the item as deleted
		/// </summary>
		public void MarkAsDeleted()
		{
			<%= _prefix %>isDeleted = true;
			<%= _prefix %>isChanged = true;
		}
		
		#endregion<%
	}
	
	private void BuildHBMDefinition( IColumns Columns )
	{
		if( Columns.Count > 0 )
		{
			output.writeln( "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" );
			output.writeln( NHibernateMappingTag() );
			output.writeln( "\t" + NHibernateClassTag( Columns ) );
			output.writeln( "\t\t" + NHibernatePrimaryKeysTag( Columns ) );
			output.writeln( "\t\t" + NHibernateProperties( Columns ) );
			output.writeln( "\t</class>" );
			output.writeln( "</hibernate-mapping>" );
		}
	}
	
	private string NHibernateMappingTag()
	{
		//// can't handle external mappings ?!?
		////string xml = "<hibernate-mapping xmlns=\"http://nhibernate.sourceforge.net/schemas/nhibernate-mapping-2.0.xsd\"";
		//string xml = "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.0\"";
		//// handle schemas, cascade, import, and access methods?
		//return xml + ">";
		return "<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.0\">";
	}
	
	private string NHibernateClassTag( IColumns Columns )
	{
		//ITable t = Columns[0].Table;
		//IView v = Columns[0].View;
		//string desc = ( t == null ) ? v.Description : t.Description;
		StringBuilder xml = new StringBuilder();
		xml.Append( "<class name=\"" ).Append( _nameSpace ).Append( "." ).Append( _className ).Append( "," ).Append( _nameSpace ).Append( "\"" );
		xml.Append( " table=\"" ).Append( _tableName ).Append( "\"" );
		if( _createReadOnly )
		{
			xml.Append( " mutable=\"false\"" );
		}
		// handle schema override, dynamic insert & update, and proxies?
		xml.Append( ">\r\n" );
		return xml.ToString();
	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -