Skip to content

Commit

Permalink
md
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Nov 12, 2023
1 parent 00af7e3 commit 5e4f1a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 148 deletions.
155 changes: 8 additions & 147 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,153 +6,14 @@

### 用法

#### 1.在Interface中定义属性

```c#
/// <summary>
/// 分页
/// </summary>
public interface IPager
{
/// <summary>
/// 页码
/// </summary>
[DefaultValue(0), Description("页码,从0开始")]
[Range(0, int.MaxValue)]
int? CurrentPage { get; set; }
/// <summary>
/// 分页项数
/// </summary>
[DefaultValue(10), Description("每页项数,10-30之间")]
[Range(10, 30)]
int? PageLen { get; set; }
}
/// <summary>
/// 查询
/// </summary>
public interface IQuery
{
/// <summary>
/// 关键字
/// </summary>
[StringLength(100), Description("查询关键字")]
string? KeyWord { get; set; }
}
/// <summary>
/// 多租户请求
/// </summary>
public interface ITenantRequest
{
/// <summary>
/// 租户ID
/// </summary>
[Required, Description("租户ID"), DefaultValue("default")]
[FromHeader(Name = "tenant-id")]
string? TenantId { get; set; }
}
```
#### 2.标注需要生成的类

```c#

//支持多次标注,可以生成多个类
[AutoGen("QueryRequest", "Biwen.AutoClassGen.Models")]
[AutoGen("Query2Request", "Biwen.AutoClassGen.Models")]
public interface IQueryRequest : IPager, IQuery
{
}

/// <summary>
/// 多租户请求
/// </summary>
[AutoGen("MyTenantRequest", "Biwen.AutoClassGen.Models")]
public interface ITenantRealRequest : ITenantRequest
{
}

//如果接口中有方法,需要定义一个partial类,实现接口中的方法
//如果接口中没有方法,可以不定义partial类
//当然partial类很重要,一般含有业务逻辑 根据需要自行决定
public partial class QueryRequest
{
public string TestMethod(string arg1, int arg2)
{
return $"{arg1} {arg2}";
}
}
```
#### 3.Gen自动生成类

```c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Threading.Tasks;
using Biwen.AutoClassGen.TestConsole.Interfaces;

#pragma warning disable
namespace Biwen.AutoClassGen.Models
{
public partial class QueryRequest : IQueryRequest
{
/// <inheritdoc cref = "IPager.CurrentPage"/>
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.DescriptionAttribute("页码,从0开始")]
[System.ComponentModel.DataAnnotations.RangeAttribute(0, 2147483647)]
public int? CurrentPage { get; set; }

/// <inheritdoc cref = "IPager.PageLen"/>
[System.ComponentModel.DefaultValueAttribute(10)]
[System.ComponentModel.DescriptionAttribute("每页项数,10-30之间")]
[System.ComponentModel.DataAnnotations.RangeAttribute(10, 30)]
public int? PageLen { get; set; }

/// <inheritdoc cref = "IQuery.KeyWord"/>
[System.ComponentModel.DataAnnotations.StringLengthAttribute(100)]
[System.ComponentModel.DescriptionAttribute("查询关键字")]
public string? KeyWord { get; set; }
}

public partial class Query2Request : IQueryRequest
{
/// <inheritdoc cref = "IPager.CurrentPage"/>
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.DescriptionAttribute("页码,从0开始")]
[System.ComponentModel.DataAnnotations.RangeAttribute(0, 2147483647)]
public int? CurrentPage { get; set; }

/// <inheritdoc cref = "IPager.PageLen"/>
[System.ComponentModel.DefaultValueAttribute(10)]
[System.ComponentModel.DescriptionAttribute("每页项数,10-30之间")]
[System.ComponentModel.DataAnnotations.RangeAttribute(10, 30)]
public int? PageLen { get; set; }

/// <inheritdoc cref = "IQuery.KeyWord"/>
[System.ComponentModel.DataAnnotations.StringLengthAttribute(100)]
[System.ComponentModel.DescriptionAttribute("查询关键字")]
public string? KeyWord { get; set; }
}

public partial class MyTenantRequest : ITenantRealRequest
{
/// <inheritdoc cref = "ITenantRequest.TenantId"/>
[System.ComponentModel.DataAnnotations.RequiredAttribute]
[System.ComponentModel.DescriptionAttribute("租户ID")]
[System.ComponentModel.DefaultValueAttribute("default")]
[Microsoft.AspNetCore.Mvc.FromHeaderAttribute(Name = "tenant-id")]
public string? TenantId { get; set; }
}
}
#pragma warning restore

```bash
dotnet add package Biwen.AutoClassGen.Attributes
```

### 错误码
- [DTO生成器文档](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/Gen-Dto.md)
- [Req生成器文档](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/Gen-request.md)
- [Decor装饰器文档](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/Gen-Decor.md)

- GEN001: 标注特性[AutoGen]的接口必须继承至少一个接口
- GEN011: 非法的类命名
- GEN021: 警告没有使用相同的命名空间
- GEN031: 建议标注特性[AutoGen]以便生成类
### Used by
#### if you use this library, please tell me, I will add your project here.
- [Biwen.QuickApi](https://github.com/vipwan/Biwen.QuickApi)
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Biwen.AutoClassGen

Usage scenario: In many cases, we will have a lot of request objects,
#### Usage scenario

- In many cases, we will have a lot of request objects,
such as GetIdRequest, GetUserRequest, etc..., and these requests may have a large number of the same fields.
For example, the multi-tenant Id, the number of pages, and these attribute fields may have validation rules, binding rules, and Swagger descriptions.
If all this code needs to be written, it will add a lot of work, so Biwen.AutoClassGen came into being to solve this pain point...
- In many cases, we will have a lot of DTO objects,
- AOP & Decorator



[中文](https://github.com/vipwan/Biwen.AutoClassGen/blob/master/README-zh.md)

Expand Down

0 comments on commit 5e4f1a6

Please sign in to comment.