refactor: 重构项目基础架构与实体体系
- 替换原有自定义生命周期接口为通用基础服务体系 - 将所有实体基类替换为带雪花ID的SqlSugarBaseEntity - 迁移DTO到Models项目并统一管理 - 移除冗余的Repository层实现,改用通用基础服务 - 添加Yitter.IdGenerator雪花ID生成支持 - 重构SqlSugar数据库上下文与依赖注入配置 - 新增微信用户、用户勋章、背包等实体与配套服务 - 整理分页查询与结果封装类 - 优化WebApi控制器结构
This commit is contained in:
@ -4,21 +4,38 @@ using BCrypt.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.OpenApi;
|
||||
using QYZH.InteractiveMagazine.Common.Extensions;
|
||||
using QYZH.InteractiveMagazine.Common.Helpers;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.Autofacs;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.Context;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.Extensions;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using QYZH.InteractiveMagazine.Models.Enum;
|
||||
using QYZH.InteractiveMagazine.Repository;
|
||||
using QYZH.InteractiveMagazine.Repository.Core;
|
||||
using Serilog;
|
||||
using SqlSugar.IOC;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using QYZH.InteractiveMagazine.Infrastructure.Autofacs;
|
||||
using System.Text.Json.Serialization;
|
||||
using Yitter.IdGenerator;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 初始化雪花ID生成器
|
||||
YitIdHelper.SetIdGenerator(new IdGeneratorOptions() { WorkerId = 1 });
|
||||
// autofac注入 允许使用autofac作为DI容器
|
||||
builder.UseAutofac();
|
||||
|
||||
builder.InitSqlSugarDb(new IocConfig()
|
||||
{
|
||||
ConfigId = 0,
|
||||
DbType = IocDbType.MySql,
|
||||
ConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"),
|
||||
IsAutoCloseConnection = true,
|
||||
});
|
||||
|
||||
|
||||
// 配置Serilog
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
@ -26,13 +43,22 @@ Log.Logger = new LoggerConfiguration()
|
||||
.CreateLogger();
|
||||
|
||||
builder.Host.UseSerilog();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;//Required 不作为必填
|
||||
})
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
// 配置返回时间格式转换
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
|
||||
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
||||
}).ConfigureApiBehaviorOptions(opt => opt.SuppressModelStateInvalidFilter = true);//关闭默认模型验证
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
// 跨域配置
|
||||
builder.AddCorsRegister();
|
||||
|
||||
//builder.Services.AddSession();
|
||||
builder.Services.AddHttpClient();
|
||||
// 注册 Swagger 文档
|
||||
builder.Services.AddSwaggerGen(option =>
|
||||
{
|
||||
@ -88,10 +114,9 @@ builder.Services.AddSwaggerGen(option =>
|
||||
|
||||
builder.Services.AddInfrastructureServices(builder.Configuration);
|
||||
|
||||
|
||||
// 初始化SqlSugar
|
||||
SqlSugarDbContext.Init(builder.Configuration);
|
||||
|
||||
//注册 HttpContextAccessor
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddScoped(typeof(BaseRepository<>));
|
||||
|
||||
// 添加CORS
|
||||
builder.Services.AddCors(options =>
|
||||
@ -118,7 +143,7 @@ var app = builder.Build();
|
||||
c.DocExpansion(DocExpansion.None); // ->修改界面打开时自动折叠
|
||||
});
|
||||
}
|
||||
|
||||
app.UseServiceContext();
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors("AllowAll");
|
||||
app.UseMiddleware<GlobalExceptionMiddleware>();
|
||||
|
||||
Reference in New Issue
Block a user