Mud.EntityCodeGenerator 1.2.3

dotnet add package Mud.EntityCodeGenerator --version 1.2.3
                    
NuGet\Install-Package Mud.EntityCodeGenerator -Version 1.2.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mud.EntityCodeGenerator" Version="1.2.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mud.EntityCodeGenerator" Version="1.2.3" />
                    
Directory.Packages.props
<PackageReference Include="Mud.EntityCodeGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mud.EntityCodeGenerator --version 1.2.3
                    
#r "nuget: Mud.EntityCodeGenerator, 1.2.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mud.EntityCodeGenerator@1.2.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mud.EntityCodeGenerator&version=1.2.3
                    
Install as a Cake Addin
#tool nuget:?package=Mud.EntityCodeGenerator&version=1.2.3
                    
Install as a Cake Tool

Mud 实体代码生成器

功能介绍

Mud 实体代码生成器是一个基于 Roslyn 的源代码生成器,用于根据实体类自动生成各种相关代码,提高开发效率。它包含以下主要功能:

  1. DTO代码生成 - 根据实体类自动生成数据传输对象(DTO)
  2. VO代码生成 - 根据实体类自动生成视图对象(VO)
  3. 查询输入类生成 - 根据实体类自动生成查询输入类(QueryInput)
  4. 创建输入类生成 - 根据实体类自动生成创建输入类(CrInput)
  5. 更新输入类生成 - 根据实体类自动生成更新输入类(UpInput)
  6. 实体映射方法生成 - 自动生成实体与DTO之间的映射方法
  7. Builder模式代码生成 - 自动生成实体的Builder构建器模式代码
  8. 实体扩展方法生成 - 自动生成实体与其他类型之间的映射扩展方法

项目参数配置

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

通用配置参数

<PropertyGroup>
  <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>  
  <EntitySuffix>Entity</EntitySuffix>  
  <EntityAttachAttributes>SuppressSniffer</EntityAttachAttributes>  
  <PropertyNameLowerCaseFirstLetter>true</PropertyNameLowerCaseFirstLetter>  
  <UsingNameSpaces>FreeSql.DataAnnotations,CodeGeneratorTest.Entites,CodeGeneratorTest.Entites.Dto</UsingNameSpaces>  
</PropertyGroup>

<ItemGroup>
  <CompilerVisibleProperty Include="EntitySuffix" />
  <CompilerVisibleProperty Include="EntityAttachAttributes" />
  <CompilerVisibleProperty Include="PropertyNameLowerCaseFirstLetter" />
  <CompilerVisibleProperty Include="UsingNameSpaces" />
</ItemGroup>

VO/BO 属性配置参数

现在支持为 VO 和 BO 类的属性分别配置额外的特性,为 VO 和 BO 类的属性生成的的特性提供更精细的控制:

<PropertyGroup>
  
  <VoAttributes>CustomVo1Attribute,CustomVo2Attribute</VoAttributes>
  
  <BoAttributes>CustomBo1Attribute,CustomBo2Attribute</BoAttributes>
</PropertyGroup>

<ItemGroup>
  <CompilerVisibleProperty Include="VoAttributes" />
  <CompilerVisibleProperty Include="BoAttributes" />
</ItemGroup>

后缀名配置参数

现在支持为生成的各类代码配置自定义后缀名,提供更灵活的命名控制:

<PropertyGroup>
  
  <VoSuffix>MyVo</VoSuffix>
  
  <InfoOutputSuffix>MyInfo</InfoOutputSuffix>
  
  <BoSuffix>MyBo</BoSuffix>
  
  <QueryInputSuffix>MyQuery</QueryInputSuffix>
  
  <CrInputSuffix>MyCreate</CrInputSuffix>
  
  <UpInputSuffix>MyUpdate</UpInputSuffix>
</PropertyGroup>

<ItemGroup>
  <CompilerVisibleProperty Include="VoSuffix" />
  <CompilerVisibleProperty Include="InfoOutputSuffix" />
  <CompilerVisibleProperty Include="BoSuffix" />
  <CompilerVisibleProperty Include="QueryInputSuffix" />
  <CompilerVisibleProperty Include="CrInputSuffix" />
  <CompilerVisibleProperty Include="UpInputSuffix" />
</ItemGroup>

依赖项配置

<ItemGroup>
  
  <PackageReference Include="Mud.EntityCodeGenerator" Version="1.2.0" />
</ItemGroup>

配置参数说明

参数名 默认值 说明
EmitCompilerGeneratedFiles false 是否在obj目录下保存生成的代码,设为true便于调试
EntitySuffix Entity 实体类后缀,用于识别实体类
EntityAttachAttributes (空) 生成的VO、BO类加上Attribute特性配置,多个特性时使用','分隔
VoAttributes (空) 需要添加至VO类的自定义特性,多个特性用逗号分隔
BoAttributes (空) 需要添加至BO类的自定义特性,多个特性用逗号分隔
PropertyNameLowerCaseFirstLetter true 是否将生成的属性名首字母小写,设为false保持原有大小写
UsingNameSpaces (空) 需要添加至DTO实体中引用的命名空间,多个命名空间用逗号分隔
VoSuffix Vo VO类后缀配置
InfoOutputSuffix InfoOutput InfoOutput类后缀配置
BoSuffix Bo BO类后缀配置
QueryInputSuffix QueryInput QueryInput类后缀配置
CrInputSuffix CrInput CrInput类后缀配置
UpInputSuffix UpInput UpInput类后缀配置

代码生成功能及样例

1. 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")]
    [property: CustomVo1, CustomVo2]
    [property: CustomBo1, CustomBo2]
    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")]
    [CustomVo1, CustomVo2]
    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不能为空"), CustomBo1, CustomBo2]
    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;
    }
}

2. Builder模式代码生成

除了上述代码生成外,Mud.EntityCodeGenerator还支持Builder构建器模式代码生成。只需在实体类上添加[Builder]特性:

/// <summary>
/// 客户端信息实体类
/// </summary>
[DtoGenerator]
[Builder]
[Table(Name = "sys_client"),SuppressSniffer]
public partial class SysClientEntity
{
    /// <summary>
    /// id
    /// </summary>
    [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不能为空")]
    private string _clientKey;

    /// <summary>
    /// 删除标志(0代表存在 2代表删除)
    /// </summary>
    [property: Column(Name = "del_flag", Position = 10)]
    private string _delFlag;
}

基于以上实体,将自动生成Builder构建器类:

/// <summary>
/// <see cref="SysClientEntity"/> 的构建者。
/// </summary>
public class SysClientEntityBuilder
{
    private SysClientEntity _sysClientEntity = new SysClientEntity();

    /// <summary>
    /// 设置 <see cref="SysClientEntity.Id"/> 属性值。
    /// </summary>
    /// <param name="id">属性值</param>
    /// <returns>返回 <see cref="SysClientEntityBuilder"/> 实例</returns>
    public SysClientEntityBuilder SetId(long? id)
    {
        this._sysClientEntity.Id = id;
        return this;
    }

    /// <summary>
    /// 设置 <see cref="SysClientEntity.ClientKey"/> 属性值。
    /// </summary>
    /// <param name="clientKey">属性值</param>
    /// <returns>返回 <see cref="SysClientEntityBuilder"/> 实例</returns>
    public SysClientEntityBuilder SetClientKey(string clientKey)
    {
        this._sysClientEntity.ClientKey = clientKey;
        return this;
    }

    /// <summary>
    /// 设置 <see cref="SysClientEntity.DelFlag"/> 属性值。
    /// </summary>
    /// <param name="delFlag">属性值</param>
    /// <returns>返回 <see cref="SysClientEntityBuilder"/> 实例</returns>
    public SysClientEntityBuilder SetDelFlag(string delFlag)
    {
        this._sysClientEntity.DelFlag = delFlag;
        return this;
    }

    /// <summary>
    /// 构建 <see cref="SysClientEntity"/> 类的实例。
    /// </summary>
    public SysClientEntity Build()
    {
        return this._sysClientEntity;
    }
}

使用Builder模式可以链式设置实体属性,创建实体对象更加方便:

var client = new SysClientEntityBuilder()
    .SetClientKey("client123")
    .SetDelFlag("0")
    .Build();

3. 实体扩展方法生成

Mud.EntityCodeGenerator还支持生成实体类的扩展方法,用于在实体和各种DTO之间进行映射。这些扩展方法包括:

  1. MapToEntity(this CrInput input) - 将创建输入对象映射到实体
  2. MapToEntity(this UpInput input) - 将更新输入对象映射到实体
  3. MapToCrInput(this Entity entity) - 将实体映射到创建输入对象
  4. MapToUpInput(this Entity entity) - 将实体映射到更新输入对象
  5. MapToListOutput(this Entity entity) - 将实体映射到列表输出对象(VO)
  6. MapToList(this IEnumerable<Entity> entities) - 将实体集合映射到列表输出对象集合
  7. BuildQueryWhere(this QueryInput input) - 根据查询输入对象构建查询条件表达式

使用示例:

// 将CrInput映射到实体
var entity = crInput.MapToEntity();

// 将实体映射到UpInput
var upInput = entity.MapToUpInput();

// 将实体映射到VO
var vo = entity.MapToListOutput();

// 将实体集合映射到VO集合
var voList = entityList.MapToList();

// 构建查询条件
var query = queryInput.BuildQueryWhere();

4. 代码生成器增强功能

TransitiveBoGenerator 增强
  • 支持通过 ExtraBoPropertyAttributes 单独配置BO类属性特性
  • BO类属性默认包含以下验证特性: Required, Xss, StringLength, MaxLength, MinLength, EmailAddress, DataValidation, RegularExpression
  • 支持通过 BoAttributes 配置额外特性
  • 自动生成的BO类包含 MapTo() 方法,用于将BO对象映射到实体对象
TransitiveQueryInputGenerator 增强
  • 支持通过 ExtraVoPropertyAttributes 单独配置VO类属性特性
  • 自动生成 BuildQueryWhere() 方法,用于构建查询条件
  • 支持 LikeQueryAttribute 特性,用于生成包含查询条件
  • 支持 OrderByAttribute 特性,用于生成排序条件
EntityMethodGenerator 增强
  • 自动生成实体类属性(基于私有字段)
  • 自动生成 MapTo() 方法,用于将实体对象映射到VO对象
  • 支持通过 ExtraPropertyAttributes 配置实体属性特性

5. 特性控制参数

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
{
    // 属性定义
}

维护者

倔强的泥巴

许可证

本项目采用MIT许可证模式:

免责声明

本项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。

不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.3 101 11/6/2025
1.2.2 116 11/4/2025
1.2.1 165 10/31/2025
1.2.0 167 10/28/2025
1.1.9 167 10/15/2025
1.1.8 158 10/9/2025
1.1.7 97 10/4/2025
1.1.6 172 9/30/2025
1.1.5 180 9/29/2025
1.1.4 149 7/11/2025
1.1.3 248 12/31/2024 1.1.3 is deprecated because it is no longer maintained and has critical bugs.
1.1.2 230 12/30/2024 1.1.2 is deprecated because it is no longer maintained and has critical bugs.
1.1.1 223 12/30/2024 1.1.1 is deprecated because it is no longer maintained and has critical bugs.
1.1.0 223 12/29/2024 1.1.0 is deprecated because it is no longer maintained and has critical bugs.
1.0.9.4 224 12/28/2024 1.0.9.4 is deprecated because it is no longer maintained and has critical bugs.
1.0.9.3 231 12/28/2024 1.0.9.3 is deprecated because it is no longer maintained and has critical bugs.
1.0.9.1 228 12/26/2024 1.0.9.1 is deprecated because it is no longer maintained and has critical bugs.
1.0.9 231 12/26/2024 1.0.9 is deprecated because it is no longer maintained and has critical bugs.
1.0.8 234 12/25/2024 1.0.8 is deprecated because it is no longer maintained and has critical bugs.
1.0.7 230 12/25/2024 1.0.7 is deprecated because it is no longer maintained and has critical bugs.
1.0.6 231 12/25/2024 1.0.6 is deprecated because it is no longer maintained and has critical bugs.
1.0.5 238 12/25/2024 1.0.5 is deprecated because it is no longer maintained and has critical bugs.
1.0.4 227 12/25/2024 1.0.4 is deprecated because it is no longer maintained and has critical bugs.
1.0.3 240 12/25/2024 1.0.3 is deprecated because it is no longer maintained and has critical bugs.
1.0.2 237 12/25/2024 1.0.2 is deprecated because it is no longer maintained and has critical bugs.
1.0.1 232 12/25/2024 1.0.1 is deprecated because it is no longer maintained and has critical bugs.
0.3.1 232 12/26/2024 0.3.1 is deprecated because it is no longer maintained and has critical bugs.