环境支持及其他接口
一、环境支持类
在扩展文件里可以使用 IDevHosting
接口来提供一些额外的操作。
成员 | 说明 |
---|---|
ConsoleInfo 方法 | 在【输出窗口】中输出一般信息。 |
ConsoleError 方法 | 在【输出窗口】中输出错误信息。 |
ShowInfo 方法 | 弹出信息提示框。 |
ShowError 方法 | 弹出错误提示框。 |
GetTables 方法 | 获取需要生成的表集合。返回 IEnumerable<Table> 类型。 |
Template 属性 | 获取当前使用的模板定义,此属性是 TemplateDefinition 类型,详见 。 |
Profile 属性 | 获取当前的变量信息。 |
IDevHosting
一般用在变量初始化器和架构初始化器中,只需要定义一个 IDevHosting
的属性即可。如下所示:
[SchemaInitializer(typeof(Table))]
public class ClassNameInitializer : ISchemaInitializer
{
public IDevHosting Hosting { get; set; }
public void Initialize(dynamic profile, dynamic schema)
{
var table = schema as Table;
var tableName = table.Name;
//略去处理
Hosting.ConsoleInfo(tableName);
Hosting.ConsoleError(table.ClassName);
}
}
在读入数据源后,会在【输出窗口】输出如下信息:
二、部件输出解析器
生成代码后,你可以对输出的路径进行调整,此时你需要定义一个 IPartitionOutputParser
接口的实现类,可放在 extensions\profile
和 extensions\schema
目录中的任意一个类文件内。此接口的定义如下:
namespace CodeBuilder.Core.Template
{
/// <summary>
/// 部件输出路径的解析器。
/// </summary>
public interface IPartitionOutputParser
{
void Parse(OutputParseContext context);
}
比如 Fireasy for Mvc+Service 完整项目(.net core3.1)
模板就定义了一个解析类,它的作用是当没有指定 Module
变量值时,把输出路径中的 Areas
去掉。
//当没有指定 Module 时,将文件路径中的Areas移走
public class AreaOutputProcessor : IPartitionOutputParser
{
public void Parse(OutputParseContext context)
{
if (string.IsNullOrEmpty(context.Profile.Module) && context.Result.IndexOf("Areas\\") != -1)
{
context.Result = context.Result.Replace("Areas\\", string.Empty);
}
}
}
OutputParseContext
类包含以下几个属性:
名称 | 说明 |
---|---|
Schema | 根据部件所定义 Loop 不同,可以是架构中的任意一种类型。dynamic 类型。 |
Profile | 变量对象。dynamic 类型。 |
Result | 文件输出的路径。 |