ChainExecutor.NetCoreApp
2.0.0
dotnet add package ChainExecutor.NetCoreApp --version 2.0.0
NuGet\Install-Package ChainExecutor.NetCoreApp -Version 2.0.0
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="ChainExecutor.NetCoreApp" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ChainExecutor.NetCoreApp" Version="2.0.0" />
<PackageReference Include="ChainExecutor.NetCoreApp" />
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 ChainExecutor.NetCoreApp --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ChainExecutor.NetCoreApp, 2.0.0"
#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 ChainExecutor.NetCoreApp@2.0.0
#: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=ChainExecutor.NetCoreApp&version=2.0.0
#tool nuget:?package=ChainExecutor.NetCoreApp&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ChainExecutor.NetCoreApp 企业级链式执行框架 - 标准使用示例
1. 业务模型(带 DataAnnotation 验证)
using System.ComponentModel.DataAnnotations;
/// <summary>
/// 用户创建模型(带自动验证特性)
/// </summary>
public class UserCreateModel
{
/// <summary>
/// 用户ID
/// </summary>
public int Id { get; set; }
/// <summary>
/// 用户名(必填)
/// </summary>
[Required(ErrorMessage = "用户名不能为空")]
public string Username { get; set; } = string.Empty;
/// <summary>
/// 年龄(18~120岁)
/// </summary>
[Range(18, 120, ErrorMessage = "年龄必须≥18且≤120")]
public int Age { get; set; }
}
2. 链式调用代码(同步 / 异步双支持)
异步写法(推荐,生产标准)
using ChainExecutor.NetCoreApp;
/// <summary>
/// 链式执行器 - 企业级标准用法
/// </summary>
public async Task<UserCreateModel> CreateUserAsync()
{
// 1. 链式编排业务流程
var result = await new ChainExecutor<UserCreateModel>()
// 初始化模型数据
.InitData(new UserCreateModel
{
Id = 1,
Username = "张三",
Age = 17 // 故意设为17,触发验证失败
})
// 自动验证 DataAnnotation 特性([Required]/[Range])
.ValidateAnnotations()
// 自定义业务规则验证
.ValidateModel(m => !string.IsNullOrWhiteSpace(m.Username))
// 业务逻辑处理
.Process(ctx =>
{
// 处理业务数据
ctx.Data!.Username = $"【正式用户】{ctx.Data.Username}";
// 上下文临时存储(跨步骤共享数据)
ctx.Items["BusinessStep"] = "用户信息格式化完成";
})
// 事务包裹(数据库操作统一提交/回滚)
.UseTransaction(ctx =>
{
// 此处编写数据库写入逻辑
// _dbContext.Add(ctx.Data);
// _dbContext.SaveChanges();
})
// 最终执行
.ExecuteAsync();
// 2. 结果处理
if (!result.IsSuccess)
{
Console.WriteLine($"执行失败:{result.ErrorMessage}");
throw new CustomValidationException(result.ErrorMessage);
}
Console.WriteLine($"执行成功:{result.Data!.Username}");
return result.Data;
}
同步写法
public UserCreateModel CreateUserSync()
{
var result = new ChainExecutor<UserCreateModel>()
.InitData(new UserCreateModel { Id = 1, Username = "张三", Age = 20 })
.ValidateAnnotations()
.ValidateModel(m => m.Age >= 18)
.Process(ctx => { })
.UseTransaction(ctx => { })
.Execute();
return result.IsSuccess ? result.Data! : throw new Exception(result.ErrorMessage);
}
- 执行结果与日志输出
// 基础结果判断
if (result.IsSuccess)
{
Console.WriteLine("✅ 业务执行成功");
Console.WriteLine($"用户名称:{result.Data!.Username}");
}
else
{
Console.WriteLine("❌ 业务执行失败");
Console.WriteLine($"错误信息:{result.ErrorMessage}");
}
// 全流程日志输出(问题排查神器)
Console.WriteLine("\n===== 执行日志 =====");
foreach (var log in result.ExecuteLogs)
{
Console.WriteLine(log);
}
// 性能耗时
Console.WriteLine($"\n总耗时:{result.ElapsedMilliseconds} ms");
4. 框架核心能力(企业级特性)
| 特性 | 说明 |
|---|---|
| 🔗 链式编程 | 流程声明式编写,无嵌套、无冗余代码 |
| 🛡️ 自动验证 | 支持 DataAnnotation 特性验证 + 自定义验证 |
| 📝 全链路日志 | 自动记录调用方、步骤、时间、状态 |
| 🔒 异常隔离 | 自动捕获异常,中断后续流程,不污染业务 |
| 🧵 线程安全 | 内置线程安全参数字典,支持高并发 |
| 🔄 同步 / 异步 | 完美支持同步 / 异步业务场景 |
| 📊 性能监控 | 自动统计全流程执行耗时 |
| 🎯 AOP 拦截 | 支持前置 / 后置 / 异常全局钩子 |
| 💼 事务管理 | 内置事务包裹,统一提交 / 回滚 |
| 🧩 依赖注入 | 支持 IServiceProvider 注入服务 |
5. 典型应用场景
- 微服务业务流程编排
- 数据库事务统一管理
- API 接口统一逻辑处理
- 审批流 / 工作流节点执行
- 数据清洗与转换管道
- 后台定时任务编排
- 复杂业务规则校验
6. 框架优势
6. 框架优势
- ✅ 无侵入:不依赖任何第三方库,复制即用
- ✅ 高兼容:完全向后兼容,原有代码无需修改
- ✅ 易维护:流程清晰,日志完整,排查问题极快
- ✅ 可扩展:支持 AOP、拦截、事务、缓存扩展
- ✅ 生产级:企业标准架构,可直接上线使用
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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.
-
net8.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.