Skip to content

Commit

Permalink
Add UnitOfWorkManager to SongService and implement Test3 for nested t…
Browse files Browse the repository at this point in the history
…ransactions
  • Loading branch information
luoyunchong committed Nov 4, 2024
1 parent 35cb147 commit b8fd54d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions docs/guide/transaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,21 @@ public class SongService
{
readonly IBaseRepository<Song> _songRepository;
readonly IBaseRepository<Detail> _detailRepository;
readonly UnitOfWorkManager _unitOfWorkManager;

public SongService(IBaseRepository<Song> songRepository, IBaseRepository<Detail> detailRepository)
public SongService(
IBaseRepository<Song> songRepository,
IBaseRepository<Detail> detailRepository,
UnitOfWorkManager unitOfWorkManager
)
{
_songRepository = songRepository;
_detailRepository = detailRepository;
_unitOfWorkManager = unitOfWorkManager;
}

[Transactional]
async public Task Test1()
public async Task Test1()
{
//所有注入的仓储对象,都是一个事务
await _songRepository.InsertAsync(xxx1);
Expand All @@ -97,6 +103,16 @@ public class SongService
public void Test2() //嵌套事务
{
}

public async Task Test3()
{
using (var uow = _unitOfWorkManager.Begin())
{
await _songRepository.InsertAsync(xxx1);
await _detailRepository.DeleteAsync(xxx2);
uow.Commit();
}
}
}
```

Expand Down

0 comments on commit b8fd54d

Please sign in to comment.