完成工作单元模块

This commit is contained in:
橙子
2023-01-17 23:33:14 +08:00
parent da2cf2acc5
commit 2fa3570f85
11 changed files with 134 additions and 102 deletions

View File

@@ -0,0 +1,48 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Uow;
namespace Yi.Framework.Core.Sqlsugar.Uow
{
public class UnitOfWork : 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; }
public void Dispose()
{
if (this.IsTran && IsCommit == false)
{
this.Tenant.RollbackTran();
}
if (this.Db.Ado.Transaction == null && IsClose == false)
{
this.Db.Close();
}
}
public bool Commit()
{
if (this.IsTran && this.IsCommit == false)
{
this.Tenant.CommitTran();
IsCommit = true;
}
if (this.Db.Ado.Transaction == null && this.IsClose == false)
{
this.Db.Close();
IsClose = true;
}
return IsCommit;
}
}
}

View File

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

View File

@@ -13,10 +13,10 @@
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
<ProjectReference Include="..\Yi.Framework.Ddd\Yi.Framework.Ddd.csproj" />
<ProjectReference Include="..\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Uow\" />
<Folder Include="CodeFirst\" />
</ItemGroup>

View File

@@ -5,8 +5,10 @@ using Yi.Framework.Core.Configuration;
using Yi.Framework.Core.Sqlsugar.Extensions;
using Yi.Framework.Core.Sqlsugar.Options;
using Yi.Framework.Core.Sqlsugar.Repositories;
using Yi.Framework.Core.Sqlsugar.Uow;
using Yi.Framework.Ddd;
using Yi.Framework.Ddd.Repositories;
using Yi.Framework.Uow;
namespace Yi.Framework.Core.Sqlsugar
{
@@ -20,6 +22,9 @@ namespace Yi.Framework.Core.Sqlsugar
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
{
services.AddTransient(typeof(IRepository<>), typeof(SqlsugarRepository<>));
services.AddSingleton<IUnitOfWorkManager, UnitOfWorkManager>();
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
services.AddSqlsugarServer();

View File

@@ -12,6 +12,7 @@
<ProjectReference Include="..\Yi.Framework.Core.Autofac\Yi.Framework.Core.Autofac.csproj" />
<ProjectReference Include="..\Yi.Framework.Core.AutoMapper\Yi.Framework.Core.AutoMapper.csproj" />
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
<ProjectReference Include="..\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Uow
{
public interface IUnitOfWork : IDisposable
{
bool IsTran { get; set; }
bool IsCommit { get; set; }
bool IsClose { get; set; }
bool Commit();
}
}

View File

@@ -1,94 +1,8 @@
//namespace Yi.Framework.Uow
//{
// public interface IUnitOfWorkManager
// {
// SugarUnitOfWork CreateContext(bool isTran = true);
// }
namespace Yi.Framework.Uow
{
public interface IUnitOfWorkManager
{
IUnitOfWork CreateContext(bool isTran = true);
}
// /// <summary>
// /// 这个放到实现类中
// /// </summary>
// public class SugarUnitOfWorkManager : IUnitOfWorkManager
// {
// public SugarUnitOfWorkManager(ISqlSugarClient db)
// {
// this.Db = db;
// }
// public SugarUnitOfWork Db { get; set; }
// public SugarUnitOfWork CreateContext(bool isTran = true)
// {
// return Db.CreateContext(isTran);
// }
// }
// /// <summary>
// /// 下面这个定死了
// /// </summary>
// public class SugarUnitOfWork : IDisposable
// {
// public ISqlSugarClient Db { get; internal set; }
// public ITenant Tenant { get; internal set; }
// public bool IsTran { get; internal set; }
// public bool IsCommit { get; internal set; }
// public bool IsClose { get; internal set; }
// public void Dispose()
// {
// if (this.IsTran && IsCommit == false)
// {
// this.Tenant.RollbackTran();
// }
// if (this.Db.Ado.Transaction == null && IsClose == false)
// {
// this.Db.Close();
// }
// }
// public SimpleClient<T> GetRepository<T>() where T : class, new()
// {
// TenantAttribute tenantAttribute = typeof(T).GetCustomAttribute<TenantAttribute>();
// if (tenantAttribute == null)
// {
// return new SimpleClient<T>(Db);
// }
// else
// {
// return new SimpleClient<T>(Db.AsTenant().GetConnection(tenantAttribute.configId));
// }
// }
// public RepositoryType GetMyRepository<RepositoryType>() where RepositoryType : ISugarRepository, new()
// {
// var result = new RepositoryType();
// var type = typeof(RepositoryType).GetGenericArguments()[0];
// TenantAttribute tenantAttribute = type.GetCustomAttribute<TenantAttribute>();
// if (tenantAttribute == null)
// {
// result.Context = this.Db;
// }
// else
// {
// result.Context = this.Db.AsTenant().GetConnection(tenantAttribute.configId);
// }
// return result;
// }
// public bool Commit()
// {
// if (this.IsTran && this.IsCommit == false)
// {
// this.Tenant.CommitTran();
// IsCommit = true;
// }
// if (this.Db.Ado.Transaction == null && this.IsClose == false)
// {
// this.Db.Close();
// IsClose = true;
// }
// return IsCommit;
// }
// }
//}
}

View File

@@ -14,6 +14,7 @@ using Yi.Framework.Application.Contracts.Student.Dtos;
using Yi.Framework.Domain.Student.Entities;
using Yi.Framework.Ddd.Services;
using Yi.Framework.Core.Attributes;
using Yi.Framework.Uow;
namespace Yi.Framework.Application.Student
{
@@ -23,24 +24,31 @@ namespace Yi.Framework.Application.Student
[AppService]
public class StudentService : CrudAppService<StudentEntity, StudentGetOutputDto, StudentGetListOutputDto, long, StudentGetListInputVo, StudentCreateInputVo, StudentUpdateInputVo>,
IStudentService,IAutoApiService
IStudentService, IAutoApiService
{
private readonly IStudentRepository _studentRepository;
private readonly StudentManager _studentManager;
public StudentService(IStudentRepository studentRepository, StudentManager studentManager)
private readonly IUnitOfWorkManager _unitOfWorkManager;
public StudentService(IStudentRepository studentRepository, StudentManager studentManager, IUnitOfWorkManager unitOfWorkManager)
{
_studentRepository = studentRepository;
_studentManager = studentManager;
_unitOfWorkManager = unitOfWorkManager;
}
/// <summary>
/// 你好世界
/// Uow
/// </summary>
/// <returns></returns>
public async Task<List<StudentGetListOutputDto>> PostShijie()
public async Task<StudentGetOutputDto> PostUow()
{
throw new NotImplementedException();
var entities = await _studentRepository.GetMyListAsync();
return await MapToGetListOutputDtosAsync(entities);
StudentGetOutputDto res = new();
using (var uow = _unitOfWorkManager.CreateContext())
{
res = await base.CreateAsync(new StudentCreateInputVo { Name = $"老杰哥{DateTime.Now.ToString("ffff")}", Number = 2023 });
if (new Random().Next(0, 2) == 0) throw new NotImplementedException();
uow.Commit();
}
return res;
}
}
}

View File

@@ -8,7 +8,7 @@ TimeTest.Start();
var builder = WebApplication.CreateBuilder(args);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>url
builder.WebHost.UseStartUrlsServer(builder.Configuration, "StartUrl");
builder.WebHost.UseStartUrlsServer(builder.Configuration);
//<2F><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
builder.UseYiModules(typeof(YiFrameworkWebModule));

View File

@@ -30,3 +30,5 @@
2023:01:17-22:43:56本次运行启动时间为2023毫秒
2023:01:17-22:45:25本次运行启动时间为1803毫秒
2023:01:17-22:45:52本次运行启动时间为1877毫秒
2023:01:17-23:24:07本次运行启动时间为1836毫秒
2023:01:17-23:29:20本次运行启动时间为1958毫秒