fengling-platform/.planning/codebase/ARCHITECTURE.md
movingsam 1b8c937aa4
Some checks failed
Build and Push Docker / build (push) Failing after 23s
Publish NuGet Packages / build (push) Failing after 8s
feat: 添加 Gateway 路由实体到 Platform
- 新增 GatewayAggregate 领域实体 (GwTenant, GwTenantRoute, GwServiceInstance)
- 新增 IRouteStore, RouteStore, IInstanceStore, InstanceStore
- 新增 IRouteManager, RouteManager
- 合并 GatewayDbContext 到 PlatformDbContext
- 统一 Extensions.AddPlatformCore 注册所有服务
2026-02-28 23:53:00 +08:00

3.8 KiB
Raw Blame History

架构

分析日期: 2026-02-28

模式概述

总体: 清洁架构 + DDD 启发的聚合模式

关键特性:

  • 使用 ASP.NET Core Identity 模式的多租户平台基础设施
  • 领域驱动设计DDD聚合用于业务实体
  • Manager + Store 模式用于数据访问ASP.NET Core Identity 风格)
  • 通过 EF Core 的仓储抽象
  • 多租户TenantInfo 值对象嵌入在用户实体中

层级

领域层 (Fengling.Platform.Domain):

  • 目的: 核心业务实体和领域逻辑
  • 位置: Fengling.Platform.Domain/AggregatesModel/
  • 包含: 聚合、实体、值对象
  • 依赖: Microsoft.AspNetCore.Identity仅接口
  • 被使用: 基础设施层

基础设施层 (Fengling.Platform.Infrastructure):

  • 目的: 数据持久化、外部集成、应用服务
  • 位置: Fengling.Platform.Infrastructure/
  • 包含: DbContext、TenantStore、TenantManager、EF 配置、迁移
  • 依赖: Fengling.Platform.Domain、EF Core、Identity
  • 被使用: 消费应用(如 Console 等)

聚合

TenantAggregate:

  • 实体: Tenant - 表示租户组织
  • 值对象: TenantInfo - 租户上下文TenantId、租户代码、租户名称
  • 模式: 贫血模型(仅属性,无业务逻辑)
  • 位置: Fengling.Platform.Domain/AggregatesModel/TenantAggregate/

UserAggregate:

  • 实体: ApplicationUser - 继承 IdentityUser<long>
  • 包含: RealName、TenantInfo、时间戳、软删除标志
  • 位置: Fengling.Platform.Domain/AggregatesModel/UserAggregate/

RoleAggregate:

  • 实体: ApplicationRole - 继承 IdentityRole<long>
  • 包含: Description、TenantId、IsSystem、DisplayName、Permissions
  • 位置: Fengling.Platform.Domain/AggregatesModel/RoleAggregate/

数据流

租户解析流程:

  1. 用户认证 → 加载 ApplicationUser 及 TenantInfo
  2. TenantInfoTenantId、租户代码、租户名称嵌入用户实体
  3. 所有租户范围查询按 TenantInfo.TenantId 过滤

租户 CRUD 流程:

  1. Controller/Service 调用 ITenantManager
  2. TenantManager 委托给 ITenantStore
  3. TenantStore 通过 PlatformDbContext 执行 EF Core 操作
  4. 更改持久化到 PostgreSQL 数据库

关键抽象

ITenantStore:

  • 目的: Tenant 实体的数据访问操作
  • 接口位置: Fengling.Platform.Infrastructure/ITenantStore.cs
  • 实现: TenantStore<TContext>
  • 模式: 自定义 Store 模式ASP.NET Core Identity 风格)

ITenantManager:

  • 目的: 租户操作的 应用服务
  • 接口位置: Fengling.Platform.Infrastructure/TenantManager.cs
  • 实现: TenantManager
  • 委托给 ITenantStore

PlatformDbContext:

  • 目的: EF Core 数据库上下文
  • 位置: Fengling.Platform.Infrastructure/PlatformDbContext.cs
  • 继承: IdentityDbContext<ApplicationUser, ApplicationRole, long>
  • 包含: Tenant、AccessLog、AuditLog DbSets

入口点

依赖注入注册:

  • 方法: Extensions.AddPlatformCore<TContext>()
  • 位置: Fengling.Platform.Infrastructure/Extensions.cs
  • 注册: DbContext、ITenantStore、ITenantManager

数据库上下文:

  • 类型: PlatformDbContext
  • 数据库: PostgreSQL通过 Npgsql
  • 迁移位置: Fengling.Platform.Infrastructure/Migrations/

错误处理

策略: 标准 ASP.NET Core Identity 结果模式

模式:

  • CRUD 操作返回 IdentityResult(成功/失败)
  • 空检查返回 Task.FromResult<T>(null) 表示未找到
  • 当前未实现自定义异常处理层

横切关注点

日志: 此层未明确配置

验证: 未明确实现(依赖 ASP.NET Core Identity

认证: 使用 ASP.NET Core Identity + OpenIddict从迁移中可见

多租户:

  • 方法: 按 TenantInfo.TenantId 过滤
  • TenantInfo 作为拥有实体嵌入 ApplicationUser
  • 通过实体上的 IsDeleted 标志进行软删除

架构分析: 2026-02-28