项目结构:

cs
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : IBusinessFlow.cs
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DecoratorPattern.Abstractions
{
/// <summary>
///
/// </summary>
public interface IBusinessFlow
{
string FlowName { get; }
Task ExecuteAsync(CancellationToken cancellationToken = default);
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : LoggingBusinessFlowDecorator.cs
*/
using DecoratorPattern.Abstractions;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace DecoratorPattern.Decorators
{
/// <summary>
///
/// </summary>
public class LoggingBusinessFlowDecorator : IBusinessFlow
{
private readonly IBusinessFlow _innerBusinessFlow;
private readonly ILogger<LoggingBusinessFlowDecorator> _logger;
/// <summary>
///
/// </summary>
/// <param name="innerBusinessFlow"></param>
/// <param name="logger"></param>
/// <exception cref="ArgumentNullException"></exception>
public LoggingBusinessFlowDecorator(
IBusinessFlow innerBusinessFlow,
ILogger<LoggingBusinessFlowDecorator> logger)
{
_innerBusinessFlow = innerBusinessFlow ?? throw new ArgumentNullException(nameof(innerBusinessFlow));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public string FlowName => _innerBusinessFlow.FlowName;
/// <summary>
///
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
_logger.LogInformation("===== 【流程启动】{FlowName} =====", FlowName);
try
{
await _innerBusinessFlow.ExecuteAsync(cancellationToken);
_logger.LogInformation("===== 【流程完成】{FlowName} =====", FlowName);
}
catch (Exception ex)
{
_logger.LogError(ex, "===== 【流程异常】{FlowName} 执行过程中发生未预期的异常 =====", FlowName);
throw;
}
}
}
}
cs
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : BusinessFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 9.业务订单流程
/// 业务:客户接单、订单录入、需求确认、订单跟进、售后对接
/// </summary>
public class BusinessFlow : IBusinessFlow
{
public string FlowName => "业务订单流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.接待客户、确认定制需求
// 2.系统录入订单信息、定价核算
// 3.订单状态全程跟进
// 4.交付后售后对接登记
await Task.Delay(170, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : DesignDrawingFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 2.设计制图流程
/// 业务:客户需求对接、首饰建模、CAD制图、方案审核定稿
/// </summary>
public class DesignDrawingFlow : IBusinessFlow
{
public string FlowName => "设计制图流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.收集客户定制需求
// 2.首饰三维建模、CAD图纸绘制
// 3.初稿审核、客户确认
// 4.定稿归档,下发生产图纸
await Task.Delay(250, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : FinanceFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 7.财务流程
/// 业务:订单对账、成本核算、开票登记、回款核销、账务归档
/// </summary>
public class FinanceFlow : IBusinessFlow
{
public string FlowName => "财务流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.订单金额对账
// 2.原料、人工、加工成本核算
// 3.增值税开票登记
// 4.客户回款核销、账务归档
await Task.Delay(210, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : ItFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 11.IT技术支撑流程
/// 业务:业务系统维护、服务器监控、日志运维、故障排查、权限管理
/// </summary>
public class ItFlow : IBusinessFlow
{
public string FlowName => "IT技术支撑流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.业务系统日常巡检
// 2.服务器与数据库监控
// 3.系统权限分配与维护
// 4.运维故障记录与修复
await Task.Delay(130, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : LogisticsFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 6.物流流程
/// 业务:仓储出库、物流对接、保价发货、物流轨迹录入、签收跟踪
/// </summary>
public class LogisticsFlow : IBusinessFlow
{
public string FlowName => "物流流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.仓库出库核验
// 2.贵重物品保价配置
// 3.物流单生成、揽收发货
// 4.系统录入物流轨迹
await Task.Delay(180, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : MarketingPromotionFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 8.营销推广流程
/// 业务:新品宣传、活动策划、线上引流、门店推广、数据统计
/// </summary>
public class MarketingPromotionFlow : IBusinessFlow
{
public string FlowName => "营销推广流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.新品款式宣传素材制作
// 2.线上商城、短视频推广投放
// 3.门店促销活动落地
// 4.推广转化数据统计汇总
await Task.Delay(190, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : PackagingFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 5.包装流程
/// 业务:首饰清洁、防护包装、礼盒封装、证书配套、防伪贴标
/// </summary>
public class PackagingFlow : IBusinessFlow
{
public string FlowName => "包装流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.成品清洁除尘
// 2.独立防护包装
// 3.配套质检证书、售后卡
// 4.礼盒封装、粘贴防伪码
await Task.Delay(120, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : PersonnelAdminFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 10.人事行政流程
/// 业务:人员考勤、排班调度、行政物资、制度落地、人员异动
/// </summary>
public class PersonnelAdminFlow : IBusinessFlow
{
public string FlowName => "人事行政流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.员工考勤统计、排班调整
// 2.办公物资申领核销
// 3.日常行政巡查
// 4.人员入职、异动、离职登记
await Task.Delay(100, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : ProcessingProductionFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 3.加工生产流程
/// 业务:金属浇筑、切割、镶嵌、打磨、抛光、电镀全工序
/// 模拟高并发多工序并行执行
/// </summary>
public class ProcessingProductionFlow : IBusinessFlow
{
public string FlowName => "加工生产流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 多工序并行,模拟工厂高并发生产场景
var tasks = new List<Task>
{
SingleProcess("金属基材浇筑成型", 150, cancellationToken),
SingleProcess("宝石精密镶嵌定位", 180, cancellationToken),
SingleProcess("首饰精细打磨修边", 160, cancellationToken),
SingleProcess("镜面抛光+真空电镀", 200, cancellationToken)
};
await Task.WhenAll(tasks);
}
/// <summary>
/// 单个生产工序模拟
/// </summary>
private async Task SingleProcess(string processName, int ms, CancellationToken ct)
{
await Task.Delay(ms, ct);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : QualityInspectionFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 4.质检流程
/// 业务:成品尺寸校验、牢固度检测、瑕疵筛查、品级定级、出具质检报告
/// </summary>
public class QualityInspectionFlow : IBusinessFlow
{
public string FlowName => "质检流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.外观瑕疵检测
// 2.宝石牢固度、金属纯度检测
// 3.尺寸参数核对
// 4.合格品定级、不合格品返修登记
await Task.Delay(220, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : RawMaterialPurchaseFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 1.原料采购核验流程
/// 业务:珠宝贵金属、宝石原料采购、到货核验、证书校验、入库登记
/// </summary>
public class RawMaterialPurchaseFlow : IBusinessFlow
{
public string FlowName => "原料采购核验流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.核对采购合同与到货清单
// 2.贵金属称重、宝石品级核验
// 3.质检证书真伪校验
// 4.录入供应链系统、完成原料入库
await Task.Delay(200, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : TrainingFlow.cs
*/
using DecoratorPattern.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 12.员工培训流程
/// 业务:新员工岗前培训、工艺培训、服务培训、考核归档
/// </summary>
public class TrainingFlow : IBusinessFlow
{
public string FlowName => "员工培训流程";
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
// 1.制定月度培训计划
// 2.首饰工艺、销售服务培训落地
// 3.员工技能考核
// 4.培训成绩归档存档
await Task.Delay(140, cancellationToken);
}
}
}
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : WorkflowScheduler.cs
*/
using DecoratorPattern.Abstractions;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
namespace DecoratorPattern.Services
{
/// <summary>
/// 珠宝全业务流程调度器
/// 负责按行业标准顺序执行所有业务流程、统一日志调度、异常管控、取消令牌支持
/// 线程安全、兼容高并发、适配DI生命周期
/// </summary>
public class WorkflowScheduler
{
private readonly IEnumerable<IBusinessFlow> _businessFlows;
private readonly ILogger<WorkflowScheduler> _logger;
/// <summary>
/// 构造注入:有序业务流程集合 + 日志器
/// </summary>
public WorkflowScheduler(IEnumerable<IBusinessFlow> businessFlows, ILogger<WorkflowScheduler> logger)
{
_businessFlows = businessFlows ?? throw new ArgumentNullException(nameof(businessFlows));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <summary>
/// 启动完整珠宝行业业务链路
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task RunEntireWorkflowAsync(CancellationToken cancellationToken = default)
{
_logger.LogInformation("========== 珠宝行业全业务流程调度启动 ==========");
_logger.LogInformation("待执行业务流程总数:{Count}", _businessFlows.Count());
var index = 0;
foreach (var flow in _businessFlows)
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogWarning("流程调度收到取消指令,终止后续所有流程");
break;
}
index++;
_logger.LogInformation("开始执行第{Index}项业务流程", index);
try
{
await flow.ExecuteAsync(cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "第{Index}项流程执行失败,全流程终止", index);
throw;
}
}
_logger.LogInformation("========== 珠宝行业全业务流程全部执行完成 ==========");
}
}
}
调用:
cs
/*
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:结构型模式 Structural Patterns 装饰器模式 Decorator Pattern 演示业务层
# Author : geovindu,Geovin Du 涂聚文.
# IDE : vs2026 c# .net 10
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/07/04 22:16
# User : geovindu
# Product : Visual Studio 2026
# Project : CSharpDesignPattern
# File : DecoratorBll.cs
*/
using DecoratorPattern.Abstractions;
using DecoratorPattern.Decorators;
using DecoratorPattern.Services;
using Karambolo.Extensions.Logging.File;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
namespace BLL
{
/// <summary>
///
/// </summary>
public class DecoratorBll
{
public async void Demo()
{
// 1.构建.NET通用主机(DI、日志、生命周期托管)
var host = Host.CreateDefaultBuilder()
.ConfigureLogging((context, loggingBuilder) =>
{
// 清空默认日志,避免重复输出
loggingBuilder.ClearProviders();
// 控制台日志配置
loggingBuilder.AddConsole(options =>
{
options.IncludeScopes = true;
options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff";
});
loggingBuilder.AddFile(fileBuilder =>
{
fileBuilder.RootPath = Directory.GetCurrentDirectory();
string todayDate = DateTime.Now.ToString("yyyy-MM-dd");
Directory.CreateDirectory(Path.Combine(fileBuilder.RootPath, "Logs", todayDate));
fileBuilder.Files = new[]
{
new LogFileOptions
{
Path = $"Logs/{todayDate}/app-debug.log",
MaxFileSize = 1000000,
MinLevel = new Dictionary<string, LogLevel>
{
[""] = LogLevel.Debug
},
IncludeScopes = true
},
new LogFileOptions
{
Path = $"Logs/{todayDate}/app-info.log",
MaxFileSize = 1000000,
MinLevel = new Dictionary<string, LogLevel>
{
[""] = LogLevel.Information
},
IncludeScopes = true
},
new LogFileOptions
{
Path = $"Logs/{todayDate}/app-warn.log",
MaxFileSize = 1000000,
MinLevel = new Dictionary<string, LogLevel>
{
[""] = LogLevel.Warning
},
IncludeScopes = true
},
new LogFileOptions
{
Path = $"Logs/{todayDate}/app-error.log",
MaxFileSize = 1000000,
MinLevel = new Dictionary<string, LogLevel>
{
[""] = LogLevel.Error
},
IncludeScopes = true
}
};
});
// 全局日志级别控制
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
// 过滤系统低级日志,避免刷屏
loggingBuilder.AddFilter("Microsoft", LogLevel.Warning);
loggingBuilder.AddFilter("System", LogLevel.Warning);
})
.ConfigureServices((context, services) =>
{
// 2.注册所有原始业务流程(瞬态,多线程安全)
services.AddTransient<RawMaterialPurchaseFlow>();
services.AddTransient<DesignDrawingFlow>();
services.AddTransient<ProcessingProductionFlow>();
services.AddTransient<QualityInspectionFlow>();
services.AddTransient<PackagingFlow>();
services.AddTransient<LogisticsFlow>();
services.AddTransient<FinanceFlow>();
services.AddTransient<MarketingPromotionFlow>();
services.AddTransient<BusinessFlow>();
services.AddTransient<PersonnelAdminFlow>();
services.AddTransient<ItFlow>();
services.AddTransient<TrainingFlow>();
// 3.【装饰器模式注入】按真实珠宝业务顺序,批量包装日志装饰器
// 业务正向流程:采购→设计→生产→质检→包装→物流→财务→营销→业务→人事→IT→培训
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<RawMaterialPurchaseFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<DesignDrawingFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<ProcessingProductionFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<QualityInspectionFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<PackagingFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<LogisticsFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<FinanceFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<MarketingPromotionFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<BusinessFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<PersonnelAdminFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<ItFlow>(), sp));
services.AddTransient<IBusinessFlow>(sp => WrapDecorator(sp.GetRequiredService<TrainingFlow>(), sp));
// 4.注册调度器
services.AddTransient<WorkflowScheduler>();
})
.Build();
// 5.执行全流程
using var scope = host.Services.CreateScope();
var provider = scope.ServiceProvider;
var logger = provider.GetRequiredService<ILogger<DecoratorBll>>();
try
{
var scheduler = provider.GetRequiredService<WorkflowScheduler>();
await scheduler.RunEntireWorkflowAsync();
}
catch (Exception ex)
{
logger.LogCritical(ex, "珠宝全业务流程调度发生致命异常");
}
finally
{
// 优雅停机,刷新日志缓冲区,防止日志丢失
await host.StopAsync();
}
}
/// <summary>
/// 统一包装方法:将任意业务流程包装为【日志装饰器流程】
/// 纯原生DI实现,无第三方框架依赖
/// </summary>
private static IBusinessFlow WrapDecorator(IBusinessFlow flow, IServiceProvider sp)
{
var logger = sp.GetRequiredService<ILogger<LoggingBusinessFlowDecorator>>();
return new LoggingBusinessFlowDecorator(flow, logger);
}
}
}
输出:

本项目展示了一个采用装饰器模式(Decorator Pattern)实现的珠宝行业业务流程管理系统。系统包含12个核心业务流程(如原料采购、设计制图、加工生产等),每个流程实现IBusinessFlow接口。通过LoggingBusinessFlowDecorator装饰器为各流程动态添加日志功能,实现业务逻辑与日志记录的分离。WorkflowScheduler负责按顺序执行所有业务流程,支持取消令牌和异常处理。系统采用.NET依赖注入框架,结合文件日志和控制台日志,实现了灵活的业务流程组合与扩展。代码结构清晰,体现了装饰器模式在不修改原有类的情况下动态扩展对象功能的优势。