feat:完成内部核心模块替换改造成furion

This commit is contained in:
橙子
2023-04-15 12:25:47 +08:00
parent 543c13d94b
commit ef5b628b31
10 changed files with 60 additions and 136 deletions

View File

@@ -1,51 +1,71 @@
using Furion;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Yi.Framework.Infrastructure.Ddd.Repositories;
using Yi.Framework.Infrastructure.Uow;
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
{
public class SqlsugarUnitOfWork : IUnitOfWork, ISingleton
public class SqlsugarUnitOfWork : IUnitOfWork
{
public ISqlSugarClient Db { get; set; }
public ITenant Tenant { get; set; }
public bool IsTran { get; set; }
public bool IsCommit { get; set; }
public bool IsClose { get; set; }
// <summary>
/// SqlSugar 对象
/// </summary>
private readonly ISqlSugarClient _sqlSugarClient;
public void Dispose()
/// <summary>
/// 构造函数
/// </summary>
/// <param name="sqlSugarClient"></param>
public SqlsugarUnitOfWork(ISqlSugarClient sqlSugarClient)
{
if (IsTran && IsCommit == false)
{
Tenant.RollbackTran();
}
if (Db.Ado.Transaction == null && IsClose == false)
{
Db.Close();
}
_sqlSugarClient = sqlSugarClient;
}
public bool Commit()
/// <summary>
/// 开启工作单元处理
/// </summary>
/// <param name="context"></param>
/// <param name="unitOfWork"></param>
/// <exception cref="NotImplementedException"></exception>
public void BeginTransaction(FilterContext context, UnitOfWorkAttribute unitOfWork)
{
if (IsTran && IsCommit == false)
{
Tenant.CommitTran();
IsCommit = true;
}
if (Db.Ado.Transaction == null && IsClose == false)
{
Db.Close();
IsClose = true;
}
return IsCommit;
_sqlSugarClient.AsTenant().BeginTran();
}
public IRepository<T> GetRepository<T>()
/// <summary>
/// 提交工作单元处理
/// </summary>
/// <param name="resultContext"></param>
/// <param name="unitOfWork"></param>
/// <exception cref="NotImplementedException"></exception>
public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
{
return App.GetRequiredService<IRepository<T>>();
_sqlSugarClient.AsTenant().CommitTran();
}
/// <summary>
/// 回滚工作单元处理
/// </summary>
/// <param name="resultContext"></param>
/// <param name="unitOfWork"></param>
/// <exception cref="NotImplementedException"></exception>
public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
{
_sqlSugarClient.AsTenant().RollbackTran();
}
/// <summary>
/// 执行完毕(无论成功失败)
/// </summary>
/// <param name="context"></param>
/// <param name="resultContext"></param>
/// <exception cref="NotImplementedException"></exception>
public void OnCompleted(FilterContext context, FilterContext resultContext)
{
_sqlSugarClient.Dispose();
}
}
}

View File

@@ -1,38 +0,0 @@
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Infrastructure.Uow;
namespace Yi.Framework.Infrastructure.Sqlsugar.Uow
{
/// <summary>
/// 此部分为sqlsugr的魔改版本
/// </summary>
internal class SqlsugarUnitOfWorkManager : IUnitOfWorkManager,ISingleton
{
public SqlsugarUnitOfWorkManager(ISqlSugarClient db)
{
Db = db;
}
public ISqlSugarClient Db { get; set; }
public IUnitOfWork CreateContext(bool isTran = true)
{
SqlsugarUnitOfWork uow = new SqlsugarUnitOfWork();
return CreateContext(isTran, uow);
}
private IUnitOfWork CreateContext(bool isTran, SqlsugarUnitOfWork sugarUnitOf)
{
sugarUnitOf.Db = Db;
sugarUnitOf.Tenant = Db.AsTenant();
sugarUnitOf.IsTran = isTran;
Db.Open();
if (isTran)
Db.AsTenant().BeginTran();
return sugarUnitOf;
}
}
}

View File

@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Yi.Framework.Infrastructure.AspNetCore;
using Yi.Framework.Infrastructure.Sqlsugar;
using Yi.Framework.Infrastructure.Sqlsugar.Uow;
namespace Yi.Framework.Infrastructure;
@@ -17,6 +18,8 @@ public class Startup : AppStartup
services.Configure<DbConnOptions>(App.Configuration.GetSection("DbConnOptions"));
services.AddDbSqlsugarContextServer();
services.AddUnitOfWork<SqlsugarUnitOfWork>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

View File

@@ -1,26 +0,0 @@
using Yi.Framework.Infrastructure.Ddd.Repositories;
namespace Yi.Framework.Infrastructure.Uow
{
internal class DefaultUnitOfWork : IUnitOfWork
{
public DefaultUnitOfWork() { }
public bool IsTran { get; set; }
public bool IsCommit { get; set; }
public bool IsClose { get; set; }
public bool Commit()
{
return true;
}
public void Dispose()
{
}
public IRepository<T> GetRepository<T>()
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,10 +0,0 @@
namespace Yi.Framework.Infrastructure.Uow
{
internal class DefaultUnitOfWorkManager : IUnitOfWorkManager
{
public IUnitOfWork CreateContext(bool isTran = true)
{
return new DefaultUnitOfWork();
}
}
}

View File

@@ -1,14 +0,0 @@
using Yi.Framework.Infrastructure.Ddd.Repositories;
namespace Yi.Framework.Infrastructure.Uow
{
public interface IUnitOfWork : IDisposable
{
bool IsTran { get; set; }
bool IsCommit { get; set; }
bool IsClose { get; set; }
IRepository<T> GetRepository<T>();
bool Commit();
}
}

View File

@@ -1,8 +0,0 @@
namespace Yi.Framework.Infrastructure.Uow
{
public interface IUnitOfWorkManager
{
IUnitOfWork CreateContext(bool isTran = true);
}
}