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

📄 collections.cs

📁 客户关系管理系统ASP.NET+VB.NET编程完整程序!
💻 CS
字号:
//This file is part of ORM.NET.
//
//ORM.NET is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//ORM.NET is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with ORM.NET; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


using System;
using System.Reflection;
using System.Text;

using ORMBiz;

namespace ORM.NET
{
	/// <summary>
	/// Summary description for Generator.
	/// </summary>
	public class CollectionsGenerator
	{
		private	TemplateParser parser = new TemplateParser();
		public CollectionsGenerator()
		{
		}

		public	string	Generate( Database db)
		{
			string namespaceString = parser.LoadTemplate(@"Biz\Collections\Namespace");
			StringBuilder sb = new StringBuilder();

			foreach(Table t in db.Tables)
			{
				if ( t.GenerateCodeFor.ToLower() == "true" )
					sb.Append( GetFormattedClassCollectionTemplate( t) );
			}
			return parser.RestoreAfterFormatting( string.Format(namespaceString, Shared.NameSpace, sb.ToString() ) );
		}

		public	string	GetFormattedClassCollectionTemplate(Table table)
		{
			string classCollection = parser.LoadTemplate(@"Biz\Collections\ClassCollection");
			StringBuilder sbFindBy = new StringBuilder();
			StringBuilder sbFilterBy = new StringBuilder();
			StringBuilder sbSortBy = new StringBuilder();

			foreach(Column c in table.Columns)
			{
				if (c.GetAccessor.ToLower() == "public virtual" )
				{
					if ( c.PrimaryKey.ToLower() == "true" )
					{
						sbFindBy.Append( GetFormattedCollectionFindBy( table.AccessorName, c.AccessorName, c.DataType.NetType, GetAccessorFunctionName(c)));
						sbFilterBy.Append( GetFormattedCollectionFilterBy( table.AccessorName, c.AccessorName, c.DataType.NetType, GetAccessorFunctionName(c)));
					}
					else
					{
						sbFindBy.Append( GetFormattedCollectionFindBy( table.AccessorName, c.AccessorName, c.DataType.NetSqlType, GetAccessorFunctionName(c)));
						sbFilterBy.Append( GetFormattedCollectionFilterBy( table.AccessorName, c.AccessorName, c.DataType.NetSqlType, GetAccessorFunctionName(c)));
					}
					sbSortBy.Append( GetFormattedCollectionSortBy( table.AccessorName, c.AccessorName));
				}
			}
						
			return string.Format( classCollection, table.AccessorName, sbFindBy.ToString(), sbFilterBy.ToString(),  sbSortBy.ToString());
		}

		public	string	GetFormattedCollectionFindBy( string accessorName, string columnName, string columnType, string GetAccessorFunctionName)
		{
			string collectionFindBy = parser.LoadTemplate(@"Biz\Collections\CollectionFindBy");
			string code = "";

			if ( HasParseMethod(columnType) )
			{
				string stringFindBy = parser.LoadTemplate(@"Biz\Collections\StringFindBy");
				code = string.Format(stringFindBy, accessorName, columnName, columnType, columnType.Replace("[]", "Array"));
			}

			return string.Format( collectionFindBy, accessorName, columnName, columnType, columnType.Replace("[]", "Array")) + code;
		}

		private bool HasParseMethod( string type )
		{
			Type t;
 
			if ( type.ToLower().StartsWith("system.data") )
			{
				t = Assembly.LoadWithPartialName("System.Data").GetType(type);
			}
			else
			{
				t = Assembly.LoadWithPartialName("mscorlib").GetType(type);
			}

			if ( t == null )
				return false;

			MethodInfo mi = t.GetMethod("Parse", new Type[]{typeof(string)});
			return (bool)( mi != null );
		}

		public	string	GetFormattedCollectionFilterBy( string accessorName, string columnName, string columnType, string GetAccessorFunctionName)
		{
			string collectionFilterBy = parser.LoadTemplate(@"Biz\Collections\CollectionFilterBy");

			string code = "";

			if ( HasParseMethod(columnType) )
			{
				string stringFindBy = parser.LoadTemplate(@"Biz\Collections\StringFilterBy");
				code = string.Format(stringFindBy, accessorName, columnName, columnType, columnType.Replace("[]", "Array"));
			}

			return string.Format( collectionFilterBy, accessorName, columnName, columnType, columnType.Replace("[]", "Array")) + code;
		}

		public	string	GetFormattedCollectionSortBy(string accessorName, string columnName)
		{
			string collectionSortBy = parser.LoadTemplate(@"Biz\Collections\CollectionSortBy");
			return string.Format( collectionSortBy, accessorName, columnName);
		}


		private string GetAccessorFunctionName(Column c)
		{
			// get and set same scope
			if ( c.SetAccessor.ToLower() == TemplateParser.GetScope(c.GetAccessor).ToLower())
				return c.AccessorName;
			
			else
			{
				// public?
				if ( c.GetAccessor.ToLower() == "public virtual" )
					return c.AccessorName;
				else
					return "Get_" + c.AccessorName + "()";
			}
		}
	}
}

⌨️ 快捷键说明

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