-
Notifications
You must be signed in to change notification settings - Fork 847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IFreeSql 事务另类玩法 #322
Comments
为什么不集成这种方式的事务到frees起来源码呢 |
在导入excel里的多行记录时, 每一行数据的写入都需要 begin 和 commit/rollback, 以保证每行的写入不会受其他行影响. 否则, 在默认情况下, 100行数据就会 耗尽 数据库连接 |
一行数据,用一个事务?你这是什么操作。 |
我这个excel 的一行有很多数据,, 实际上是要写入3张表 |
@zhouandke 一楼代码已更新,多谢提醒 |
fsql 外部事务demo通过fsql得到transaction
通过外部事务WithTransaction 指定事务
如下demo
|
这个用完不需要归还连接吗 之前自己写的都归还啦 |
加了using |
请问这种方式怎么与IBaseRepository 结合使用?创建BaseRepository后需要手工绑定工作单元,但这个TransactionFreeSql类没有UnitOfWork相关的数据,我尝试为TransactionFreeSql类写了一个获取UnitOfWork的属性,但貌似不能满足DbContext事务的使用方式,不知道该怎么写,或者有没有更好实现方式?
|
大概如下,fsql、trans、uow、repo 交给 IoC 错误点:public IUnitOfWork UnitOfWork => new UowOfFsql(this);
IFreeSql fsql = ...;
using (var trans = new TransactionFreeSql(fsql))
{
var uow = new UowOfFsql(trans);
var repo = fsql.GetRepository<T>();
repo.UnitOfWork = uow;
repo.Insert(...);
trans.Commit();
} 如果你需要 uow,应该把 TransacitonFreeSql 定义到 uow 中,如下: IFreeSql fsql = ...;
//使用者,只需要关心 IUnitOfWork
using (var uow = new UowOfFsql(fsql))
{
var repo = fsql.GetRepository<T>();
repo.UnitOfWork = uow;
repo.Insert(...);
uow.Commit();
}
public class UowOfFsql : IUnitOfWork
{
readonly IFreeSql _fsql;
readonly TransactionFreeSql _trans;
public UowOfFsql(IFreeSql fsql)
{
_fsql = fsql;
_trans = new TransactionFreeSql(_fsql);
}
public IFreeSql Orm => _trans;
public IsolationLevel? IsolationLevel { get; set; }
public DbContext.EntityChangeReport EntityChangeReport { get; }
public Dictionary<string, object> States { get; }
public void Commit() => _trans.Commit();
public void Dispose() { }
public DbTransaction GetOrBeginTransaction(bool isCreate = true)
{
return _trans._transaction;
}
public void Rollback() => _trans.Rollback();
} |
@2881099 |
@2881099 有个typo,是 |
我们都知道 IFreeSql 以单例注入使用,所以直接使用 IFreeSql curd 做事务。
要么:
要么:
除了这两种事务,还可以选择使用 FreeSql.DbContext、FreeSql.Repository + UnitOfWork 来做事务,他们不受异步限制、并且能减少显示传递事务的麻烦。
条件允许还可以使用 FreeSql.Repository + UnitOfWorkManager 完成更高阶的事务管理操作。
本文内容
另类事务,直接在 IFreeSql 上开刀:
ITransactionFreeSql 的代码定义如下:(相信大多数人都看得懂)
The text was updated successfully, but these errors were encountered: