11# CodeInject - 代码区域源生成器
22
3+ [ English] ( README.md ) | 简体中文
4+
5+ [ ![ Build and Publish] ( https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml/badge.svg )] ( https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml )
6+ [ ![ Release] ( https://github.com/RRQM/CodeInject/actions/workflows/release.yml/badge.svg )] ( https://github.com/RRQM/CodeInject/actions/workflows/release.yml )
7+ [ ![ NuGet Version] ( https://img.shields.io/nuget/v/CodeInject )] ( https://www.nuget.org/packages/CodeInject/ )
8+ [ ![ NuGet Downloads] ( https://img.shields.io/nuget/dt/CodeInject )] ( https://www.nuget.org/packages/CodeInject/ )
9+
310一个强大的源生成器,可在编译时将模板文件中的代码区域注入到部分类中。
411
512## ✨ 特性
@@ -56,11 +63,10 @@ public async Task<{ReturnType}> Create{EntityName}Async({ReturnType} entity)
5663### 2. 应用特性
5764
5865``` csharp
59- using CodeRegionSourceGenerator ;
66+ using CodeInject ;
6067
61- [RegionInject (" Templates/ApiTemplate.cs" , " ApiMethods" ,
62- " ReturnType" , " User" ,
63- " EntityName" , " User" )]
68+ [RegionInject (FileName = " Templates/ApiTemplate.cs" , RegionName = " ApiMethods" ,
69+ Placeholders = new [] { " ReturnType" , " User" , " EntityName" , " User" })]
6470public partial class UserService
6571{
6672 private readonly IRepository _repository ;
@@ -100,19 +106,34 @@ partial class UserService
100106### 多重注入
101107
102108``` csharp
103- [RegionInject (" Templates/CrudTemplate.cs" , " CreateMethods" , " Entity" , " Product" )]
104- [RegionInject (" Templates/CrudTemplate.cs" , " UpdateMethods" , " Entity" , " Product" )]
105- [RegionInject (" Templates/ValidationTemplate.cs" , " Validators" , " Type" , " Product" )]
109+ [RegionInject (FileName = " Templates/CrudTemplate.cs" , RegionName = " CreateMethods" ,
110+ Placeholders = new [] { " Entity" , " Product" })]
111+ [RegionInject (FileName = " Templates/CrudTemplate.cs" , RegionName = " UpdateMethods" ,
112+ Placeholders = new [] { " Entity" , " Product" })]
113+ [RegionInject (FileName = " Templates/ValidationTemplate.cs" , RegionName = " Validators" ,
114+ Placeholders = new [] { " Type" , " Product" })]
106115public partial class ProductService
107116{
108117 // 多个代码区域将被注入
109118 }
110119```
111120
112- ### 使用 Placeholders 属性
121+ ### 搜索所有文件中的区域
122+
123+ 如果不指定 ` FileName ` ,生成器将在所有可用文件中搜索指定的区域:
124+
125+ ``` csharp
126+ [RegionInject (RegionName = " CommonMethods" )]
127+ public partial class BaseService
128+ {
129+ // 生成器将在所有文件中搜索"CommonMethods"区域
130+ }
131+ ```
132+
133+ ### 使用属性初始化器
113134
114135``` csharp
115- [RegionInject (" Templates/ApiTemplate.cs" , " ApiMethods" ,
136+ [RegionInject (FileName = " Templates/ApiTemplate.cs" , RegionName = " ApiMethods" ,
116137 Placeholders = new [] { " ReturnType" , " Order" , " EntityName" , " Order" })]
117138public partial class OrderService
118139{
@@ -171,6 +192,16 @@ public async Task<ActionResult<{EntityType}>> Create{EntityName}({EntityType} {e
171192#endregion
172193```
173194
195+ 使用方法:
196+ ``` csharp
197+ [RegionInject (FileName = " Templates/ControllerTemplate.cs" , RegionName = " CrudActions" ,
198+ Placeholders = new [] { " EntityType" , " Product" , " EntityName" , " Product" , " entityName" , " product" })]
199+ public partial class ProductController : ControllerBase
200+ {
201+ // 生成的 CRUD 操作将被注入到这里
202+ }
203+ ```
204+
174205### 2. 仓储模式模板
175206
176207``` csharp
@@ -195,6 +226,15 @@ public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
195226#endregion
196227```
197228
229+ 使用方法:
230+ ``` csharp
231+ [RegionInject (FileName = " Templates/RepositoryTemplate.cs" , RegionName = " RepositoryMethods" ,
232+ Placeholders = new [] { " EntityType" , " User" , " EntityName" , " User" })]
233+ public partial class UserRepository
234+ {
235+ // 生成的仓储方法将被注入到这里
236+ }
237+
198238## 🔍 诊断信息
199239
200240源生成器提供以下诊断信息:
@@ -205,10 +245,11 @@ public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
205245
206246## 💡 最佳实践
207247
208- 1 . ** 组织模板文件 ** : 将模板文件放在专门的 ` Templates ` 文件夹中
248+ 1 . ** 组织模板 ** : 将模板文件保存在专门的 `Templates ` 文件夹中
2092492 . ** 命名约定** : 使用描述性的区域名称,如 `CrudMethods `、`ValidationRules `
210- 3 . ** 占位符命名** : 使用大写的占位符名称 ,如 ` ENTITY_NAME ` 、` RETURN_TYPE `
250+ 3 . ** 占位符命名** : 使用一致的占位符名称 ,如 `EntityType `、`EntityName `
2112514 . ** 模块化** : 将相关功能分组到不同的区域中
252+ 5 . ** 基于属性的语法** : 使用新的基于属性的初始化方式以获得更好的可读性
212253
213254## 📋 系统要求
214255
@@ -226,21 +267,21 @@ public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
226267
227268## 🆚 与其他方案对比
228269
229- | 特性 | CodeInject | T4 模板 | 手动编写 |
230- | ------| ------------| -- -------| ---------- |
231- | 编译时生成 | ✅ | ❌ | ❌ |
232- | 增量编译 | ✅ | ❌ | ✅ |
233- | IDE 支持 | ✅ | ⚠️ | ✅ |
234- | 学习成本 | 低 | 高 | 低 |
235- | 灵活性 | 高 | 高 | 低 |
270+ | 特性 | CodeInject | T4 模板 | 手动编写 |
271+ | ---------- | ---------- | ------ - | -------- |
272+ | 编译时生成 | ✅ | ❌ | ❌ |
273+ | 增量编译 | ✅ | ❌ | ✅ |
274+ | IDE 支持 | ✅ | ⚠️ | ✅ |
275+ | 学习成本 | 低 | 高 | 低 |
276+ | 灵活性 | 高 | 高 | 低 |
236277
237278## 📞 支持
238279
239- 如果遇到问题,请 :
280+ 如果遇到问题:
240281
241- 1 . 检查[ 常见问题 ] ( https://github.com/yourusername/CodeInject/wiki/FAQ )
242- 2 . 搜索[ 已有问题] ( https://github.com/yourusername/CodeInject/issues )
243- 3 . 创建[ 新问题] ( https://github.com/yourusername/CodeInject/issues/new )
282+ 1 . 检查 [ FAQ ](https :// github.com/yourusername/CodeInject/wiki/FAQ)
283+ 2 . 搜索 [已有问题](https :// github.com/yourusername/CodeInject/issues)
284+ 3 . 创建 [新问题](https :// github.com/yourusername/CodeInject/issues/new)
244285
245286-- -
246287
0 commit comments