diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextCreationContext.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextCreationContext.cs index fc868c8c..17032ad4 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextCreationContext.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextCreationContext.cs @@ -1,5 +1,6 @@ using System.Data.Common; using Volo.Abp; +using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.SqlSugarCore; @@ -7,7 +8,6 @@ public class SqlSugarDbContextCreationContext { public static SqlSugarDbContextCreationContext Current => _current.Value; private static readonly AsyncLocal _current = new AsyncLocal(); - public string ConnectionStringName { get; } public string ConnectionString { get; } diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs index 2f6b4d6f..78ee04c8 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Diagnostics; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using SqlSugar; @@ -17,7 +18,7 @@ namespace Yi.Framework.SqlSugarCore.Uow public ILogger> Logger { get; set; } public IServiceProvider ServiceProvider { get; set; } - private static AsyncLocalDbContextAccessor ContextInstance => AsyncLocalDbContextAccessor.Instance; + private static AsyncLocalDbContextAccessor ContextInstance => AsyncLocalDbContextAccessor.Instance; protected readonly IUnitOfWorkManager UnitOfWorkManager; protected readonly IConnectionStringResolver ConnectionStringResolver; protected readonly ICancellationTokenProvider CancellationTokenProvider; @@ -39,12 +40,12 @@ namespace Yi.Framework.SqlSugarCore.Uow _dbConnectionCreator = dbConnectionCreator; } - private static object _databaseApiLock = new object(); + //private static object _databaseApiLock = new object(); public virtual async Task GetDbContextAsync() { var unitOfWork = UnitOfWorkManager.Current; - if (unitOfWork == null|| unitOfWork.Options.IsTransactional==false) + if (unitOfWork == null || unitOfWork.Options.IsTransactional == false) { if (ContextInstance.Current is null) { @@ -56,7 +57,6 @@ namespace Yi.Framework.SqlSugarCore.Uow return (TDbContext)ContextInstance.Current; } - var connectionStringName = ConnectionStrings.DefaultConnectionStringName; //获取当前连接字符串,未多租户时,默认为空 @@ -64,8 +64,9 @@ namespace Yi.Framework.SqlSugarCore.Uow var dbContextKey = $"{this.GetType().FullName}_{connectionString}"; - lock (_databaseApiLock) - { + + //lock (_databaseApiLock) + //{ //尝试当前工作单元获取db var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey); @@ -81,9 +82,9 @@ namespace Yi.Framework.SqlSugarCore.Uow unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); } - return (TDbContext)((SqlSugarDatabaseApi)databaseApi).DbContext; - } + //} + } diff --git a/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs b/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs index 94663511..12ef79d4 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Application/Services/TestService.cs @@ -1,34 +1,54 @@ -using Volo.Abp.Application.Services; +using Mapster; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Application.Services; using Volo.Abp.Uow; +using Yi.Framework.Bbs.Application.Contracts.Dtos.Banner; using Yi.Framework.Bbs.Domain.Entities.Forum; +using Yi.Framework.Rbac.Domain.Authorization; +using Yi.Framework.Rbac.Domain.Extensions; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Abp.Application.Services { /// - /// 这是一个示例 + /// 常用魔改及扩展示例 /// public class TestService : ApplicationService { public ISqlSugarRepository sqlSugarRepository { get; set; } + /// - /// 你好世界,动态Api + /// 动态Api /// /// /// + [HttpGet("hello-world")] public string GetHelloWorld(string? name) { + //会自动添加前缀,而不是重置,更符合习惯 + //如果需要重置以"/"根目录开头即可 + //你好世界 return name ?? "HelloWord"; } + /// + /// SqlSugar + /// + /// + public async Task GetSqlSugarDbAsync() + { + //用户体验优先,可直接使用Db操作,依赖抽象 + return await sqlSugarRepository._DbQueryable.ToListAsync(); + } + /// /// 工作单元魔改 - /// 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。 - /// 自动在各个情况处理db客户端最优解之一 /// /// public async Task GetUowAsync() { + // 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。 + // 自动在各个情况处理db客户端最优解之一 int i = 10; List tasks = new List(); @@ -36,12 +56,12 @@ namespace Yi.Abp.Application.Services { tasks.Add(Task.Run(async () => { - using (var uow = UnitOfWorkManager.Begin(true, true)) + await sqlSugarRepository.InsertAsync(new BannerEntity { Name = "插入2" }); + using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true)) { await sqlSugarRepository.InsertAsync(new BannerEntity { Name = "插入1" }); await uow.CompleteAsync(); } - await sqlSugarRepository.InsertAsync(new BannerEntity { Name = "插入2" }); })); await sqlSugarRepository.InsertAsync(new BannerEntity { Name = "插入3" }); i--; @@ -49,5 +69,48 @@ namespace Yi.Abp.Application.Services await Task.WhenAll(tasks); } + + /// + /// 当前用户 + /// + /// + public void GetCurrentUser() + { + //当token鉴权之后,可以直接获取 + if (CurrentUser.Id is not null) + { + //权限 + CurrentUser.GetPermissions(); + + //角色信息 + CurrentUser.GetRoleInfo(); + + //部门id + CurrentUser.GetDeptId(); + } + } + + /// + /// 数据权限 + /// + public void GetDataFilter() + { + //这里会数据权限过滤 + using (DataFilter.DisablePermissionHandler()) + { + //这里不会数据权限过滤 + } + //这里会数据权限过滤 + } + + /// + /// 对象映射 + /// + public void GetMapper() + { + //直接无脑Adapt,无需配置 + var entity = new BannerEntity(); + var dto = entity.Adapt(); + } } }