Qx.Tools.DTOGenerator 1.0.0

dotnet tool install --global Qx.Tools.DTOGenerator --version 1.0.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local Qx.Tools.DTOGenerator --version 1.0.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Qx.Tools.DTOGenerator&version=1.0.0
                    
nuke :add-package Qx.Tools.DTOGenerator --version 1.0.0
                    

DTOGenerator - Entity到DTO转换工具

这是一个.NET工具,用于将Entity类自动转换为各种类型的DTO(数据传输对象)。

功能特点

  • 自动解析Entity类文件,提取属性和特性
  • 支持生成多种类型的DTO:
    • 创建DTO (CreateDto)
    • 修改DTO (UpdateDto)
    • 列表DTO (ListDto)
    • 详情DTO (DetailDto)
    • 搜索DTO (SearchDto)
  • 保留Entity类中属性的特性(对于创建DTO和修改DTO)
  • 根据DTO类型智能筛选属性
  • 交互式属性选择:通过表格界面选择哪些属性应包含在哪些DTO中
  • 配置文件支持:保存属性选择到配置文件,并通过配置文件生成DTO

安装

dotnet tool install --global DTOGenerator

使用方法

dtogen --file <Entity类文件路径> --output <输出目录> [选项]

选项

  • --file: Entity类文件路径(必需)
  • --output: 输出DTO文件的目录路径(必需)
  • --config: 配置文件路径,用于指定属性选择
  • --create: 生成创建DTO
  • --update: 生成修改DTO
  • --list: 生成列表DTO
  • --detail: 生成详情DTO
  • --search: 生成搜索DTO
  • --all: 生成所有类型的DTO

如果不指定任何DTO类型选项,默认会生成所有类型的DTO。

如果同时提供了配置文件和DTO类型选项,工具会根据配置文件中的属性选择生成指定类型的DTO。

交互式属性选择

运行工具时,会询问是否要使用交互式界面选择属性。如果选择"Y",将显示一个表格界面:

  • 表格横向显示不同的DTO类型(创建、修改、列表、详情、搜索)
  • 表格纵向显示Entity的所有属性
  • 使用方向键移动选择,空格键切换选择状态,Enter键确认,Esc键退出
  • 初始状态下,会根据默认规则预选属性

这样,您可以精确控制每个属性在哪些DTO中出现。

完成属性选择后,工具会询问是否要保存当前配置到文件。如果选择"Y",可以指定配置文件的保存路径,默认保存到实体类所在目录,文件名为[实体类名].dtocfg

配置文件

配置文件使用JSON格式,包含实体类名称、命名空间和每个属性在不同DTO类型中的包含状态。您可以:

  • 通过交互式界面生成配置文件
  • 手动编辑配置文件
  • 使用配置文件自动生成DTO,无需每次都重新选择属性

配置文件示例:

{
  "EntityClassName": "User",
  "EntityNamespace": "Example.Models",
  "PropertySelections": [
    {
      "PropertyName": "Id",
      "PropertyType": "int",
      "IncludeInCreateDto": false,
      "IncludeInUpdateDto": true,
      "IncludeInListDto": true,
      "IncludeInDetailDto": true,
      "IncludeInSearchDto": true
    },
    {
      "PropertyName": "Username",
      "PropertyType": "string",
      "IncludeInCreateDto": true,
      "IncludeInUpdateDto": true,
      "IncludeInListDto": true,
      "IncludeInDetailDto": true,
      "IncludeInSearchDto": true
    }
  ]
}

示例

生成所有类型的DTO

dtogen --file C:\Projects\MyApp\Models\User.cs --output C:\Projects\MyApp\Dtos

只生成创建和修改DTO

dtogen --file C:\Projects\MyApp\Models\User.cs --output C:\Projects\MyApp\Dtos --create --update

使用配置文件生成DTO

dtogen --file C:\Projects\MyApp\Models\User.cs --output C:\Projects\MyApp\Dtos --config C:\Projects\MyApp\User.dtocfg --all

开发

构建项目

dotnet build

打包为工具

dotnet pack

本地安装工具(用于测试)

dotnet tool install --global --add-source ./nupkg DTOGenerator

注意事项

  • 支持解析多层基类的属性
  • 对于复杂的特性和泛型类型可能需要手动调整生成的代码
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
1.0.0 163 8/1/2025