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

📄 nhibernate.cst

📁 nhibernate编程,功能强大且实用
💻 CST
字号:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="This Template Generates C# domain objects for NHibernate" Debug="true" %>
<%@ Property Name="Namespace" Type="System.String" Default="MyNamespace.Data" Category="Object" Description="The class namespace that the mapping file should use" %>
<%@ Property Name="Assembly" Type="System.String" Default="MyApp.MyAssembly" Category="Object" Description="The assembly that the class will be used in" %>
<%@ Property Name="RemoveTablePrefix" Type="System.String" Default="tbl" Category="Object" Description="The prefix to remove from table names" %>
<%@ Property Name="ForceId" Type="System.Boolean" Default="true" Category="Object" Description="Force Id for identity column" %>
<%@ Assembly Name="System.Design" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Windows.Forms.Design" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<script runat="template">
	private DatabaseSchema _sourceDatabase;
	private string _outputDirectory;
    private bool _implementNotification = true;

	private CodeTemplate _csharpTemplate;
	private CodeTemplate _mappingTemplate;

    [Category("Database")]
    [Description("Database that the mapping file should be based on.")]
    public DatabaseSchema SourceDatabase {
        get { return _sourceDatabase; }
        set { _sourceDatabase = value; }
    }

	[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
	[Category("Class")]
    [Description("The folder to save the generated class files.")]
	public string OutputDirectory
	{
		get {return _outputDirectory;}
		set {_outputDirectory= value;}
	}

	[Browsable(false)]
	public CodeTemplate ClassTemplate
	{
		get
		{
			if (_csharpTemplate == null)
				_csharpTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "NHibernate.class.cst");

			return _csharpTemplate;
		}
	}

	[Browsable(false)]
	public CodeTemplate MappingTemplate
	{
		get
		{
			if (_mappingTemplate == null)
				_mappingTemplate = CompileTemplate(CodeTemplateInfo.DirectoryName + "NHibernate.hbm.cst");

			return _mappingTemplate;
		}
	}

	public CodeTemplate CompileTemplate(string templateName)
	{
		CodeTemplateCompiler compiler = new CodeTemplateCompiler(templateName);
		compiler.Compile();

		if (compiler.Errors.Count == 0)
		{
			return compiler.CreateInstance();
		}
		else
		{
			for (int i = 0; i < compiler.Errors.Count; i++)
			{
				Response.WriteLine(compiler.Errors[i].ToString());
			}
			return null;
		}

	}

	public void Generate()
	{
		foreach(TableSchema sourceTable in SourceDatabase.Tables)
		{
			Response.Write(string.Format("Processing Table {0} ... ", sourceTable.Name));
			if (IsManyToManyTable(sourceTable))
			{
				Response.WriteLine("skipped, many-to-many link table");
			}
			else if (IsSubClassTable(sourceTable))
			{
				Response.WriteLine("skipped, sub-class table");
			}
			else if (sourceTable.PrimaryKey == null)
			{
				Response.WriteLine("skipped, no primary key");
			}
			else
			{
				try
				{
				string className = sourceTable.Name;
				if (className.StartsWith(RemoveTablePrefix))
					className = className.Substring(RemoveTablePrefix.Length);

				string classFileName = className;
				classFileName += ".cs";
				classFileName = Path.Combine(OutputDirectory, classFileName);

				string mappingFileName = className + ".hbm.xml";
				mappingFileName = Path.Combine(OutputDirectory, mappingFileName);

				this.ClassTemplate.SetProperty("SourceTable", sourceTable);
				this.ClassTemplate.SetProperty("Namespace", Namespace);
				this.ClassTemplate.SetProperty("Assembly", Assembly);
				this.ClassTemplate.SetProperty("RemoveTablePrefix", RemoveTablePrefix);
				this.ClassTemplate.SetProperty("ForceId", ForceId);

				this.ClassTemplate.RenderToFile(classFileName, true);

				Response.Write(string.Format("{0} ", classFileName));

				this.MappingTemplate.SetProperty("SourceTable", sourceTable);
				this.MappingTemplate.SetProperty("Namespace", Namespace);
				this.MappingTemplate.SetProperty("Assembly", Assembly);
				this.MappingTemplate.SetProperty("RemoveTablePrefix", RemoveTablePrefix);
				this.MappingTemplate.SetProperty("ForceId", ForceId);

				this.MappingTemplate.RenderToFile(mappingFileName, true);

				Response.WriteLine(string.Format("{0} ", mappingFileName));

				}
				catch (Exception ex)
				{
					Response.WriteLine("Error: " + ex);
				}
			}
		}
	}
</script>
<script runat="template">
private Regex cleanRegEx = new Regex(@"\s+|_|-|\.", RegexOptions.Compiled);
private Regex cleanID = new Regex(@"(_ID|_id|_Id|\.ID|\.id|\.Id|ID|Id)", RegexOptions.Compiled);

public string CleanName(string name)
{
	return cleanRegEx.Replace(name, "");
}

public string CamelCase(string name)
{
	string output = CleanName(name);
	return char.ToLower(output[0]) + output.Substring(1);
}

public string PascalCase(string name)
{
	string output = CleanName(name);
	return char.ToUpper(output[0]) + output.Substring(1);
}

public string MakePlural(string name)
{
	Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
	Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
	Regex plural3 = new Regex("(?<keep>[sxzh])$");
	Regex plural4 = new Regex("(?<keep>[^sxzhy])$");

	if(plural1.IsMatch(name))
		return plural1.Replace(name, "${keep}ies");
	else if(plural2.IsMatch(name))
		return plural2.Replace(name, "${keep}s");
	else if(plural3.IsMatch(name))
		return plural3.Replace(name, "${keep}es");
	else if(plural4.IsMatch(name))
		return plural4.Replace(name, "${keep}s");

	return name;
}

public string MakeSingle(string name)
{
	Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
	Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
	Regex plural3 = new Regex("(?<keep>[sxzh])es$");
	Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");

	if(plural1.IsMatch(name))
		return plural1.Replace(name, "${keep}y");
	else if(plural2.IsMatch(name))
		return plural2.Replace(name, "${keep}");
	else if(plural3.IsMatch(name))
		return plural3.Replace(name, "${keep}");
	else if(plural4.IsMatch(name))
		return plural4.Replace(name, "${keep}");

	return name;
}

public bool IsManyToManyTable(TableSchema table)
{
	if (table.Columns.Count == 2 && table.PrimaryKey != null && table.PrimaryKey.MemberColumns.Count == 2 && table.ForeignKeys.Count == 2)
//	if (table.Columns.Count >= 2 && table.PrimaryKey != null && table.PrimaryKey.MemberColumns.Count == 2 && table.ForeignKeys.Count >= 2)
		return true;
	else
		return false;
}

public bool IsSubClassTable(TableSchema sourceTable)
{
	int count = 0;
	foreach(TableKeySchema foreignKey in sourceTable.ForeignKeys)
	{
		if (foreignKey.ForeignKeyMemberColumns[0].IsPrimaryKeyMember)
		{
			//Response.Write(String.Format(" {0}:", foreignKey.PrimaryKeyTable.Name));
			foreach(TableKeySchema primaryKey in foreignKey.PrimaryKeyTable.PrimaryKeys)
			{
				//Response.Write(String.Format(" {0} ", primaryKey.ForeignKeyTable.Name ));
				if (primaryKey.ForeignKeyMemberColumns[0].IsPrimaryKeyMember && primaryKey.ForeignKeyTable.PrimaryKey.MemberColumns.Count == 1)
					count++;
			}
		}
	}
	if (count > 1)
		return true;

	return false;
}
</script>
<% this.Generate();  %>

⌨️ 快捷键说明

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