style: 添加扩展示例

This commit is contained in:
陈淳
2024-01-25 10:06:32 +08:00
parent 633f8d66f2
commit 3fa884f326
3 changed files with 81 additions and 17 deletions

View File

@@ -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<SqlSugarDbContextCreationContext> _current = new AsyncLocal<SqlSugarDbContextCreationContext>();
public string ConnectionStringName { get; }
public string ConnectionString { get; }

View File

@@ -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<UnitOfWorkSqlsugarDbContextProvider<TDbContext>> 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<TDbContext> 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;
}
//}
}

View File

@@ -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
{
/// <summary>
/// 这是一个示例
/// 常用魔改及扩展示例
/// </summary>
public class TestService : ApplicationService
{
public ISqlSugarRepository<BannerEntity> sqlSugarRepository { get; set; }
/// <summary>
/// 你好世界,动态Api
/// 动态Api
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[HttpGet("hello-world")]
public string GetHelloWorld(string? name)
{
//会自动添加前缀,而不是重置,更符合习惯
//如果需要重置以"/"根目录开头即可
//你好世界
return name ?? "HelloWord";
}
/// <summary>
/// SqlSugar
/// </summary>
/// <returns></returns>
public async Task<object> GetSqlSugarDbAsync()
{
//用户体验优先可直接使用Db操作依赖抽象
return await sqlSugarRepository._DbQueryable.ToListAsync();
}
/// <summary>
/// 工作单元魔改
/// 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。
/// 自动在各个情况处理db客户端最优解之一
/// </summary>
/// <returns></returns>
public async Task GetUowAsync()
{
// 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。
// 自动在各个情况处理db客户端最优解之一
int i = 10;
List<Task> tasks = new List<Task>();
@@ -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);
}
/// <summary>
/// 当前用户
/// </summary>
/// <returns></returns>
public void GetCurrentUser()
{
//当token鉴权之后可以直接获取
if (CurrentUser.Id is not null)
{
//权限
CurrentUser.GetPermissions();
//角色信息
CurrentUser.GetRoleInfo();
//部门id
CurrentUser.GetDeptId();
}
}
/// <summary>
/// 数据权限
/// </summary>
public void GetDataFilter()
{
//这里会数据权限过滤
using (DataFilter.DisablePermissionHandler())
{
//这里不会数据权限过滤
}
//这里会数据权限过滤
}
/// <summary>
/// 对象映射
/// </summary>
public void GetMapper()
{
//直接无脑Adapt无需配置
var entity = new BannerEntity();
var dto = entity.Adapt<BannerGetListOutputDto>();
}
}
}