博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
YbSoftwareFactory 代码生成插件【二】:二次开发之 IPlugInGroupRepository 接口的实现...
阅读量:6124 次
发布时间:2019-06-21

本文共 10261 字,大约阅读时间需要 34 分钟。

    程序员得学会自我解放,YbSoftwareFactory就是这样一款解放程序员的利器,能让你更加专注于具体业务逻辑的实现,把自己从每日盲目的重复工作中解脱出来。能更有思想、更充分、更游刃有余地看待具体问题,而不仅仅是做一名水平低下的编码员。本章节开始将以一个Demo来重点介绍YbSoftwareFactory代码生成插件二次开发所需各个类的实现。 本章节将首先来看看“IPlugInGroupRepository”插件组接口的实现。

    开始之前,说说插件加载的原理吧。YbSoftwareFactory使用了MEF来实现插件的加载,这是插件工作原理的关键,因此,实现的各个类必须遵循MEF的契约规范,重点是要在具体的实现类上声明[Exprot]特性与Metadata。

    1、“IPlugInGroupRepository”接口的实现,该接口的实现类用于加载本组插件中的各个子插件。插件组将在YbSoftwareFactory左边的导航栏出现。

    2、CastleDxWebPlugInGroupRepository源码

ExpandedBlockStart.gif
CastleDxWebPlugInGroupRepository 类实现
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using Yb.PlugIn.Base;
using Yb.Utility;
namespace Yb.PlugIn.CastleDevExpress.WebForm{
    [PlugInGroupAttribute(
        Name = 
"
Castle DxV2 for WebForm
",
        Description = 
"
生成 Castle 数据访问层,DxV2 界面层代码及解决方案
",
        DisplayOrder = 
3,
        DataSourceType = DataSourceType.Database,
        DataSourceProviderString=DataSourceProviderString.SqlDataSource,
        ImageUri = 
"
pack://application:,,,/Yb.PlugIn.CastleDevExpress.WebForm;component/Images/WebFormSolution.png
",
        LargeImageUri = 
"
pack://application:,,,/Yb.PlugIn.CastleDevExpress.WebForm;component/Images/WebFormSolution.png
",
        NavigationGroup = 
"
Castle DxV2 代码生成插件
")]
    [Export(
typeof(IPlugInGroupRepository))]
    
public 
class CastleDxWebPlugInGroupRepository : IPlugInGroupRepository, IPartImportsSatisfiedNotification
    {
        [ImportMany(StaticResources.PlugInGroupKey,AllowRecomposition = 
true)]
        
public IEnumerable<Lazy<IPlugInRepository, IPlugInMetadata>> LazyPlugInRepositories { 
get
set; }
        
        
public 
void OnImportsSatisfied()
        {
            
        }
        
///
 
<summary>
        
///
 创建数据源
        
///
 
</summary>
 
<param name="arg"></param>
        
///
 
<returns></returns>
        
public 
object CreateDataSource(
object arg)
        {
            
if(arg == 
null || 
string.IsNullOrWhiteSpace(arg.ToString()))
                
throw 
new ArgumentNullException(
"
arg
",
"
参数不能为空
");
            
var connectionString = arg.ToString();
           
            
var smo=
new SmoUtility(connectionString);
            
//
获取表信息
            
try
            {
                
var tableInfos = smo.GetTableInfos();
                
//
过滤掉和权限有关的表
                
if (tableInfos != 
null && tableInfos.Count > 
0)
                    tableInfos = tableInfos.Where(c => !StaticResources.SecurityDbTablesName.Contains(c.Name)).ToList();
                
return tableInfos;
            }
            
catch (Exception er)
            {
                
throw 
new DataException(
"
获取表信息失败
",er);
            }
        }
        
///
 
<summary>
        
///
 初始化模板
        
///
 
</summary>
        
///
 
<returns></returns>
        
public IEnumerable<TemplateInfo> GetTemplateInfos()
        {
            
return StaticResources.GetTemplateInfos();
        }
    }
}

    3、重点元数据声明说明:

    Name:将在YbSoftwareFactory软件的NavigationControl中显示的名称。

    DataSourceType:要生成代码的数据源类型,当前仅仅支持数据源为数据库,未来支持Asssembly和XML等,因此此处声明为:DataSourceType.Database。

    DataSourceProviderString:插件数据源所支持的数据库类型,此处为SQL Server,如你的代码生成插件可支持多种数据库则可以在此处使用“|”分割(当前版本仅支持SQL Server)。

    ImageUri和LargeImageUri:WPF格式的资源URI声明,主要是在“Name”上显示个性化图标,注意在图片对应的“属性”窗口的把生成操作设置为“Resource”。

    NavigationGroup:控制该插件组在界面NavigationControl控件的哪一个Group组中显示。

    4、实现“LazyPlugInRepositories”属性,这个属性最关键的是特性声明,为避免加载其他人开发的插件,这里使用了如下声明:

    [ImportMany(StaticResources.PlugInGroupKey,AllowRecomposition = true)]

    其中“StaticResources.PlugInGroupKey”就是来控制要加载的插件的范围的,声明如下:

public const string PlugInGroupKey = "Yb.PlugIn.CastleDevExpress.WebForm";

    为避免和其他插件冲突,这里定义成了"Yb.PlugIn.CastleDevExpress.WebForm",一看就知道这是本项目的命名空间,建议通过这种方式来保证惟一性。

    5、CreateDataSource方法

    主要是生成在界面上要显示的表信息和字段信息,传入的arg参数是一个连接字符串信息,你可使用封装好的“SmoUtility”类快速、方便地生成数据库下的表信息。本Demo还对将在界面上显示的表进行了过滤,主要是对一些和权限控制有关的表进行过滤,禁止其在界面上显示。这些表将在该组的子插件下进行处理,最后使用SQL脚本统一安装。如你没有这方面的需求,你完全可以不用理会上面对数据库表进行过滤的代码。

    6、最后是“GetTemplateInfos”方法

    该方法主要是获取当前插件组的模板信息,用于进行模板管理,你可以自由的实现,本Demo首先判断有保存的模板的记录没有,没有则加载默认模板并保存,当前实现的核心代码如下:

ExpandedBlockStart.gif
GetTemplateInfos 方法
///
 
<summary>
        
///
 初始化模板
        
///
 
</summary>
        
///
 
<returns></returns>
        
public 
static IEnumerable<TemplateInfo> GetTemplateInfos()
        {
            
var dbHelper = 
new YbSqliteHelper();
            
//
加载保存到数据库中的模板
            
var result = dbHelper.FindTemplateInfosBy(StaticResources.PlugInGroupKey);
            
if (result == 
null || result.Count == 
0)
            {
                
//
数据库中不包含该插件组的任何模板信息,则保存到数据库
                
return InstallTemplates(dbHelper);
            }
            
return result;
        }
        
///
 
<summary>
        
///
 保存模板到数据库
        
///
 
</summary>
        
///
 
<param name="dbHelper"></param>
        
///
 
<returns></returns>
        
private 
static IEnumerable<TemplateInfo> InstallTemplates(YbSqliteHelper dbHelper)
        {
            
var templateInfos = StaticResources.GetDefaultTemplateInfos().ToList();
            
foreach (
var item 
in templateInfos)
                dbHelper.InsertTemplateInfo(item);
            
return templateInfos;
        }
///
 
<summary>
        
///
 默认模板信息
        
///
 
</summary>
        
///
 
<returns></returns>
        
public 
static IEnumerable<TemplateInfo> GetDefaultTemplateInfos()
        {
            
var templateInfos = 
new List<TemplateInfo>
                                    {
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
实体对象模板
",
                                                TemplateRelativePath = 
@"
Templates\CastleActiveRecord\Entity\User.tt
",
                                                Description = 
"
实体对象模板
",
                                                DisplayOrder = 
5,
                                                Encoding = Encoding.Default,
                                                ExportFileNameFormatString = 
"
%s.cs
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.Entities\%s
",
                                                    SyntaxHighlightingName = SyntaxHighlighting.CSharp,
                                                    BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = EntitiesPlugInTemplateTag|SolutionPlugInTemplateTag
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
实体对象后置代码模板
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\Entity\AutoGenerated.tt
",
                                                Description = 
"
实体对象后置代码模板
",
                                                DisplayOrder = 
10,
                                                Encoding = Encoding.Default,
                                                ExportFileNameFormatString = 
"
%s.AutoGenerated.cs
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.Entities\%s
",
                                                SyntaxHighlightingName = SyntaxHighlighting.CSharp,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = EntitiesPlugInTemplateTag|SolutionPlugInTemplateTag
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
实体项目模板
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\Entity\ProjectInfo.tt
",
                                                Description = 
"
实体项目模板
",
                                                DisplayOrder = 
15,
                                                Encoding = Encoding.Default,
                                                ExportFileNameFormatString = 
"
YbRapidSolution.Entities.csproj
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.Entities\%s
",
                                                SyntaxHighlightingName = SyntaxHighlighting.CSharp,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = SolutionPlugInTemplateTag|IsTableInfoListOfTemplateArg
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
对象页面文件
",
                                                TemplateRelativePath = 
@"
Templates\CastleActiveRecord\WebForm\aspx.tt
",
                                                Description = 
"
对象页面文件
",
                                                DisplayOrder = 
50,
                                                ExportFileNameFormatString = 
"
%sadmin.aspx
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\Admin\%s
",
                                                Encoding = Encoding.UTF8,
                                                SyntaxHighlightingName = SyntaxHighlighting.ASPX,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = UIPlugInTemplateTag|SolutionPlugInTemplateTag
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
对象页面后置文件
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\WebForm\aspx.cs.tt
",
                                                Description = 
"
对象页面后置文件
",
                                                DisplayOrder = 
55,
                                                ExportFileNameFormatString = 
"
%sadmin.aspx.cs
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\Admin\%s
",
                                                Encoding = Encoding.UTF8,
                                                SyntaxHighlightingName = SyntaxHighlighting.CSharp,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = UIPlugInTemplateTag|SolutionPlugInTemplateTag
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
对象页面设计时后置文件
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\WebForm\aspx.designer.cs.tt
",
                                                Description = 
"
对象页面设计时后置文件
",
                                                DisplayOrder = 
65,
                                                ExportFileNameFormatString = 
"
%sadmin.aspx.designer.cs
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\Admin\%s
",
                                                Encoding = Encoding.Default,
                                                SyntaxHighlightingName = SyntaxHighlighting.CSharp,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = UIPlugInTemplateTag|SolutionPlugInTemplateTag
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
对象页面后台起始页
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\WebForm\Default.aspx.tt
",
                                                Description = 
"
对象页面后台起始页
",
                                                DisplayOrder = 
70,
                                                ExportFileNameFormatString = 
"
Default.aspx
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\Admin\Default.aspx
",
                                                Encoding = Encoding.UTF8,
                                                SyntaxHighlightingName = SyntaxHighlighting.ASPX,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = SolutionPlugInTemplateTag|IsTableInfoListOfTemplateArg
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
ASP.NET Global 后置代码文件
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\WebForm\Global.asax.cs.tt
",
                                                Description = 
"
SP.NET Global 后置代码文件
",
                                                DisplayOrder = 
70,
                                                EncodingName = 
"
Default
",
                                                ExportFileNameFormatString = 
"
Global.asax.cs
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\Global.asax.cs
",
                                                Encoding = Encoding.Default,
                                                SyntaxHighlightingName = SyntaxHighlighting.CSharp,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = SolutionPlugInTemplateTag|IsTableInfoListOfTemplateArg
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
WebForm 项目文件
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\WebForm\ProjectInfo.tt
",
                                                Description = 
"
WebForm 项目文件
",
                                                DisplayOrder = 
75,
                                                ExportFileNameFormatString = 
"
YbRapidSolution.WebForms.csproj
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\%s
",
                                                Encoding = Encoding.UTF8,
                                                SyntaxHighlightingName = SyntaxHighlighting.CSharp,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = SolutionPlugInTemplateTag|IsTableInfoListOfTemplateArg
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
Web.config 文件
",
                                                TemplateRelativePath =
                                                    
@"
Templates\CastleActiveRecord\WebForm\Web.config.tt
",
                                                Description = 
"
Web.config 文件
",
                                                DisplayOrder = 
80,
                                                ExportFileNameFormatString = 
"
Web.config
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\Web.config
",
                                                Encoding = Encoding.UTF8,
                                                SyntaxHighlightingName = SyntaxHighlighting.XML,
                                                BuildEnable = 
true,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey,
                                                Tag = SolutionPlugInTemplateTag|IsTableInfoListOfTemplateArg
                                            },
                                        
new TemplateInfo()
                                            {
                                                Name = 
"
权限数据库表初始化脚本文件
",
                                                TemplateRelativePath = 
@"
Templates\ProviderInstallScript.sql
",
                                                Description = 
"
权限数据库表初始化脚本文件
",
                                                DisplayOrder = 
100,
                                                ExportFileNameFormatString = 
@"
ProviderInstallScript.sql
",
                                                ExportRelativePathFormatString =
                                                    
@"
YbRapidSolution\YbRapidSolution.WebForms\Web.config
",
                                                Encoding = Encoding.Default,
                                                SyntaxHighlightingName = SyntaxHighlighting.XML,
                                                BuildEnable = 
false,
                                                PlugInGroupKey = StaticResources.PlugInGroupKey
                                            }
                                    };
            
return templateInfos;
        }

    一个插件组的代码就完成了,下一章节将开始介绍各个子插件的Demo实现。

    有兴趣的朋友可以下载本插件Demo源代码和YbSoftwareFactory应用程序(本站上传文件大小受限,只有临时找个网盘,有需要的可以加QQ)。

    

    

    

 

转载地址:http://qefua.baihongyu.com/

你可能感兴趣的文章
LNMP+Nagios server一键安装脚本
查看>>
Ubuntu默认root用户密码
查看>>
Jfinal和httl整合
查看>>
oracle 多表查询
查看>>
无业务不伸缩之一,云计算有ESS(基础篇)
查看>>
结构体嵌套对齐
查看>>
dns学习笔记01
查看>>
centos DNS 服务器配置
查看>>
jQuery animate方法开发极客标签Logo动画融合效果
查看>>
免费图标:30款彩色圆角图标设计
查看>>
springboot-activiti多数据源配置
查看>>
spring data mongo模糊搜索
查看>>
zuul超时及重试配置
查看>>
tomcat jdbc连接池的suspect、abandon操作解析
查看>>
MySQL登录
查看>>
Scala下Play框架学习笔记(Body parsers)
查看>>
mysql权限管理
查看>>
老虞要学GoLang-为什么会学习GoLang
查看>>
利用estools修改javascript源代码
查看>>
Redis构建分布式锁
查看>>