news 2026/8/7 22:15:03

不一样的.NET烟火,基于Roslyn的开源代码生成器

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
不一样的.NET烟火,基于Roslyn的开源代码生成器

代码生成项目参数配置

在使用Mud代码生成器时,可以通过在项目文件中配置以下参数来自定义生成行为:

开源项目

Mud-Code-Generator 源代码

Mud-Code-Generator 帮助文档

通用配置参数

<PropertyGroup>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <!-- 在obj目录下保存生成的代码 -->

<EntitySuffix>Entity</EntitySuffix> <!-- 实体类后缀配置 -->

<EntityAttachAttributes>SuppressSniffer</EntityAttachAttributes> <!-- 实体类加上Attribute特性配置,多个特性时使用','分隔 -->

</PropertyGroup>

<ItemGroup>

<CompilerVisibleProperty Include="EntitySuffix" />

<CompilerVisibleProperty Include="EntityAttachAttributes" />

</ItemGroup>

依赖项配置

<ItemGroup>

<!-- 引入的代码生成器程序集,注意后面的参数 -->

<PackageReference Include="Mud.EntityCodeGenerator" Version="1.1.5" PrivateAssets="all" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>

</ItemGroup>

配置参数说明

参数名 默认值 说明

EmitCompilerGeneratedFiles false 是否在obj目录下保存生成的代码,设为true便于调试

EntitySuffix Entity 实体类后缀,用于识别实体类

EntityAttachAttributes (空) 实体类上需要附加的特性,多个特性用逗号分隔

代码生成功能及样例

DTO/VO/输入类代码生成

在实体程序项目中添加生成器及配置相关参数:

<PropertyGroup>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>

<EntitySuffix>Entity</EntitySuffix>

<EntityAttachAttributes>SuppressSniffer</EntityAttachAttributes>

</PropertyGroup>

<ItemGroup>

<CompilerVisibleProperty Include="EntitySuffix" />

<CompilerVisibleProperty Include="EntityAttachAttributes"/>

</ItemGroup>

在实体中添加DtoGenerator特性:

/// <summary>

/// 客户端信息实体类

/// </summary>

[DtoGenerator]

[Table(Name = "sys_client"),SuppressSniffer]

public partial class SysClientEntity

{

/// <summary>

/// id

/// </summary>

[property: TableField(Fille = FieldFill.Insert, Value = FillValue.Id)]

[property: Column(Name = "id", IsPrimary = true, Position = 1)]

[property: Required(ErrorMessage = "id不能为空")]

private long? _id;

/// <summary>

/// 客户端key

/// </summary>

[property: Column(Name = "client_key", Position = 3)]

[property: Required(ErrorMessage = "客户端key不能为空")]

[property: ExportProperty("客户端key")]

private string _clientKey;

/// <summary>

/// 删除标志(0代表存在 2代表删除)

/// </summary>

[property: Column(Name = "del_flag", Position = 10)]

[property: ExportProperty("删除标志")]

[IgnoreQuery]

private string _delFlag;

}

基于以上实体,将自动生成以下几类代码:

实体类属性

/// <summary>

/// 客户端信息实体类

/// </summary>

public partial class SysClientEntity

{

/// <summary>

/// id

/// </summary>

[TableField(Fille = FieldFill.Insert, Value = FillValue.Id), Column(Name = "id", IsPrimary = true, Position = 1)]

public long? Id

{

get

{

return _id;

}

set

{

_id = value;

}

}

/// <summary>

/// 客户端key

/// </summary>

[Column(Name = "client_key", Position = 3)]

public string? ClientKey

{

get

{

return _clientKey;

}

set

{

_clientKey = value;

}

}

/// <summary>

/// 删除标志(0代表存在 2代表删除)

/// </summary>

[Column(Name = "del_flag", Position = 10)]

public string? DelFlag

{

get

{

return _delFlag;

}

set

{

_delFlag = value;

}

}

/// <summary>

/// 通用的实体映射至VO对象方法。

/// </summary>

public virtual SysClientListOutput MapTo()

{

var voObj = new SysClientListOutput();

voObj.id = this.Id;

voObj.clientKey = this.ClientKey;

voObj.delFlag = this.DelFlag;

return voObj;

}

}

VO类 (视图对象)

/// <summary>

/// 客户端信息实体类

/// </summary>

[SuppressSniffer, CompilerGenerated]

public partial class SysClientListOutput

{

/// <summary>

/// id

/// </summary>

public long? id { get; set; }

/// <summary>

/// 客户端key

/// </summary>

[ExportProperty("客户端key")]

public string? clientKey { get; set; }

/// <summary>

/// 删除标志(0代表存在 2代表删除)

/// </summary>

[ExportProperty("删除标志")]

public string? delFlag { get; set; }

}

QueryInput类 (查询输入对象)

// SysClientQueryInput.g.cs

/// <summary>

/// 客户端信息实体类

/// </summary>

[SuppressSniffer, CompilerGenerated]

public partial class SysClientQueryInput : DataQueryInput

{

/// <summary>

/// id

/// </summary>

public long? id { get; set; }

/// <summary>

/// 客户端key

/// </summary>

public string? clientKey { get; set; }

/// <summary>

/// 删除标志(0代表存在 2代表删除)

/// </summary>

public string? delFlag { get; set; }

/// <summary>

/// 构建通用的查询条件。

/// </summary>

public Expression<Func<SysClientEntity, bool>> BuildQueryWhere()

{

var where = LinqExtensions.True<SysClientEntity>();

where = where.AndIF(this.id != null, x => x.Id == this.id);

where = where.AndIF(!string.IsNullOrEmpty(this.clientKey), x => x.ClientKey == this.clientKey);

where = where.AndIF(!string.IsNullOrEmpty(this.delFlag), x => x.DelFlag == this.delFlag);

return where;

}

}

CrInput类 (创建输入对象)

// SysClientCrInput.g.cs

/// <summary>

/// 客户端信息实体类

/// </summary>

[SuppressSniffer, CompilerGenerated]

public partial class SysClientCrInput

{

/// <summary>

/// 客户端key

/// </summary>

[Required(ErrorMessage = "客户端key不能为空")]

public string? clientKey { get; set; }

/// <summary>

/// 删除标志(0代表存在 2代表删除)

/// </summary>

public string? delFlag { get; set; }

/// <summary>

/// 通用的BO对象映射至实体方法。

/// </summary>

public virtual SysClientEntity MapTo()

{

var entity = new SysClientEntity();

entity.ClientKey = this.clientKey;

entity.DelFlag = this.delFlag;

return entity;

}

}

UpInput类 (更新输入对象)

/// <summary>

/// 客户端信息实体类

/// </summary>

[SuppressSniffer, CompilerGenerated]

public partial class SysClientUpInput : SysClientCrInput

{

/// <summary>

/// id

/// </summary>

[Required(ErrorMessage = "id不能为空")]

public long? id { get; set; }

/// <summary>

/// 通用的BO对象映射至实体方法。

/// </summary>

public override SysClientEntity MapTo()

{

var entity = base.MapTo();

entity.Id = this.id;

return entity;

}

}

特性控制参数

DtoGenerator特性支持以下参数控制代码生成行为:

参数名 类型 默认值 说明

GenMapMethod bool true 是否生成实体映射方法

GenVoClass bool true 是否生成VO类

GenQueryInputClass bool true 是否生成查询输入类

GenBoClass bool true 是否生成BO类

DtoNamespace string "Dto" DTO类命名空间

使用示例:

[DtoGenerator(

GenMapMethod = true,

GenVoClass = true,

GenQueryInputClass = false,

DtoNamespace = "ViewModels"

)]

public class SysClientEntity : BaseEntity

{

// 属性定义

}

与传统代码生成器的比较

相较于传统的代码生成器(如CodeSmith)和低代码平台的代码生成器,Mud 代码生成器有着独特的优势:

零散添加字段不需要整体重新生成实体

传统的代码生成器通常需要在模型变更时重新生成整个文件,这可能导致已有的自定义代码丢失或者需要手动合并。而Mud代码生成器采用增量式生成方式,在添加新字段时只需重新编译项目即可自动更新相关代码,无需重新生成整个实体。

零散添加字段不需要手动添加至其它DTO

当实体新增字段时,传统代码生成器往往需要手动将新字段添加到各个相关的DTO中,容易遗漏且繁琐。Mud 代码生成器会在编译时自动检测实体变化并同步更新所有相关的DTO、VO以及各种输入类,保证代码的一致性。

代码整洁,关注核心字段

Mud 代码生成器遵循"关注点分离"原则,将生成的代码与手写的业务逻辑完全隔离。开发者只需要关注核心业务字段的定义,其他辅助代码会自动生成,使代码更加整洁易维护。

实时生成

代码在编译时自动生成,无需额外的操作步骤。开发者只需关注业务逻辑代码的编写,当修改实体类并重新编译时,所有相关的DTO、VO和输入类都会自动更新,大大提升了开发效率。

强类型安全

基于Roslyn编译器平台,提供强类型的代码生成和验证。生成的代码与项目中的其他代码一样,都经过编译器的严格检查,避免了运行时错误,提高了代码质量和可靠性.

高度可定制

支持多种配置选项,可以根据项目需求灵活调整生成规则。开发者可以通过项目配置文件控制生成行为,如实体类后缀、需要附加的特性等,满足不同项目的个性化需求。

无缝集成

作为.NET项目的一部分,与现有开发流程完美融合。无需额外的工具或复杂的配置,只需添加NuGet包引用并在项目中进行简单配置,即可享受代码自动生成带来的便利。

版本控制友好

生成的代码不会污染版本历史,便于团队协作。由于代码是在编译时生成的,不会产生大量人工编写的重复代码,使得版本控制系统中的变更记录更加清晰,更容易进行代码审查和团队协作。

使用方法

在您的项目中添加对 Mud.EntityCodeGenerator 包的引用

根据需要配置项目参数,如实体后缀、特性等

将实体类标记为 partial 并添加 [DtoGenerator] 特性

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/8 0:31:54

基于SpringAI构建大模型应用

1. 背景在这里&#xff0c;我主要分享的是在应用层面大模型相关的技术&#xff0c;假如你已有一个现成的大模型接口&#xff0c;无论是符合OpenAI规范的&#xff0c;还是各家公司一些自己的接口&#xff0c;例如Gemini&#xff0c;Deepseek&#xff0c;通义千问&#xff0c;问心…

作者头像 李华
网站建设 2026/8/7 18:50:22

黑锋科技(HeifengTech)过压过流保护开关芯片全系列技术解析

一、芯片系列核心定位本系列是黑锋科技面向现代便携设备电源前端保护而打造的高集成度解决方案矩阵 其核心价值在于为智能手机、平板电脑、TWS耳机、导航设备等产品&#xff0c;在充电端口或系统电源入口处&#xff0c;构建一道抵御异常高压、浪涌电流、短路及热插拔应力的坚固…

作者头像 李华
网站建设 2026/8/7 12:44:17

DVWA -SQL Injection-通关教程-完结

DVWA -SQL Injection-通关教程-完结 SQL注入是一种攻击者通过在应用程序的输入字段中插入恶意SQL代码&#xff0c;从而操纵后端数据库查询的攻击技术。如果应用程序未对用户输入进行适当过滤&#xff0c;这些恶意SQL代码会被数据库执行&#xff0c;导致数据泄露、篡改或破坏。 …

作者头像 李华
网站建设 2026/8/8 5:17:29

0x3f第七天 二叉搜索树

1.前序遍历&#xff1a;4213657先验证根是否满足大于左子树最大值&#xff0c;小于右子树最小值遍历左子树&#xff08;更新右值&#xff09;遍历右子树&#xff08;更新左值&#xff09;对于最大值和最小值&#xff0c;可以直接在函数里加上def isValidBST(self, root: Option…

作者头像 李华
网站建设 2026/8/7 18:08:36

扩容U盘,资料毁灭盘

早就听过扩容盘了&#xff0c;没想到也发生在我身上。原本上面是印着长城的 &#xff0c;但现在掉漆了。店铺现在这个商品下架了&#xff0c;但店铺是有品牌认证的这个U盘也算是用了将近4年了&#xff0c;存的都是大学的上课的作业。大四没课回家了几个月没有使用过这个U盘&…

作者头像 李华