根据DbSchema生成代码2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.IO;
using Rocky;
namespace Rocky.CodeBuilder
{
public class DbBuilder : Disposable
{
#region 字段
public event EventHandler DoWork;
private BackgroundWorker bgWorker;
#endregion
#region 属性
public event ProgressChangedEventHandler ProgressChanged
{
add { bgWorker.ProgressChanged += value; }
remove { bgWorker.ProgressChanged -= value; }
}
public event RunWorkerCompletedEventHandler RunWorkerCompleted
{
add { bgWorker.RunWorkerCompleted += value; }
remove { bgWorker.RunWorkerCompleted -= value; }
}
public DbSchema Schema { get; set; }
#endregion
#region 方法
public DbBuilder()
{
bgWorker = new BackgroundWorker();
bgWorker.WorkerReportsProgress = true;
bgWorker.WorkerSupportsCancellation = true;
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
}
protected override void DisposeInternal(bool disposing)
{
if (disposing)
{
bgWorker.Dispose();
}
DoWork = null;
bgWorker = null;
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (this.Schema == null)
{
this.Schema = SystemConfig.Instance.GetDbSchema();
if (this.Schema == null)
{
bgWorker.ReportProgress(100, "GetDbSchema error.");
return;
}
}
bgWorker.ReportProgress(0, "Working...");
var schema = this.Schema.GetSchema();
GenerateMode mode = SystemConfig.Instance.Mode;
if ((mode & GenerateMode.Entity) == GenerateMode.Entity)
{
AppendEntity(schema);
}
if ((mode & GenerateMode.DAC) == GenerateMode.DAC)
{
AppendDAC(schema);
}
//if ((mode & GenerateMode.TableImage) == GenerateMode.TableImage)
//{
//}
bgWorker.ReportProgress(100, "Completed.");
}
public void RunWorkerAsync()
{
base.CheckDisposed();
if (DoWork != null)
{
DoWork(this, EventArgs.Empty);
}
if (!bgWorker.IsBusy)
{
bgWorker.RunWorkerAsync();
}
}
public void CancelAsync()
{
if (bgWorker.IsBusy)
{
bgWorker.CancelAsync();
}
}
#endregion
#region Append
#region Entity
protected void AppendEntity(DbTableSchema[] schema)
{
string path = SystemConfig.Instance.SavePath + SystemConfig.Instance.FolderName + @"\Entity\";
Runtime.CreateDirectory(path);
StringBuilder main = new StringBuilder(1600);
for (int i = 0; i < schema.Length; i++)
{
DbTableSchema t_schema = schema[i];
main.AppendLine("using System;");
main.AppendLine("using Rocky.Data;");
main.AppendLine();
main.AppendLine("namespace " + SystemConfig.Instance.NameSpacePrefix + ".Entity");
main.AppendLine("{");
main.AppendLine(" [Table(\"" + t_schema.Name + "\"), Serializable]");
main.AppendLine(" public class " + t_schema.Name);
main.AppendLine(" {");
for (int j = 0; j < t_schema.Columns.Length; j++)
{
DbColumnSchema c_schema = t_schema.Columns[j];
TypeCode typeCode = Type.GetTypeCode(c_schema.FieldType);
string fieldType = c_schema.FieldType.Name;
switch (typeCode)
{
case TypeCode.String:
case TypeCode.Object:
if (c_schema.FieldType == typeof(Guid))
{
if (c_schema.IsNullable)
{
goto default;
}
}
if (c_schema.IsPrimaryKey)
{
main.AppendLine(" [Column(IsNullable = false, IsPrimaryKey = true)]");
}
else
{
main.AppendLine(" [Column(IsNullable = " + c_schema.IsNullable.ToString().ToLower() + ")]");
}
main.AppendLine(" public " + fieldType + " " + c_schema.Name + " { get; set; }");
main.AppendLine();
break;
case TypeCode.Int32:
case TypeCode.Int64:
if (c_schema.IsNullable)
{
goto default;
}
if (c_schema.IsPrimaryKey)
{
main.AppendLine(" [Column(IsDbGenerated = true, IsPrimaryKey = true)]");
}
else
{
main.AppendLine(" [Column(IsNullable = false)]");
}
main.AppendLine(" public " + fieldType + " " + c_schema.Name + " { get; set; }");
main.AppendLine();
break;
default:
if (c_schema.IsNullable)
{
fieldType += "?";
}
main.AppendLine(" [Column(IsNullable = " + c_schema.IsNullable.ToString().ToLower() + ")]");
main.AppendLine(" public " + fieldType + " " + c_schema.Name + " { get; set; }");
main.AppendLine();
break;
}
}
main.AppendLine(" }");
main.AppendLine("}");
File.WriteAllText(path + t_schema.Name + ".cs", main.ToString());
main.Length = 0;
bgWorker.ReportProgress((100 * i) / schema.Length, t_schema.Name + "Entity");
Thread.Sleep(200);
}
}
#endregion
#region Repository
protected void AppendDAC(DbTableSchema[] schema)
{
string path = SystemConfig.Instance.SavePath + SystemConfig.Instance.FolderName + @"\DAC\";
Runtime.CreateDirectory(path);
path += "TableComponents.cs";
Runtime.CreateFileFromResource(CoreHelper.Template_TableComponents, path);
StringBuilder main = new StringBuilder();
for (int i = 0; i < schema.Length; i++)
{
DbTableSchema dt = schema[i];
string type = "EntityTable<" + dt.Name + ">";
main.AppendLine(" public static readonly " + type + " Db" + dt.Name + " = new " + type + "();");
bgWorker.ReportProgress((100 * i) / schema.Length, dt.Name + "DAC");
Thread.Sleep(200);
}
File.WriteAllText(path, File.ReadAllText(path).Replace("_namespace_", SystemConfig.Instance.NameSpacePrefix).Replace("_Class_", main.ToString()));
}
#endregion
#endregion
}
}
posted on 2013-04-02 18:28 RockyLOMO 阅读(...) 评论(...) 编辑 收藏