refactor: 重构项目基础架构与实体体系
- 替换原有自定义生命周期接口为通用基础服务体系 - 将所有实体基类替换为带雪花ID的SqlSugarBaseEntity - 迁移DTO到Models项目并统一管理 - 移除冗余的Repository层实现,改用通用基础服务 - 添加Yitter.IdGenerator雪花ID生成支持 - 重构SqlSugar数据库上下文与依赖注入配置 - 新增微信用户、用户勋章、背包等实体与配套服务 - 整理分页查询与结果封装类 - 优化WebApi控制器结构
This commit is contained in:
@ -17,17 +17,8 @@ namespace QYZH.InteractiveMagazine.Infrastructure.Autofacs
|
||||
/// <param name="builder"></param>
|
||||
protected override void Load(ContainerBuilder builder)
|
||||
{
|
||||
//注册Repository(只注册接口,遵循依赖倒置原则)
|
||||
builder.RegisterAssemblyTypes(GetAssemblyByName($"{_assemblyName}.Repository"))
|
||||
.Where(t => t.Name.EndsWith("Repository") && !t.IsAbstract)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerLifetimeScope();
|
||||
|
||||
//注册Service
|
||||
builder.RegisterAssemblyTypes(GetAssemblyByName($"{_assemblyName}.Service"))
|
||||
.Where(t => t.Name.EndsWith("Service") && !t.IsAbstract)
|
||||
.AsImplementedInterfaces()
|
||||
.InstancePerLifetimeScope();
|
||||
builder.RegisterAssemblyTypes(GetAssemblyByName($"{_assemblyName}.Service")).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerLifetimeScope();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -0,0 +1,75 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace QYZH.InteractiveMagazine.Infrastructure.Context
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用程序上下文
|
||||
/// </summary>
|
||||
public static class ServiceContext
|
||||
{
|
||||
public static IServiceProvider ServiceProvider;
|
||||
|
||||
public static void UseServiceContext(this IApplicationBuilder applicationBuilder)
|
||||
{
|
||||
ServiceProvider = applicationBuilder.ApplicationServices;
|
||||
}
|
||||
|
||||
public static void UseServiceContext(this IServiceCollection services)
|
||||
{
|
||||
ServiceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对象实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetService<T>()
|
||||
{
|
||||
return ServiceProvider.GetService<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Scope对象实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetScopeService<T>()
|
||||
{
|
||||
var scope = ServiceProvider.CreateScope();
|
||||
return scope.ServiceProvider.GetService<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对象实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetRequiredService<T>()
|
||||
{
|
||||
return ServiceProvider.GetRequiredService<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对象实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetOptionsMonitor<T>()
|
||||
{
|
||||
return ServiceProvider.GetService<IOptionsMonitor<T>>().CurrentValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取对象实例
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetOptions<T>() where T : class, new()
|
||||
{
|
||||
return ServiceProvider.GetService<IOptions<T>>().Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user