using System;
using System.Collections.Generic;
using System.Linq;
using Yb.CustomTemplating;
using Yb.DbSchemaReader.DataSchema;
using Yb.Plugin.Base;
using Yb.Utility;
namespace Yb.Plugin.EfCslaDx.WinForm
{
public
abstract
class EfCslaDxWinPluginRepositoryBase : PluginRepositoryBase
{
private
int _totalCount =
0;
private
int _currentIndex =
0;
protected
virtual
int GetShouldBuildCodeNum(
int listCount)
{
var templateCount = GetTemplateInfos().Count();
return listCount * templateCount;
}
///
<summary>
///
生成代码
///
</summary>
///
<param name="dataSource"></param>
///
<param name="templateInfos"></param>
///
<returns></returns>
public
override IEnumerable<TemplateInfo> BuildCodes(
object dataSource, IEnumerable<TemplateInfo> templateInfos,
object dataSourceInfo)
{
//
参数类型转换
var tableInfos = BaseInfoUtility.ObjectToDatabaseTableList(dataSource).Where(c => c.Checked).ToList();
//
生成结果
var templateList =
new List<TemplateInfo>();
_totalCount = GetShouldBuildCodeNum(tableInfos.Count);
_currentIndex =
0;
foreach (
var templateInfo
in templateInfos)
{
if (!templateInfo.BuildEnable)
continue;
//
读模板信息
templateInfo.TemplateContent = FileUtility.ReadFile(templateInfo.TemplateRelativePath,templateInfo.Encoding);
//
判断模板类型,是否每张表都需生成一个模板
if ((templateInfo.Tag & StaticResources.IsTableInfoListOfTemplateArg) ==
0)
{
foreach (
var tableInfo
in tableInfos)
{
//
复制模板,防止生成后下一个循环被覆盖
var currentTemplateInfo = (TemplateInfo)templateInfo.Clone();
//
生成代码
BuildTableCode(
"
CurrentTable
", templateList, tableInfo, currentTemplateInfo);
}
}
else
{
//
生成 App.Config 文件
if (templateInfo.ExportFileNameFormatString.Equals(
"
App.config
", StringComparison.OrdinalIgnoreCase)
||templateInfo.ExportFileNameFormatString.Equals(
"
Web.config
", StringComparison.OrdinalIgnoreCase))
{
var sourceInfo = dataSourceInfo
as DataSourceInfo;
//
创建Web.Config代码
if (sourceInfo !=
null)
BuildAppConfigCode(templateList, templateInfo, tableInfos, sourceInfo);
}
else
{
//
创建如项目文件等需要传入“表对象集合”为参数的模板代码
BuildTablesCode(templateList, templateInfo, tableInfos);
}
}
}
return templateList;
}
///
<summary>
///
创建和表有关的代码
///
</summary>
///
<param name="templateArgName"></param>
///
<param name="templateList"></param>
///
<param name="tableInfo"></param>
///
<param name="templateInfo"></param>
private
void BuildTableCode(
string templateArgName, List<TemplateInfo> templateList, DatabaseTable tableInfo, TemplateInfo templateInfo)
{
try
{
//
代码生成关键代码,根据模板生成代码
templateInfo.ExportContent = Template.Transform(templateInfo.TemplateContent, templateArgName,
tableInfo);
//
更新模板标题,标题也是要生成文件的名称
templateInfo.Title = BaseInfoUtility.ReplaceString(templateInfo.ExportFileNameFormatString,
tableInfo.Name);
templateInfo.ExportRelativePath = BaseInfoUtility.ReplaceString(templateInfo.ExportRelativePathFormatString,
tableInfo.Name);
AddCodeList(templateList, templateInfo);
}
catch (Exception er)
{
NotifyException(templateInfo, er);
}
}
///
<summary>
///
创建和数据库有关的代码
///
</summary>
///
<param name="templateList"></param>
///
<param name="templateInfo"></param>
///
<param name="tableInfos"></param>
private
void BuildTablesCode(List<TemplateInfo> templateList, TemplateInfo templateInfo, List<DatabaseTable> tableInfos)
{
try
{
templateInfo.ExportContent = Template.Transform(templateInfo.TemplateContent,
"
TableInfos
",
tableInfos);
templateInfo.Title = templateInfo.ExportFileNameFormatString;
templateInfo.ExportRelativePath = templateInfo.ExportRelativePathFormatString;
AddCodeList(templateList, templateInfo);
}
catch (Exception er)
{
NotifyException(templateInfo, er);
}
}
///
<summary>
///
创建Web.Config 代码
///
</summary>
///
<param name="templateList"></param>
///
<param name="templateInfo"></param>
///
<param name="tableInfos"></param>
private
void BuildAppConfigCode(List<TemplateInfo> templateList, TemplateInfo templateInfo, List<DatabaseTable> tableInfos, DataSourceInfo dataSourceInfo)
{
try
{
templateInfo.ExportContent = Template.Transform(templateInfo.TemplateContent,
"
DataSourceInfo
",
dataSourceInfo);
templateInfo.Title = templateInfo.ExportFileNameFormatString;
templateInfo.ExportRelativePath = templateInfo.ExportRelativePathFormatString;
AddCodeList(templateList, templateInfo);
}
catch (Exception er)
{
NotifyException(templateInfo, er);
}
}
///
<summary>
///
代码创建后进行事件通知
///
</summary>
///
<param name="templateList"></param>
///
<param name="templateInfo"></param>
private
void AddCodeList(List<TemplateInfo> templateList, TemplateInfo templateInfo)
{
templateList.Add(templateInfo);
_currentIndex++;
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Seccessful,
string.Format(
"
代码 {0} 生成成功
", templateInfo.Title)
, _currentIndex, _totalCount));
}
///
<summary>
///
通知代码创建失败
///
</summary>
///
<param name="templateInfo"></param>
///
<param name="er"></param>
private
void NotifyException(TemplateInfo templateInfo, Exception er)
{
_currentIndex++;
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Error,
string.Format(
"
代码 {0} 生成失败,模板名称:{1}。\r\n{2}
", templateInfo.Title,templateInfo.TemplateRelativePath, er.Message),
_currentIndex, _totalCount));
}
public
override IEnumerable<TemplateInfo> GetTemplateInfos()
{
return StaticResources.GetTemplateInfos();
}
}
}
EfCslaDxWinEntitiesPluginRepository
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Yb.CustomTemplating;
using Yb.Plugin.Base;
using Yb.Utility;
namespace Yb.Plugin.EfCslaDx.WinForm
{
[PluginMetadataAttribute(Name =
"
实体代码生成
",
Description =
"
生成 Entity Framework Code First 实体对象
",
DisplayOrder =
5,
ImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/Win_EF_Entity_64x64.png
",
LargeImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/Win_EF_Entity_64x64.png
",
CodeOutputMode = CodeOutputMode.PreView)]
[Export(StaticResources.PluginGroupKey,
typeof(IPluginRepository))]
public
class EfCslaDxWinEntitiesPluginRepository : EfCslaDxWinPluginRepositoryBase
{
public
override IEnumerable<TemplateInfo> GetTemplateInfos()
{
var templateInfo =
base.GetTemplateInfos();
//
通过 Tag 来计算是否需要使用该模板
return templateInfo.Where(c => c.BuildEnable && (c.Tag & StaticResources.EntitiesPluginTemplateTag) >
0);
}
}
}
EfCslaDxWinBusinessPluginRepository
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Yb.Plugin.Base;
using Yb.Plugin.EfCslaDx.WinForm;
namespace Yb.Plugin.EfCslaDx.WinForm
{
[PluginMetadataAttribute(Name =
"
CSLA业务逻辑层代码
",
Description =
"
生成基本 CSLA.NET 的业务逻辑层代码
",
DisplayOrder =
15,
ImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/CSLA.png
",
LargeImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/CSLA.png
",
CodeOutputMode = CodeOutputMode.PreView)]
[Export(StaticResources.PluginGroupKey,
typeof(IPluginRepository))]
public
class EfCslaDxWinBusinessPluginRepository : EfCslaDxWinPluginRepositoryBase
{
///
<summary>
///
获取业务层模板
///
</summary>
///
<returns></returns>
public
override IEnumerable<TemplateInfo> GetTemplateInfos()
{
var templateInfo =
base.GetTemplateInfos();
return templateInfo.Where(c => c.BuildEnable && (c.Tag & StaticResources.BusinessPluginTemplateTag) >
0);
}
}
}
EfCslaDxWinRepositoriesPluginRepository
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Yb.Plugin.Base;
namespace Yb.Plugin.EfCslaDx.WinForm
{
[PluginMetadataAttribute(Name =
"
数据访问层代码
",
Description =
"
生成数据访问层代码
",
DisplayOrder =
10,
ImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/Win_EF_Respository_64x64.png
",
LargeImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/Win_EF_Respository_64x64.png
",
CodeOutputMode = CodeOutputMode.PreView)]
[Export(StaticResources.PluginGroupKey,
typeof(IPluginRepository))]
public
class EfCslaDxWinRepositoriesPluginRepository : EfCslaDxWinPluginRepositoryBase
{
///
<summary>
///
获取界面层模板
///
</summary>
///
<returns></returns>
public
override IEnumerable<TemplateInfo> GetTemplateInfos()
{
var templateInfo =
base.GetTemplateInfos();
return templateInfo.Where(c => c.BuildEnable && (c.Tag & StaticResources.RepositoryPluginTemplateTag) >
0);
}
}
}
EfCslaDxWinUIPluginRepository
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Yb.Plugin.Base;
using Yb.Plugin.EfCslaDx.WinForm;
namespace Yb.Plugin.EfCslaDx.WinForm
{
[PluginMetadataAttribute(Name =
"
WinForm界面层代码
",
Description =
"
生成DevExpress界面层代码
",
DisplayOrder =
20,
ImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/DXv2.png
",
LargeImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/DXv2.png
",
CodeOutputMode = CodeOutputMode.PreView)]
[Export(StaticResources.PluginGroupKey,
typeof(IPluginRepository))]
public
class EfCslaDxWinUIPluginRepository : EfCslaDxWinPluginRepositoryBase
{
///
<summary>
///
获取界面层模板
///
</summary>
///
<returns></returns>
public
override IEnumerable<TemplateInfo> GetTemplateInfos()
{
var templateInfo =
base.GetTemplateInfos();
return templateInfo.Where(c => c.BuildEnable && (c.Tag & StaticResources.WinFormPluginTemplateTag) >
0);
}
}
}
EfCslaDxWinSolutionPluginRepository
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Yb.Plugin.Base;
using Yb.Utility;
namespace Yb.Plugin.EfCslaDx.WinForm
{
[PluginMetadataAttribute(Name =
"
解决方案
",
Description =
"
生成Castel数据访问层,Catel视图模型层,DevExpress界面层代码的解决方案
",
DisplayOrder =
100,
ImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/Win.png
",
LargeImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/Win.png
",
CodeOutputMode = CodeOutputMode.File)]
[Export(StaticResources.PluginGroupKey,
typeof(IPluginRepository))]
public
class EfCslaDxWinSolutionPluginRepository : EfCslaDxWinPluginRepositoryBase
{
private
const
string ZipFileRelativePath =
@"
Plugin\Yb.Plugin.EfCslaDx.WinForm\ZipFiles\YbRapidSolution.Win.zip
";
protected
override
int GetShouldBuildCodeNum(
int listCount)
{
var templateCount = GetTemplateInfos().Count();
return listCount * (templateCount -
12)+
12;
}
///
<summary>
///
解压文件到指定路径
///
</summary>
///
<param name="path">
解压路径
</param>
public
override
void BeforeBuild(
string path)
{
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Infomation,
"
正在解压文件,请稍候
"));
//
解压文件到指定路径
FileUtility.ExtractFileTo(ZipFileRelativePath, path);
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Infomation,
"
解压文件结束
"));
}
///
<summary>
///
获取本插件所需的模板信息
///
</summary>
///
<returns></returns>
public
override IEnumerable<TemplateInfo> GetTemplateInfos()
{
var templateInfo =
base.GetTemplateInfos();
return templateInfo.Where(c => c.BuildEnable && (c.Tag & StaticResources.SolutionPluginTemplateTag) >
0);
}
///
<summary>
///
安装和权限有关的数据库脚本
///
</summary>
///
<param name="dataSourceInfo"></param>
public
override
void AfterBuild(
object dataSourceInfo)
{
if (dataSourceInfo ==
null)
return;
var sourceInfo = dataSourceInfo
as DataSourceInfo;
if (sourceInfo ==
null)
return;
var connStr = sourceInfo.DataSourceString;
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Infomation,
"
正在安装数据库脚本
"));
try
{
//
执行创建表的脚本文件
ExecuteSqlServerDbScript(connStr);
//
进度通知
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Infomation,
"
数据库脚本处理完成
"));
}
catch (Exception er)
{
//
进度通知
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Infomation,
string.Format(
"
数据库脚本安装失败,{0}
", er.Message)));
}
}
private
void ExecuteSqlServerDbScript(
string connStr)
{
var smoUtiltiy =
new SmoUtility(connStr);
var tableInfos = smoUtiltiy.GetTableInfos();
//
如果没有表信息或者不包含默认的和权限有关的表,则执行数据库表初始化脚本
if (tableInfos ==
null || tableInfos.Select(c => c.Name).Contains(StaticResources.MembershipConfigTableName))
{
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Infomation,
"
未安装数据库脚本,相关表已存在
"));
return;
}
//
创建相应表的脚本
string providerInstallScript = FileUtility.ReadFile(
@"
Plugin\Yb.Plugin.EfCslaDx.WinForm\Templates\ProviderInstallScript.sql
");
SqlServerDataUtility.ExecuteSqlServerDbScript(connStr, providerInstallScript);
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Infomation,
"
安装数据库脚本成功,应用程序默认登录名:admin,密码:123456
"));
}
}
}
EfCslaDxWinPluginGroupRepository
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Data;
using System.Linq;
using System.Text;
using Yb.Data;
using Yb.Plugin.Base;
using Yb.Utility;
namespace Yb.Plugin.EfCslaDx.WinForm
{
[PluginGroupAttribute(
Name =
"
Ef Csla Dx for WinForm
",
Description =
"
生成 EF 数据访问层,CSLA.NET 业务逻辑层,DxV2 界面层的 WinForm 代码及解决方案
",
DisplayOrder =
5,
DataSourceType = DataSourceType.Database,
DataSourceName = DataSourceName.SqlDataSource | DataSourceName.AccessDataSource,
ImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/WinFormGroup.png
",
LargeImageUri =
"
pack://application:,,,/Yb.Plugin.EfCslaDx.WinForm;component/Images/WinFormGroup.png
",
NavigationGroup =
"
Ef Csla Dx 代码生成插件
")]
[Export(
typeof(IPluginGroupRepository))]
public
class EfCslaDxWinPluginGroupRepository : IPluginGroupRepository, IPartImportsSatisfiedNotification
{
[ImportMany(StaticResources.PluginGroupKey, AllowRecomposition =
true)]
public IEnumerable<Lazy<IPluginRepository, IPluginMetadata>> LazyPluginRepositories {
get;
set; }
public
void OnImportsSatisfied()
{
LazyPluginRepositories = LazyPluginRepositories.OrderBy(c => c.Metadata.DisplayOrder);
}
///
<summary>
///
创建数据源
///
</summary>
<param name="arg"></param>
///
<returns></returns>
public
object CreateDataSource(
object arg)
{
if (!(arg
is DataSourceInfo))
{
throw
new ArgumentException(
"
参数不是有效的 DataSourceInfo 类型
",
"
arg
");
}
var dataSourceInfo = arg
as DataSourceInfo;
if (dataSourceInfo ==
null)
throw
new ArgumentNullException(
"
arg
",
"
DataSourceInfo 参数不能为空
");
var schemaUtil =
new DbSchemaUtility();
//
获取表信息
try
{
var tableInfos = schemaUtil.GetDbTables(dataSourceInfo);
//
过滤掉和权限有关的表
if (tableInfos !=
null && tableInfos.Count >
0)
tableInfos = tableInfos.Where(c => !StaticResources.SecurityDbTablesName.Contains(c.Name)).ToList();
return tableInfos;
}
catch (Exception er)
{
throw
new DataException(
string.Format(
"
获取表信息失败,{0}
", er.Message));
}
}
///
<summary>
///
初始化模板
///
</summary>
///
<returns></returns>
public IEnumerable<TemplateInfo> GetTemplateInfos()
{
return StaticResources.GetTemplateInfos();
}
}
}