Hand.GeneratePoco
0.2.1.14-alpha
This is a prerelease version of Hand.GeneratePoco.
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Hand.GeneratePoco --version 0.2.1.14-alpha
NuGet\Install-Package Hand.GeneratePoco -Version 0.2.1.14-alpha
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="Hand.GeneratePoco" Version="0.2.1.14-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hand.GeneratePoco" Version="0.2.1.14-alpha" />
<PackageReference Include="Hand.GeneratePoco" />
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 Hand.GeneratePoco --version 0.2.1.14-alpha
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hand.GeneratePoco, 0.2.1.14-alpha"
#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 Hand.GeneratePoco@0.2.1.14-alpha
#: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=Hand.GeneratePoco&version=0.2.1.14-alpha&prerelease
#tool nuget:?package=Hand.GeneratePoco&version=0.2.1.14-alpha&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
生成类型成员及转化器
1 GeneratePoco生成构造函数
1 生成构造函数默认样式
1.1 配置代码
- Initializer配置为InitializeKind.Constructor
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Constructor)]
public partial class UserDto;
1.2 生成代码
partial class UserDto(int id, string name)
{
public int Id { get; } = id;
public string Name { get; } = name;
public User ToUser() => new(Id, Name);
}
2 生成构造函数含字段样式
2.1 配置代码
- Initializer配置为InitializeKind.Constructor | InitializeKind.Field
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Constructor | InitializeKind.Field)]
public partial class UserDto;
2.2 生成代码
partial class UserDto(int id, string name)
{
private readonly int _id = id;
private readonly string _name = name;
public int Id => _id;
public string Name => _name;
public User ToUser() => new(_id, _name);
}
3 生成构造函数设置属性样式
3.1 配置代码
- Initializer配置为InitializeKind.Constructor | InitializeKind.Property
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Constructor | InitializeKind.Property)]
public partial class UserDto;
3.2 生成代码
partial class UserDto(int id, string name)
{
public int Id { get; set; } = id;
public string Name { get; set; } = name;
public User ToUser() => new(Id, Name);
}
4 生成构造函数设置字段及属性样式
4.1 配置代码
- Initializer配置为InitializeKind.Constructor | InitializeKind.Property | InitializeKind.Field
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Constructor | InitializeKind.Property | InitializeKind.Field)]
public partial class UserDto;
4.2 生成代码
partial class UserDto(int id, string name)
{
private int _id = id;
private string _name = name;
public int Id { get => _id; set => _id = value; }
public string Name { get => _name; set => _name = value; }
public GeneratePocoTests.User ToUser() => new(_id, _name);
}
5 生成属性默认样式
5.1 配置代码
- Initializer配置为InitializeKind.Constructor
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Property)]
public partial class UserDto;
5.2 生成代码
partial class UserDto
{
public int Id { get; set; }
public string Name { get; set; }
public User ToUser() => new(Id, Name);
}
6 生成属性Init样式
6.1 配置代码
- Initializer配置为InitializeKind.Init
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Init)]
public partial class UserDto;
6.2 生成代码
partial class UserDto
{
public int Id { get; init; }
public string Name { get; init; }
public User ToUser() => new(Id, Name);
}
7 生成属性含字段样式
7.1 配置代码
- Initializer配置为InitializeKind.Property | InitializeKind.Field
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Property | InitializeKind.Field)]
public partial class UserDto;
7.2 生成代码
partial class UserDto
{
private int _id;
private string _name;
public int Id { get => _id; set => _id = value; }
public string Name { get => _name; set => _name = value; }
public GeneratePocoTests.User ToUser() => new(_id, _name);
}
8 生成属性Init含字段样式
8.1 配置代码
- Initializer配置为InitializeKind.Init | InitializeKind.Field
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Init | InitializeKind.Field)]
public partial class UserDto;
8.2 生成代码
partial class UserDto
{
private int _id;
private string _name;
public int Id { get => _id; init => _id = value; }
public string Name { get => _name; init => _name = value; }
public User ToUser() => new(_id, _name);
}
9 生成字段样式
9.1 配置代码
- Initializer配置为InitializeKind.Field
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Field)]
public partial class UserDto;
9.2 生成代码
partial class UserDto
{
public int Id;
public string Name;
public User ToUser() => new(Id, Name);
}
10 生成记录样式
10.1 配置代码
- 需要目标类型为record(或struct record),Initializer不配置
public record User(int Id, string Name);
[GeneratePoco<User>()]
public partial record UserDto;
10.2 生成代码
partial record UserDto(int Id, string Name)
{
public User ToUser() => new(Id, Name);
}
11 GeneratePoco其他配置属性
11.1 ConvertFrom/ConvertTo配置
- ConvertFrom默认false,为true时生成从源类型到目标类型的静态扩展转换方法
- ConvertTo默认true,为true时生成从目标类型到源类型的实例转换方法
- 前面已有生成两种方法的示例,这里不再赘述
11.2 Rules配置
- 从源类型到目标类型的属性映射规则
- 可以配置Include、Exclude等规则
- 参看SourceGenerator之扑风捉影
11.3 NullableRule配置
- NullableRule配置哪些属性(及其相关的字段和参数)为可空类型
- 参看SourceGenerator之扑风捉影
11.4 GenerateAttribute配置
- GenerateAttribute默认false,为true时源类型属性的特性会被复制到目标类型属性(及其相关的字段和参数)上
- 会判断源Attribute类型的AttributeUsage,是否适合应用到目标类型属性(及其相关的字段和参数)上,如果不适合则不会复制
// 配置代码
public class Product(int productId, string productName)
{
public int ProductId { get; } = productId;
[StringLength(100, MinimumLength = 6)]
public string ProductName { get; } = productName;
}
[GeneratePoco<Product>(GenerateAttribute = true)]
public partial class ProductDto;
// 生成代码
partial class ProductDto
{
public int ProductId { get; set; }
[StringLength(100, MinimumLength = 6)]
public string ProductName { get; set; }
public GeneratePocoTests.Supports.Product ToProduct() => new(ProductId, ProductName);
}
11.5 Default配置
- Default默认false,为true时生成默认默认值
- 同一个属性及其相关的字段和参数之一生成默认值
- 生成默认值的优先级是参数>字段>属性
// 配置代码
public record User(int Id, string Name);
[GeneratePoco<User>(Initializer = InitializeKind.Property | InitializeKind.Field, Default = true)]
public partial class UserDto;
// 生成代码
partial class UserDto
{
private int _id = default;
private string _name = "";
public int Id { get => _id; set => _id = value; }
public string Name { get => _name; set => _name = value; }
public User ToUser() => new(_id, _name);
}
12 GeneratePoco不只是生成DTO
- GeneratePoco实际上是生成十种不同的POCO类
- 这十种样式并不全适合DTO
- 也可以称之为影子类
3.2 GenerateTable生成器
- GenerateTable生成器按实体类型生成表结构类
- 生成的表结构类可以用于生成数据库表,也可以用于对数据表进行增删改查等操作
- 一年前网友秦时明留言建议做源生成器,现在才补上会不会有点亡羊补牢的感觉
- 可以参看ShadowSql.net之正确使用方式
3.2.1 GenerateTable配置代码
- 通过Attribute配置表结构
- Table配置表名
- DatabaseGeneratedOption.Identity配置自增列
- Key配置主键
- Column配置了列名或数据库原始类型
- Unique配置唯一索引(支持多列组合唯一)
[Table("Products")]
public class Product(int id, string name)
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; } = id;
[Unique]
[Column("ProductName")]
public string Name { get; } = name;
[Unique("CategoryModel")]
public int CategoryId { get; set; }
[Unique("CategoryModel")]
public string Model { get; set; }
}
[GenerateTable<Product>]
public partial class ProductTable;
3.2.2 GenerateTable生成代码
partial class ProductTable : ShadowSql.Identifiers.Table
{
public ProductTable(string tableName = "Products") : base(tableName)
{
Id = DefineColumn("Id");
Name = DefineColumn("Name", "ProductName");
CategoryId = DefineColumn("CategoryId");
Model = DefineColumn("Model");
AddInsertIgnore(Id);
AddUpdateIgnore(Id);
}
public ShadowSql.Identifiers.IColumn Id { get; }
public new ShadowSql.Identifiers.IColumn Name { get; }
public ShadowSql.Identifiers.IColumn CategoryId { get; }
public ShadowSql.Identifiers.IColumn Model { get; }
}
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Hand.AutoCache (>= 0.3.1.3-alpha)
- Hand.GenerateConvert (>= 0.2.1.12-alpha)
- Hand.GenerateCore (>= 0.2.1.13)
- Hand.Generators.Attributes (>= 0.2.1.7)
- Hand.Generators.EasySyntax (>= 0.2.1.10)
- Hand.MemberValidation (>= 0.3.1.7-alpha)
- Hand.Naming (>= 0.3.1.3-alpha)
- Hand.ParseXml (>= 0.3.1.4-alpha)
- Hand.Projections (>= 0.3.1.9-alpha)
- Microsoft.CodeAnalysis.Analyzers (>= 5.9.0)
- Microsoft.CodeAnalysis.CSharp (>= 5.9.0)
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 |
|---|---|---|
| 0.2.1.20-alpha | 32 | 8/28/2026 |
| 0.2.1.19-alpha | 34 | 8/27/2026 |
| 0.2.1.18-alpha | 30 | 8/27/2026 |
| 0.2.1.17-alpha | 45 | 8/25/2026 |
| 0.2.1.16-alpha | 42 | 8/25/2026 |
| 0.2.1.15-alpha | 50 | 8/24/2026 |
| 0.2.1.14-alpha | 52 | 8/23/2026 |
| 0.2.1.13-alpha | 53 | 8/23/2026 |
| 0.2.1.12-alpha | 55 | 8/21/2026 |
| 0.2.1.11-alpha | 51 | 8/21/2026 |
| 0.2.1.10-alpha | 53 | 8/20/2026 |
| 0.2.1.9-alpha | 60 | 8/17/2026 |
| 0.2.1.8-alpha | 67 | 8/15/2026 |
| 0.2.1.7-alpha | 56 | 8/14/2026 |
| 0.2.1.6-alpha | 61 | 8/13/2026 |
| 0.2.1.5-alpha | 57 | 8/9/2026 |
| 0.2.1.4-alpha | 53 | 8/5/2026 |
| 0.2.1.3-alpha | 61 | 8/2/2026 |
| 0.2.0.2-alpha | 72 | 4/28/2026 |
| 0.2.0.1-alpha | 73 | 4/27/2026 |
Loading failed