<%-- 
Name:项目文件生成器
Author: winson
Ver:3.3
Description: 项目文件生成器
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="创建Winson.Framework项目文件." ResponseEncoding="UTF-8"%>
<%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="数据库" Description="连接数据库对象." %>
<%@ Property Name="ProjectName" Type="System.String" Default="Winson" Optional="False" Category="项目" Description="项目名称" %>
<%@ Property Name="UseIdentityID" Type="System.Boolean" Default="False" Optional="False" Category="项目" Description="是否使用自增主键" %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="可获取数据类型" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %> 
<%@ Assembly Name="System.Data" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Design" %>

<% if(!Directory.Exists(OutputDirectory)) Directory.CreateDirectory(OutputDirectory); 

//生成实体基类文件
CodeTemplate EntityObjectTemplate = GetCodeTemplate("Base\\BaseOP.cst");
string EntityDirectory = OutputDirectory + "\\Winson.Framework\\"+ProjectName+"\\EntityOP\\Base\\";

if(!Directory.Exists(EntityDirectory)) Directory.CreateDirectory(EntityDirectory);

RenderFileUsingProperties(EntityDirectory  + "BaseOP.cs",true,ref EntityObjectTemplate); 

//生成实体事件
 EntityObjectTemplate = GetCodeTemplate("Base\\OPEventHandler.cst");
 EntityDirectory = OutputDirectory + "\\Winson.Framework\\"+ProjectName+"\\EntityOP\\Base\\";

if(!Directory.Exists(EntityDirectory)) Directory.CreateDirectory(EntityDirectory);

RenderFileUsingProperties(EntityDirectory  + "OPEventHandler.cs",true,ref EntityObjectTemplate); 
%>
实体基类文件生成成功!

<%
//生成实体类文件
 EntityObjectTemplate = GetCodeTemplate("Entity\\Entity.cst");
 EntityDirectory = OutputDirectory + "\\Winson.Framework\\"+ProjectName+"\\EntityOP\\Entity\\";

if(!Directory.Exists(EntityDirectory)) Directory.CreateDirectory(EntityDirectory);

//写入文件
for (int i = 0; i < SourceDatabase.Tables.Count; i++)
{
	if(SourceDatabase.Tables[i].PrimaryKey!=null)
	{
		switch(SourceDatabase.Tables[i].Columns[0].SystemType.FullName)
		{
			case "System.Int32":
            case "System.Int64":
			case "System.Int16":
            case "System.Double":
            case "System.SByte":
			case "System.Byte":
            case "System.UInt32":
            case "System.UInt64":
			case "System.Decimal":
				RenderFileUsingProperties(EntityDirectory + SourceDatabase.Tables[i].Name + "DS.cs",true,ref EntityObjectTemplate,SourceDatabase.Tables[i]);    
				break;
			default:
			%>
				<%=SourceDatabase.Tables[i].Name%>　　表的主键不是数字型，因此将不被生成
			
			<%
				break;				
		}
	}    
}
%>
实体类文件生成成功!

<% 
//生成实体操作类文件
 EntityObjectTemplate = GetCodeTemplate("Entity\\BaseEntityOP.cst");
 EntityDirectory = OutputDirectory + "\\Winson.Framework\\"+ProjectName+"\\EntityOP\\BaseEntityOP\\";

if(!Directory.Exists(EntityDirectory)) Directory.CreateDirectory(EntityDirectory);

//写入文件
for (int i = 0; i < SourceDatabase.Tables.Count; i++)
{
	if(SourceDatabase.Tables[i].PrimaryKey!=null)
	{
		switch(SourceDatabase.Tables[i].Columns[0].SystemType.FullName)
		{
			case "System.Int32":
            case "System.Int64":
            case "System.Double":
            case "System.SByte":
            case "System.UInt32":
            case "System.UInt64":
			case "System.Int16":
			case "System.Byte":
			case "System.Decimal":
				RenderFileUsingProperties(EntityDirectory + SourceDatabase.Tables[i].Name + "OP.cs",true,ref EntityObjectTemplate,SourceDatabase.Tables[i]); 
				break;
			default:
			%>
				<%=SourceDatabase.Tables[i].Name%>　　表的主键不是数字型，因此将不被生成
			
			<%
				break;				
		}
	}    
}
%>
实体操作基类文件生成成功!


<% 
// 生成实体操作类文件
 EntityObjectTemplate = GetCodeTemplate("Entity\\EntityOP.cst");
 EntityDirectory = OutputDirectory + "\\Winson.Framework\\"+ProjectName+"\\EntityOP\\EntityOP\\";

if(!Directory.Exists(EntityDirectory)) Directory.CreateDirectory(EntityDirectory);

//写入文件
for (int i = 0; i < SourceDatabase.Tables.Count; i++)
{
	if(SourceDatabase.Tables[i].PrimaryKey!=null)
	{
		switch(SourceDatabase.Tables[i].Columns[0].SystemType.FullName)
		{
			case "System.Int32":
            case "System.Int64":
            case "System.Double":
            case "System.SByte":
            case "System.UInt32":
            case "System.UInt64":
			case "System.Int16":
			case "System.Byte":
			case "System.Decimal":
				RenderFileUsingProperties(EntityDirectory + SourceDatabase.Tables[i].Name + ".cs",true,ref EntityObjectTemplate,SourceDatabase.Tables[i]); 
				break;
			default:
			%>
				<%=SourceDatabase.Tables[i].Name%>　　表的主键不是数字型，因此将不被生成
			
			<%
				break;				
		}
	}    
}

%>
实体操作类文件生成成功!

使用方法：
创建一个新项目，名为EntityOP，将生成目录里三个子目录COPY到此项目里，然后编译即可


如有任何问题，请访问 http://bbs.szblogs.com

深圳博客技术论坛
<script runat="template">

    #region 输出
    private string _outputDirectory = String.Empty;
    
    [Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] 
    [CodeTemplateProperty(CodeTemplatePropertyOption.Optional)]
    [Category("General")]
    [Description("The directory to output the results to.")]
    [DefaultValue("")]
    public string OutputDirectory 
    { 
        get
        {
            if (_outputDirectory.Length == 0)
            {
                return @"D:\";
            }
            else
            {
                return _outputDirectory;
            }
        }
        set
        {
            if (value.EndsWith("\\")) value = value.Substring(0, value.Length - 1);
            _outputDirectory = value;
        } 
    }
    #endregion
    
    #region 获取模板
    public  CodeTemplate GetCodeTemplate(string TemplateName)
    {        
        //CodeTemplate template=null;
        CodeTemplateCompiler compiler = new CodeTemplateCompiler(this.CodeTemplateInfo.DirectoryName+TemplateName); 
        compiler.CodeTemplateInfo.ToString();
        compiler.Compile(); 
        if (compiler.Errors.Count == 0) 
        { 
            return compiler.CreateInstance();
        } 
        else 
        { 
            System.Text.StringBuilder errorMessage = new System.Text.StringBuilder();                
            for (int i = 0; i < compiler.Errors.Count; i++) 
            {
                errorMessage.Append(compiler.Errors[i].ToString()).Append("\n");
            } 
            throw new ApplicationException(errorMessage.ToString());
        } 
              
        return null;
    }
    #endregion
    
    #region 过滤不需要的表 
    #region TableUtilities Class
    public class TableUtilities
    {
        public static bool SkipTable(TableSchema table)
        {
            return (table.PrimaryKey == null || table.Columns.Count < 2);
        }
        public static TableSchemaCollection FilterTables(TableSchemaCollection tables)
        {            
            TableSchemaCollection filtered = new TableSchemaCollection();
            foreach(TableSchema table in tables) 
            {
                if(!SkipTable(table)) filtered.Add(table);
            }
            return filtered;
        }
    }
    #endregion
    
#endregion

    #region 写入文件
     
	/// <summary>
    /// 产生相应的文件
    /// </summary>
    /// <param name="fullPath">输出路径</param>
    /// <param name="overrite">是否复盖现有文件</param>
    /// <param name="template">模板对象</param>
    /// <param name="SourceTable">需要操作的表</param>
    public void RenderFileUsingProperties(string fullPath, bool overrite, ref CodeTemplate template, TableSchema SourceTable)
    {
        if (overrite || (!File.Exists(fullPath)))
        {
            CopyPropertiesTo(template);
            template.SetProperty("SourceDatabase", SourceDatabase);
			template.SetProperty("SourceTable", SourceTable);
			template.SetProperty("ProjectName", ProjectName);
			template.SetProperty("UseIdentityID", UseIdentityID);
            template.RenderToFile(fullPath, overrite);
        }
    }
		
	/// <summary>
    /// 产生实体基类文件
    /// </summary>
    /// <param name="fullPath">输出路径</param>
    /// <param name="overrite">是否复盖现有文件</param>
    /// <param name="template">模板对象</param>
    public void RenderFileUsingProperties(string fullPath, bool overrite, ref CodeTemplate template)
    {
        if (overrite || (!File.Exists(fullPath)))
        {
            CopyPropertiesTo(template);
			template.SetProperty("ProjectName", ProjectName);
            template.RenderToFile(fullPath, overrite);
        }
    }
    
    #endregion
</script>



