Files
f9-web-ai-test/backend/F9WebAiTest.Infrastructure/DependencyInjection.cs
T
2026-10-08 10:43:59 +08:00

84 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using FreeSql;
using FreeSql.Aop;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.Departments;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Menus;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Roles;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Users;
using F9WebAiTest.Application.Interfaces.Security;
using F9WebAiTest.Application.Interfaces.SnowflakeID;
using F9WebAiTest.Application.Interfaces.UOW;
using F9WebAiTest.Domain.Common;
using F9WebAiTest.Domain.Entities;
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Permissions;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
using F9WebAiTest.Infrastructure.FreeSqls.Persistence;
using F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Departments;
using F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Menus;
using F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Permissions;
using F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Roles;
using F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.RBAC.Users;
using F9WebAiTest.Infrastructure.InterfaceImplements.Security;
using F9WebAiTest.Infrastructure.InterfaceImplements.SnowflakeID;
using F9WebAiTest.Infrastructure.Repositories;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace F9WebAiTest.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
// 创建 FreeSql 实例(SQLite),关闭自动同步结构(只建新表,不修改旧库)
var connStr = configuration.GetConnectionString("Default");
var fsql = new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, connStr)
.UseAutoSyncStructure(false)
.Build();
// 软删除全局过滤器:自动应用到新旧两套实体基类
fsql.GlobalFilter.Apply<BaseEntity>("软删除", a => a.IsDeleted == false);
fsql.GlobalFilter.Apply<Entity>("软删除", a => a.IsDeleted == false);
// 枚举统一映射为 int(与旧库 INTEGER 存储保持一致)
fsql.Aop.ConfigEntityProperty += (_, e) =>
{
if (e.Property.PropertyType.IsEnum)
e.ModifyResult.MapType = typeof(int);
};
services.AddSingleton(fsql);
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
// ==================== F9 风格 RBAC(DDD + CQRS)依赖注册 ====================
// 雪花 ID(有状态,单例)
services.AddSingleton<ISnowflakeIdGenerator, SnowflakeIdGenerator>();
// 密码哈希
services.AddScoped<IPasswordHasher, PasswordHasher>();
// 工作单元(事务)
services.AddScoped<IUnitOfWork_F9, UnitOfWork_F9>();
// 写仓储(Domain 接口)
services.AddScoped<IUserWriteRepository, UserWriteRepository>();
services.AddScoped<IUserRoleWriteRepository, UserRoleWriteRepository>();
services.AddScoped<IRoleWriteRepository, RoleWriteRepository>();
services.AddScoped<IRolePermissionWriteRepository, RolePermissionWriteRepository>();
services.AddScoped<IMenuWriteRepository, MenuWriteRepository>();
services.AddScoped<IPermissionWriteRepository, PermissionWriteRepository>();
services.AddScoped<IDepartmentWriteRepository, DepartmentWriteRepository>();
services.AddScoped<IDepartmentUserRepository, DepartmentUserRepository>();
// 读仓储(Application 接口)
services.AddScoped<IUserReadRepository, UserReadRepository>();
services.AddScoped<IRoleReadRepository, RoleReadRepository>();
services.AddScoped<IMenuReadRepository, MenuReadRepository>();
services.AddScoped<IDepartmentReadRepository, DepartmentReadRepository>();
return services;
}
}