不一樣的.NET煙火,基于Roslyn的開源代碼生成器
功能介紹
Mud 代碼生成器是一個基于Roslyn的源代碼生成器,專為.NET開發者設計,用于根據實體類自動生成各種相關的代碼,從而顯著提升開發效率。它具有以下核心功能:
- DTO代碼生成 - 根據實體類自動生成數據傳輸對象(DTO)
- VO代碼生成 - 根據實體類自動生成視圖對象(VO)
- 查詢輸入類生成 - 根據實體類自動生成查詢輸入類(QueryInput)
- 創建輸入類生成 - 根據實體類自動生成創建輸入類(CrInput)
- 更新輸入類生成 - 根據實體類自動生成更新輸入類(UpInput)
- 實體映射方法生成 - 自動生成實體與DTO之間的映射方法
通過這些功能,開發者可以專注于業務邏輯的實現,而無需花費大量時間在重復性的代碼編寫上。
代碼生成項目參數配置
在使用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]特性 - 定義實體字段,使用適當的特性進行標注
- 編譯項目,代碼生成器將自動生成相關代碼
- 在業務代碼中使用生成的DTO、VO等類
通過以上步驟,您可以輕松地使用Mud代碼生成器來提升開發效率,減少重復勞動,讓團隊更專注于業務邏輯的實現。

浙公網安備 33010602011771號