完成工作单元

This commit is contained in:
橙子
2023-01-18 00:34:14 +08:00
parent 2fa3570f85
commit eb1a86e5b2
9 changed files with 32 additions and 10 deletions

View File

@@ -1,21 +1,24 @@
using SqlSugar; using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Core.Model;
using Yi.Framework.Ddd.Repositories;
using Yi.Framework.Uow; using Yi.Framework.Uow;
namespace Yi.Framework.Core.Sqlsugar.Uow namespace Yi.Framework.Core.Sqlsugar.Uow
{ {
public class UnitOfWork : IUnitOfWork public class UnitOfWork : IUnitOfWork
{ {
public ISqlSugarClient Db { get; set; } public ISqlSugarClient Db { get; set; }
public ITenant Tenant { get; set; } public ITenant Tenant { get; set; }
public bool IsTran { get; set; } public bool IsTran { get; set; }
public bool IsCommit { get; set; } public bool IsCommit { get; set; }
public bool IsClose { get; set; } public bool IsClose { get; set; }
public void Dispose() public void Dispose()
{ {
@@ -44,5 +47,13 @@ namespace Yi.Framework.Core.Sqlsugar.Uow
} }
return IsCommit; return IsCommit;
} }
public IRepository<T> GetRepository<T>()
{
if (ServiceLocatorModel.Instance is null)
throw new ArgumentNullException("ServiceLocatorModel.Instance");
//又是你这个骚东西
return ServiceLocatorModel.Instance.GetRequiredService<IRepository<T>>();
}
} }
} }

View File

@@ -25,6 +25,7 @@ namespace Yi.Framework.Core.Sqlsugar
services.AddSingleton<IUnitOfWorkManager, UnitOfWorkManager>(); services.AddSingleton<IUnitOfWorkManager, UnitOfWorkManager>();
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions")); services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
services.AddSqlsugarServer(); services.AddSqlsugarServer();

View File

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

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Ddd.Repositories;
namespace Yi.Framework.Uow namespace Yi.Framework.Uow
{ {
@@ -12,6 +13,7 @@ namespace Yi.Framework.Uow
bool IsCommit { get; set; } bool IsCommit { get; set; }
bool IsClose { get; set; } bool IsClose { get; set; }
IRepository<T> GetRepository<T>();
bool Commit(); bool Commit();
} }
} }

View File

@@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Ddd\Yi.Framework.Ddd.csproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -9,9 +9,9 @@
服务实现 服务实现
</summary> </summary>
</member> </member>
<member name="M:Yi.Framework.Application.Student.StudentService.PostShijie"> <member name="M:Yi.Framework.Application.Student.StudentService.PostUow">
<summary> <summary>
你好世界 Uow
</summary> </summary>
<returns></returns> <returns></returns>
</member> </member>

View File

@@ -28,13 +28,15 @@ namespace Yi.Framework.Application.Student
{ {
private readonly IStudentRepository _studentRepository; private readonly IStudentRepository _studentRepository;
private readonly StudentManager _studentManager; private readonly StudentManager _studentManager;
private readonly IUnitOfWorkManager _unitOfWorkManager; //private readonly IUnitOfWorkManager _unitOfWorkManager;
public StudentService(IStudentRepository studentRepository, StudentManager studentManager, IUnitOfWorkManager unitOfWorkManager) public StudentService(IStudentRepository studentRepository, StudentManager studentManager, IUnitOfWorkManager unitOfWorkManager)
{ {
_studentRepository = studentRepository; _studentRepository = studentRepository;
_studentManager = studentManager; _studentManager = studentManager;
_unitOfWorkManager = unitOfWorkManager; _unitOfWorkManager = unitOfWorkManager;
} }
private readonly IUnitOfWorkManager _unitOfWorkManager;
/// <summary> /// <summary>
/// Uow /// Uow
/// </summary> /// </summary>
@@ -44,6 +46,7 @@ namespace Yi.Framework.Application.Student
StudentGetOutputDto res = new(); StudentGetOutputDto res = new();
using (var uow = _unitOfWorkManager.CreateContext()) using (var uow = _unitOfWorkManager.CreateContext())
{ {
var studentRepository = uow.GetRepository<StudentEntity>();
res = await base.CreateAsync(new StudentCreateInputVo { Name = $"老杰哥{DateTime.Now.ToString("ffff")}", Number = 2023 }); res = await base.CreateAsync(new StudentCreateInputVo { Name = $"老杰哥{DateTime.Now.ToString("ffff")}", Number = 2023 });
if (new Random().Next(0, 2) == 0) throw new NotImplementedException(); if (new Random().Next(0, 2) == 0) throw new NotImplementedException();
uow.Commit(); uow.Commit();

View File

@@ -9,6 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\src\framework\Yi.Framework.Uow\Yi.Framework.Uow.csproj" />
<ProjectReference Include="..\Yi.Framework.Application.Contracts\Yi.Framework.Application.Contracts.csproj" /> <ProjectReference Include="..\Yi.Framework.Application.Contracts\Yi.Framework.Application.Contracts.csproj" />
<ProjectReference Include="..\Yi.Framework.Domain\Yi.Framework.Domain.csproj" /> <ProjectReference Include="..\Yi.Framework.Domain\Yi.Framework.Domain.csproj" />
</ItemGroup> </ItemGroup>

View File

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