跳到主要内容

IGeekFan.FreeKit.Infrastructure

IGeekFan.FreeKit.Infrastructure 是 FreeKitModules 的内部 BuildingBlock,不对外发布 NuGet 包。

它的职责是承载应用层与接口层之间的通用基础设施:应用服务基类、DTO 约定、统一异常、安全与鉴权、缓存、限流、CAP 集成、HttpClient 包装、验证本地化等。

如果把 FreeKit 看成三层:

  1. Core / Extras 负责基础约定与通用扩展
  2. Infrastructure 负责可复用的应用基础设施
  3. Web 负责 Host 项目的注册与启动装配

使用方式

不是安装包,而是项目引用

在 FreeKitModules 内部业务模块或 Host 项目中,使用 ProjectReference 引用它,而不是 dotnet add package

<ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\IGeekFan.FreeKit.Infrastructure\IGeekFan.FreeKit.Infrastructure.csproj" />
</ItemGroup>

适合放在哪里

  • Application 层:继承 CrudAppService、ApplicationService
  • Host 层:复用 JWT、限流、CAP、HttpClient、reCAPTCHA 等注册扩展
  • Autofac 容器配置:扫描并注册 DomainService

主要能力

应用服务基类

  • ApplicationService:应用服务基类
  • CrudAppService:标准 CRUD 应用服务基类
  • FreeKitController:控制器基础类
  • ICrudAppService:CRUD 服务约定接口

典型用法:

public class TodoAppService
: CrudAppService<Todo, TodoDto, TodoDto, Guid, TodoQuery, CreateTodoInput, UpdateTodoInput>
{
public TodoAppService(IBaseRepository<Todo, Guid> repository) : base(repository)
{
}
}

扩展方法清单

AddJwtService

命名空间:IGeekFan.FreeKit.Infrastructure.Extensions

services.AddJwtService();

注册内容:

  • IJwtService -> JwtService 单例

依赖配置:

  • Authentication 节点中的发行者、受众、签名密钥、过期时间

AddCacheStrategy

命名空间:IGeekFan.FreeKit.Infrastructure

services.AddCacheStrategy();

注册内容:

  • RedisCacheProvider 单例
  • MemoryCacheProvider 单例
  • CacheProviderFactory 单例

AddIpRateLimiting

命名空间:IGeekFan.FreeKit.Infrastructure.IpRateLimiting

services.AddIpRateLimiting(configuration);

注册内容:

  • IpRateLimitOptions
  • IpRateLimitPolicies
  • IRateLimitConfiguration -> RateLimitConfiguration

依赖配置:

  • IpRateLimiting
  • IpRateLimitPolicies

AddGooglereCaptchav3

命名空间:IGeekFan.FreeKit.Infrastructure.Google

services.AddGooglereCaptchav3(configuration);

注册内容:

  • RecaptchaVerifyActionFilter
  • GooglereCAPTCHAOptions
  • AddreCAPTCHAV3(...)

AddApiClient<TClient, TImplementation>

命名空间:IGeekFan.FreeKit.Infrastructure.ApiClient

services.AddApiClient<IUserApiClient, UserApiClient>("https://api.example.com");

注册内容:

  • HttpMessageDeleagteHandler
  • Typed HttpClient
  • Polly 重试策略

AddModelValidatorLocalization

命名空间:IGeekFan.FreeKit.Infrastructure.AspNetCore

services.AddModelValidatorLocalization();

注册内容:

  • IValidationAttributeAdapterProvider -> LocalizedValidationAttributeAdapterProvider

RegisterDomainServices

命名空间:IGeekFan.FreeKit.Infrastructure.Extensions

containerBuilder.RegisterDomainServices(typeof(TodoDomainService).Assembly);

注册规则:

  • 扫描 IDomainService 实现
  • AsImplementedInterfaces()
  • InstancePerLifetimeScope()
  • PropertiesAutowired()
  • EnableInterfaceInterceptors()

UseCap 与 AddKitCap

命名空间:IGeekFan.FreeKit.Infrastructure.Cap

services.AddKitCap(configuration, new[] { typeof(OrderCreatedHandler).Assembly });

依赖配置:

  • CAP:DefaultStorage
  • CAP:DefaultMessageQueue
  • CAP:Version
  • CAP:Path
  • CAP:RabbitMQ:*
  • ConnectionStrings:MySql

与 Web 的边界

  • Infrastructure 负责定义能力和底层注册扩展
  • Web 负责把这些能力组合进 ASP.NET Core Host

如果你关心"一个 Host 从 Program.cs 到中间件顺序怎么装起来",继续看 Web