Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Add]工作单元开关,可在任意 Insert/Update/Delete 之前调用,以关闭工作单元使其失效
  • Loading branch information
stulzq committed May 9, 2019
commit ca53986ab9987277b639343015df632cb87e71a1
127 changes: 124 additions & 3 deletions FreeSql.DbContext.Tests/RepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class AddUpdateInfo {

[Fact]
public void UnitOfWorkRepository() {
foreach (var fsql in new[] { g.sqlite, /*g.mysql,*/ g.pgsql, g.oracle, g.sqlserver }) {
foreach (var fsql in new[] { g.sqlite, /*g.mysql, g.pgsql, g.oracle, g.sqlserver*/ }) {

fsql.CodeFirst.ConfigEntity<FlowModel>(f => {
f.Property(b => b.UserId).IsPrimary(true);
Expand Down Expand Up @@ -104,9 +104,130 @@ public void UnitOfWorkRepository() {
flowRepos.Insert(flow);
uow.Commit();
}
}
}
}
public partial class FlowModel {

[Fact]
public void UnitOfWorkRepositoryWithDisableBeforeInsert()
{
foreach (var fsql in new[] { g.sqlite, })
{
fsql.CodeFirst.ConfigEntity<FlowModel>(f => {
f.Property(b => b.UserId).IsPrimary(true);
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
f.Property(b => b.Name).IsNullable(false);
});

var flowRepos = fsql.GetRepository<FlowModel>();

var flow = new FlowModel()
{
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};

//���������ݿ����Ѵ��ڵ����ݣ�Ϊ�˽������IJ������
flowRepos.Delete(a => a.UserId == 1 &&a.Name== "aaa");

using (var uow = fsql.CreateUnitOfWork())
{
//�رչ�����Ԫ�����Ὺʼ����
uow.Disable();
var uowFlowRepos = uow.GetRepository<FlowModel>();
uowFlowRepos.Insert(flow);
//�ѹرչ�����Ԫ���᲻�ύ��ûӰ�죬�˴�ע����ȷ��������Ԫ�����Ƿ���Ч���ر��ˣ���CommitҲӦ�ò�������
//uow.Commit();
}

Assert.True(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
}

}

[Fact]
public void UnitOfWorkRepositoryWithDisableAfterInsert()
{
foreach (var fsql in new[] {g.sqlite,})
{
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
{
f.Property(b => b.UserId).IsPrimary(true);
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
f.Property(b => b.Name).IsNullable(false);
});

var flowRepos = fsql.GetRepository<FlowModel>();

//���������ݿ����Ѵ��ڵ����ݣ�Ϊ�˽������IJ������
flowRepos.Delete(a => a.UserId == 1 && a.Name == "aaa");

var flow = new FlowModel()
{
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};


Assert.Throws<Exception>(() =>
{
using (var uow = fsql.CreateUnitOfWork())
{
var uowFlowRepos = uow.GetRepository<FlowModel>();
uowFlowRepos.Insert(flow);
//�������� Insert/Update/Delete ���ùر�uow�ķ������ᷢ���쳣
uow.Disable();
uow.Commit();
}

});
}
}

[Fact]
public void UnitOfWorkRepositoryWithoutDisable()
{
foreach (var fsql in new[] { g.sqlite, })
{
fsql.CodeFirst.ConfigEntity<FlowModel>(f =>
{
f.Property(b => b.UserId).IsPrimary(true);
f.Property(b => b.Id).IsPrimary(true).IsIdentity(true);
f.Property(b => b.Name).IsNullable(false);
});

var flowRepos = fsql.GetRepository<FlowModel>();
if (flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"))
{
flowRepos.Delete(a => a.UserId == 1);
}


var flow = new FlowModel()
{
CreateTime = DateTime.Now,
Name = "aaa",
LastModifyTime = DateTime.Now,
UserId = 1,
};


using (var uow = fsql.CreateUnitOfWork())
{
var uowFlowRepos = uow.GetRepository<FlowModel>();
uowFlowRepos.Insert(flow);
//������commit�������ύ���ݿ����
//uow.Commit();
}
Assert.False(flowRepos.Select.Any(a => a.UserId == 1 && a.Name == "aaa"));
}
}


public partial class FlowModel {
public int UserId { get; set; }
public int Id { get; set; }
public int? ParentId { get; set; }
Expand Down
17 changes: 15 additions & 2 deletions FreeSql.DbContext/UnitOfWork/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ public interface IUnitOfWork : IDisposable {

IsolationLevel? IsolationLevel { get; set; }

void Commit();
/// <summary>
/// 是否启用工作单元
/// </summary>
bool Enable { get; }

void Commit();

void Rollback();
}

/// <summary>
/// 禁用工作单元
/// <exception cref="Exception"></exception>
/// <para></para>
/// 若已开启事务(已有Insert/Update/Delete操作),调用此方法将发生异常,建议在执行逻辑前调用
/// </summary>
void Disable();
}
}
25 changes: 24 additions & 1 deletion FreeSql.DbContext/UnitOfWork/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,35 @@ void ReturnObject() {
_conn = null;
}

public IsolationLevel? IsolationLevel { get; set; }

/// <summary>
/// 是否启用工作单元
/// </summary>
public bool Enable { get; private set; } = true;

/// <summary>
/// 禁用工作单元
/// <exception cref="Exception"></exception>
/// <para></para>
/// 若已开启事务(已有Insert/Update/Delete操作),调用此方法将发生异常,建议在执行逻辑前调用
/// </summary>
public void Disable()
{
if (_tran != null)
{
throw new Exception("已开启事务,不能禁用工作单元");
}

Enable = false;
}

public IsolationLevel? IsolationLevel { get; set; }

public DbTransaction GetOrBeginTransaction(bool isCreate = true) {

if (_tran != null) return _tran;
if (isCreate == false) return null;
if (!Enable) return null;
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);

_conn = _fsql.Ado.MasterPool.Get();
Expand Down