完成工作单元模块

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;
// }
// }
//}
}