跳到主要内容

Platform 架构设计

Platform 模块聚合了大量「工具型」子域(待办、短链接、热榜、TTS、导航、消息、节假日等)。为避免单体膨胀,它采用与 Identity/CmsKit 一致的 DDD 分层 + IModuleStartup 模块化启动,并通过「子域自治 + 统一宿主」的方式组织代码。本文聚焦其架构与扩展方式,功能清单见 Platform 总览

模块启动协议

Platform 通过 PlatformModuleStartup : IModuleStartup 挂载到主宿主(见 宿主总览)。ConfigureServices 中集中注册所有子域服务,Configure 中在 DEBUG 下同步各子域表结构。

public class PlatformModuleStartup : IModuleStartup
{
public void ConfigureServices(IServiceCollection services, IConfiguration c)
{
// 子域服务注册(见下文)
}

public void Configure(WebApplication app, IWebHostEnvironment env)
{
// DEBUG 下 CodeFirst.SyncStructure(...)
}
}

模块启停由 src/Services/Host/FreeKit.DI/E.csModules 字典控制——删除 "platform" 条目即可整体关闭本模块。

分层结构

FreeKit.Platform/
├── Application/ # 应用层:用例、DTO、服务
│ ├── DailyHot/ # 热榜聚合(多平台 Provider)
│ ├── Nav/ # 导航分类与导航项
│ ├── Messages/ # 消息中心(多渠道发送)
│ ├── Notices/ # 通知
│ ├── Projects/ # 开发者项目
│ ├── TTS/ # 文本转语音(MiMo)
│ └── ...
├── Domain/ # 领域层:实体、值对象、领域服务
└── Infrastructure/ # 基础设施:仓库、外部 API 适配

各子域遵循「XxxService(应用服务) → IXxxRepository(仓储) → FreeSql」的标准调用链,与 BuildingBlocks 约定的 ApplicationService/CrudAppService 一致。

子域注册模式

1. 热榜聚合(Keyed Service 多实现)

25+ 平台各自实现 IDailyHotService,通过 AddKeyedSingleton 按枚举键注册,调用方按平台取用:

services.AddKeyedSingleton<IDailyHotService, ZhiHuService>(DailyHotEnum.ZhiHu);
services.AddKeyedSingleton<IDailyHotService, BiliBiliService>(DailyHotEnum.BiliBili);
// ... 其余平台

2. 短链接(独立数据库)

短链接使用独立 FreeSql 实例(连接串 ShortUrlConnectionStrings),与主库物理隔离:

services.AddDefaultFreeSql<ShortFlag>(c, "ShortUrlConnectionStrings");
services.TryAddScoped(typeof(IShortDbRepository<,>), typeof(ShortDbRepository<,>));
services.AddShortUrlServices(c);

3. 消息中心(多渠道策略)

消息发送抽象为 IMessageChannelSender,飞书/企业微信/公众号作为多个实现并存,由 PlatformMessageService 按配置路由:

services.AddScoped<IMessageChannelSender, FeishuWebhookMessageChannelSender>();
services.AddScoped<IMessageChannelSender, WeComWebhookMessageChannelSender>();
services.AddScoped<IMessageChannelSender, WeChatOfficialAccountMessageChannelSender>();

4. TTS(弹性管线)

MiMo TTS 调用较慢,单独配置 HttpClient 弹性管线(超时、重试、熔断),避免与全局超时冲突:

services.AddHttpClient<IMimoTtsClient, MimoTtsClient>(/* BaseAddress */)
.AddStandardResilienceHandler(options =>
{
options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(ttsTimeoutSec);
options.Retry.MaxRetryAttempts = 2;
options.CircuitBreaker.SamplingDuration = TimeSpan.FromSeconds(ttsTimeoutSec * 2);
});

如何新增一个工具子域

  1. Application/ 下新建子域文件夹,定义实体(继承 FullAuditEntity)、应用服务、控制器。
  2. PlatformModuleStartup.ConfigureServices 注册服务;如有独立表,在 ConfigureSyncStructure 中追加实体类型。
  3. 需要独立数据源时,仿照短链接使用 AddDefaultFreeSql<T> + 独立连接串。
  4. E.Modules 已包含 "platform" 的前提下,重建主宿主即可生效。

相关文档